• Home
  • Patel Strategy
Subscribe: Posts | Comments | E-mail
  • Business
  • Design
  • Experiments
  • WordPress
  • News

Impress Lab

Posted on August 29, 2008 - by Impress Lab

Using CSS for Bar Graphs

Design

You do not need advanced PHP scripting or XML data to create a web graph. All you need is simple CSS and HTML. You can use graphs to display data more illustratively. This tutorial is going to show you how to take boring data in a table and display it in a bar graph. The technique is simple, but the affect is drastic. This is what we will be creating:

Sample Graph

55%
36%
75%

This data is not accurate. Do not cite this please.

Purpose

Graphs are very useful, especially on the web. Considering that the average website visitors spends only a few seconds or a minute on a page, you need to display information that can be viewed quickly. Graphs display information better than a table of data.

Now, that does not mean data tables are useless. In fact, they are also important for people doing research. As a best practice approach, you should make data tables available, but also include an appealing graph. So let’s get started.

The CSS

We will first start with some simple CSS:

    #graphbox {
border: 1px solid #e7e7e7;
padding: 20px 20px;
width: 250px;
margin-right:10px;
float:left;
background: #f8f8f8;
}
#graphbox h2{
color: #666666;
font-family:Arial, sans-serif;
font-size:1.1em;
padding-bottom: 0.3em;
font-weight:bold;
text-align:center;
}
#graphbox p {
color: #888888;
font-family:Arial, sans-serif;
font-size:0.8em;
padding-top: 0.3em;
text-align:left;
}
.graph {
position: relative;
background: #F0EFEF url(images/graphpaper.gif) left;
border: 1px solid #cccccc;
padding: 2px 2px;
}
.graph .orangebar, .greenbar, .bluebar {
text-align: left;
color: #ffffff;
height: 1.8em;
line-height: 1.8em;
font-family:Arial, sans-serif;
display: block;
position: relative;
}
.graph .orangebar {
background: #ff6600;
}
.graph .greenbar {
background: #66CC33;
}
.graph .bluebar {
background: #3399CC;
}
.graph .bar span {
position: absolute; left: 1em;
}

There are a few key points about the styling. First, the width of the entire graph is set by the graphbox div; in this case 250px. This outer box has 20px paddings on all sides, so the actual graph (the colors) are only 210px wide (250px - 20px from left - 20px from right). Adjusting this graphbox width will adjust the width of the colored graphs. Also, the graphbox div in this case uses a light gray background to help the graph standout from the white page. The graphbox div has left floating so that the main page text can wrap around the graph like an image. We used left float because the left side of your main page text is straight as opposed to a jagged edge on the right site (unless you have justified text alignment).

For the graph title, Heading 2 styling is used. The footnote at the bottom of the graph is simply the paragraph styling. This is a great place to put your source.

Our graph here has three colors, orange, green, and blue. Each bar graph has some common styling as shown:

.graph .orangebar, .greenbar, .bluebar {
text-align: left;
color: #ffffff;
height: 1.8em;
line-height: 1.8em;
font-family:Arial, sans-serif;
display: block;
position: relative;
}

The only styling that is unique is the background color for each, which encompasses the next part of the CSS. Finally, the span styling is what actually is used to display the width of each colored bars in percentage terms.

You will also need the graph paper background and upload it so that the path matches the CSS. The graphpaper.gif image:
graphpaper.gif

The HTML

Take a look at the following HTML, which creates the graph directly below.

<div id="graphbox">
<h2>Student Grades</h2>
<div class="graph"><strong class="orangebar" style="width: 55%;">Steve: 55%</strong></div>
<div class="graph"><strong class="greenbar" style="width: 90%;">Linda: 90%</strong></div>
<div class="graph"><strong class="bluebar" style="width: 75%;">Bill: 75%</strong></div>
<div class="graph"><strong class="orangebar" style="width: 32%;">Jill: 32%</strong></div>
<div class="graph"><strong class="greenbar" style="width: 98%;">Jose: 98%</strong></div>
<div class="graph"><strong class="greenbar" style="width: 85%;">George: 85%</strong></div>
Green: Exceptional. Blue: Marginal. Orange: Needs Improvement.</div>

And the outputted graph:

Student Grades

Steve: 55%
Linda: 90%
Bill: 75%
Jill: 32%
Jose: 98%
George: 85%

Green: Exceptional. Blue: Marginal. Orange: Needs Improvement.

You can easily modify the colors by simply changing the background colors in the CSS and the class names in the HTML. Also notice that the graph floats to the left. Depending on the amount of data to display, you may want to have the graph width equal 100% the width of your main content div. But in this case we wanted a simple graph that can stand next to body text.

While this graph is not dynamic (this would require PHP scripts and database calls, or XML data), it is perfect for static data and is much better than using a table to display data.

This entry was posted on Friday, August 29th, 2008 at 11:32 am and is filed under Design. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

13 Comments

We'd love to hear yours!



  1. Visit My Website

    August 29, 2008

    nichols said:

    great css examples :)



  2. Visit My Website

    August 29, 2008

    B. Scott said:

    this is a really obvious technique that nobody knows about - i wish i knew about this before. it is cool to have little graphs in your pages!



  3. Visit My Website

    August 31, 2008

    Websites You Shouldn´’t Have Missed in August said:

    [...] - Using CSS for Bar Graphs [...]



  4. Visit My Website

    August 31, 2008

    » links for 2008-08-31 said:

    [...] Using CSS for Bar Graphs | Impress Lab (tags: useful resources css webdesign) [...]



  5. Visit My Website

    August 31, 2008

    S A N D E E P [ I N D I A N I C ] » Blog Archive » Websites You Shouldn’t Have Missed in August 2008 said:

    [...] - Using CSS for Bar Graphs [...]



  6. Visit My Website

    September 1, 2008

    Adwa said:

    That’s a great way!



  7. Visit My Website

    September 2, 2008

    rss blog » Blog Archive » Websites You Shouldn’t Have Missed in August 2008 said:

    [...] - Using CSS for Bar Graphs [...]



  8. Visit My Website

    September 4, 2008

    Websites You Shouldn’t Have Missed in August 2008 said:

    [...] - Using CSS for Bar Graphs [...]



  9. Visit My Website

    September 8, 2008

    web design company said:

    annisdalX - Using CSS for graphs - can be used for ratings stars as well



  10. Visit My Website

    September 9, 2008

    Websites You Shouldn’t Have Missed in August 2008 | POLPDESIGN said:

    [...] - Using CSS for Bar Graphs [...]



  11. Visit My Website

    September 15, 2008

    Technology Story» Blog Archive » Website You Shouldn’t Have Missed said:

    [...] - Using CSS for Bar Graphs [...]



  12. Visit My Website

    October 1, 2008

    Gráficos de barra com CSS » CSS no Lanche said:

    [...] tutorial é uma livre tradução do original de: Impress Lab. Categorias: CSS Tags: [...]



  13. Visit My Website

    October 11, 2008

    Identium Reklambyrå said:

    Thanks for this useful CSS trix. Im sure it will come handy!



Leave a Comment

Here's your chance to speak.

  1. Name (required)

    Mail (required)

    Website

    Message

    August
    29th
    2008
    • 13 Commentswe'd love to hear yours!
    • Trackback from your own site
  • Recent Articles

    • Business Realignment: Services move to Patel Strategy September 26, 2008
    • Designing through Writing September 11, 2008
    • Bad Practices: Website Login Form Placement September 10, 2008

© 2008 Impress Lab - Minnesota Creative Marketing
A Patel Strategy Production