Bar Chart in CSS and JavaScript

Illustrating a Web page with a bar graph, quickly and without using a framework that would slow the page load, you will be surprised to see that this can be achieved with a few lines of CSS and a single JavaScript function!

All it needed was 3 minutes 15 seconds, to copy paste the code from the demonstration and adding data to create the graph below:

The HTML code

Explaining the code of the demonstration.

Basically, the code is just a list:


The only thing that the webmaster has to add, is the data. In each list item, separated by the code ":", place the label for the bar, the amount or the percentage, and a color for the bar.
The JavaScript function is responsible for producing the graph.

To add axis and references, a container is added:

<div id="graph">
200<br /> <br /> <br /> 150 <br /> <br /> <br /> 100 <br /> <br /> <br /> 50

The choice of values depends on the application. For the market share of browsers, for example, a single number is sufficient.

Another container is added for labels:

<div id="labels">Years : </div>

Displaying automatically labels on the graph would be complicated. To have them displayed on the graph itself, you can capture the graph once generated and add text on the image with Paint.NET or The Gimp. (see software).

The CSS code

It turns <ul> container and <li> items into a bar graph.

It is especially important that the container has the absolute position inside the container that displays the landmarks and items an absolute position. The JavaScript function is responsible for positioning the bars inside the container.

#graph ul
height:200px; border-left:1px solid black;
border-bottom:1px solid black; }

Axis are created by a border of one pixel thick.

Bars are defined as:

#graph li
position:absolute; list-style:none;
border:1px solid black;
visibility: hidden;

The black border added to bars is an option.
A blue background color is given by default, it is modified by the JavaScript function.

Visibility delayed

To prevent the display of content of items before transformation in graphics by JavaScript, <li> tags are hidden at start:

visibility: hidden;

and then made visible:"visible";	

The JavaScript function

The role of this function is to create bars and display labels.

function makeGraph(container, labels)
    container = document.getElementById(container);
    labels = document.getElementById(labels)
    var dnl = container.getElementsByTagName("li");
for(var i = 0; i < dnl.length; i++)
var item = dnl.item(i);
var value = item.innerHTML; var color =;
var content = value.split(":");
value = content[0]; + "px"; - value) + "px"; = (i * 50 + 20) + "px"; = value + "px";
item.innerHTML = value;"visible";
color = content[2];
if(color != false);
labels.innerHTML = labels.innerHTML + "<span style='margin:8px;background:"+ color+"'>" + content[1] + "</span>"; }
} window.onload=function () { makeGraph("graph", "labels") }

We extract the DOM object of the container to modify locally <ul> <li> and not all these tags in the page.

For each tag <li> object graph:

This is sufficient to complete a bar chart to a page without adding a framework in a few minutes!







© 2010-2012