I thought of another reason for a little fun with CSS today: percentage bars. First of all, here’s the example I’ve uploaded.
Now let’s start building the first bar. We’re going to use two div elements likes this:
<div id="box">
<div id="bar">50%</div>
</div>#box{width:250px;border:1px solid #000;height:17px;}
#bar{width:50%;background:#000;height:14px;color:#fff;text-align:right;
padding:3px 0px 0px 0px;}#box{width:250px;border:1px solid #000;height:17px;}
#bar{background:#000;height:14px;color:#fff;text-align:right;padding:3px 0px 0px 0px;}<div id="box">
<div id="bar" style="width:50%;">50%</div>
</div>
Now if we wanted to spice it up a little bit, we could add a background image instead of just a black background.#bar{background:url("bar.png");height:14px;color:#000;text-align:right;
padding:3px 0px 0px 0px;}
Again, you can take a look at the example file for a demo of all of options I just mentioned. If you’d like to reuse any of these on the same page, just change from id to class. This should work in pretty much any browser.
Matt,
Really cool percentage bars. Wouldn’t it make more sense though to use classes instead of id’s? That way you can reuse ‘box’ and ‘bar’ as many times as you want on a page.
Thanks for posting this, I will definitely be using these the next time I need percentage bars.
- Josh
@Josh I just added that as a note. I just used id for the example, but yes, good point.
Chris Thomson
posted on Dec 1, 01:53 AMThat’s really cool, I’ve been wondering how people make those!
Thanks!
-Chris
P.S. Dugg