Web development by Gabriele Romanato

Post navigation

Pure CSS bar chart

In this post I'd like to explain how to create a pure CSS bar chart. To accomplish this task, we need some data to show. In our example, we'll show the statistics of the 3 most used browser at the moment, namely Internet Explorer, Firefox and Chrome. On the top of each bar, we also want to include an icon for each browser. Let's start with our basic markup:

As you can see, each bar contains a different percentage. This is important, because we want our chart to contain actual data in order to avoid the use of empty elements. To create our bar chart, we first need to position the main container in the center of the viewport. This can be accomplished using absolute positioning:

The left and bottom border of our container will actually draw the y and x axes, respectively. Since our container has been absolutely positioned, a new contextual positioning has been created for its child elements. We can take advantage of this fact and absolutely position our bars as well:

Each bar has a different ID and has been positioned at the bottom of the main container. To display the level of each bar, we use a span element with a different background color, also positioned at the bottom of its parent container. Further, each bar has a different left offset, calculated by summing up the width of the bar (120 pixels) and adding an additional 20 pixels. Finally, each bar has a different background image that has been vertically positioned with a different offset. You can see a demo below.