• Multiple CSS classes on the same element

    This one is pretty obvious to most CSS developers but it’s not actually used that widely on a lot of websites. It is really easy to apply multiple CSS classes to an element allowing you to mix and match various classes on elements. One instance I find it handy is when I have elements that require the same CSS as another element but simple things like the background color is different. Adding a second class to the element means I don’t need to re-write the class with an alternate color, I can just create an additional class to set the background color.

    The Code

    It’s really simple, all you have to do is separate each class with a space like below.

    <div class="first_class second_class"></div>

    This will reference both classes to style the element.

    This is a nice little trick to keep in mind for your sites.

3 Comments


  1. It’s a neat “trick” but be careful as always IE has some issues using this method.

  2. David says:

    I’d forgotten about that. Handy in some places.
    Though I’m not sure about using in production – I’ve been burned before using a trick or two then having a later version of browser blow up.

  3. It really nice idea but be careful with the issues of IE. Otherwise it’s good.

Leave a comment