Tutorial: One Object With Mutliple CSS Classes
Thursday, July 8th, 2010Here is a great technique for intermediate CSS users. I use this technique often for Social Media icons, buttons or any objects that have similar yet different declarations. While this technique works in all major browsers you may have trouble getting this to work in browsers OLDER than IE6. I’ve noticed that WordPress uses this technique as well.
So let’s get the ball rolling. Let’s say we’re going to have 3 squares like below.
Here is the code I used.
.square {
width:30px;
height:30px;
display:block;
border:1px solid #333;
float:left;
margin:10px;
}
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
Now let’s set up three colors for our squares.
.red {background-color:#f00;}
.orange {background-color:#f39720;}
.blue {background-color:#00f;}
<div class="square red"></div>
<div class="square orange"></div>
<div class="square blue"></div>
How about we make one a circle using CSS3 because that’s how I like to party.
.circle {-moz-border-radius: 15px;-webkit-border-radius: 15px;}
<div class="square red"></div>
<div class="square orange circle"></div>
<div class="square blue"></div>
Here’s how this looks all put together. You can switch the colors or the circle class to any of the three squares.
<style type="text/css">
.post p {clear:both;}
.square {width:30px;height:30px;display:block;border:1px solid #333;float:left;margin:10px;}
.red {background-color:#f00;}
.orange {background-color:#f39720;}
.blue {background-color:#00f;}
.circle {-moz-border-radius: 15px;-webkit-border-radius: 15px;}
</style>
<div class="square red"></div>
<div class="square orange circle"></div>
<div class="square blue"></div>
Hope you enjoy this tutorial, it can really come in handy if you get creative. You could for instance make a class called .right and the only declaration could be that it floats the element to the right. If you have any questions let me know in the comments.








