Save Time By Using Multiple Classes

Quick Tip: Save Time by Using Multiple Classes

Tutorial Details
  • Difficulty: Beginner
  • Completion Time: 5 Minutes

One of the most exhausting aspects of web development is writing out endless amounts of CSS styling for various elements – that differ only slightly in style from one another. It has taken the life (or will) of many a talented designer. Well, why give each element its own unique CSS entry, when you can mix match and add re-usable CSS styling to elements using multiple CSS classes?

Step 1: Set Up The CSS

Lets start with identifying and writing out some of our most common CSS markup. We’ll set them up as individual styles. The ones that we’ve set up here are the most basic, but the same technique can be applied to even the coolest CSS 3 properties.

.primaryColor {
color: blue;
}

.articleImage {
border: 1px solid #292929;
padding: 5px;
}

.standOut {
font-size: 22px;
}

.important {
font-weight: bold;
}

.floatRight {
float: right;
}

Step 2: Applying The Classes In Our Document

  • <p class=”primaryColor important”>This text will be blue and bold</p>
  • <img src=”#” class=”articleImage floatRight”>This image will be floated to the right, and will have some padding and a border.</p>
  • <p class=”standOut”>This text will be huge</p>
  • <p class=”primaryColor standOut important”>This text will be blue, 22px large, and bold. </p>

Why It’s Useful

This can be a massive help, especially when designing layouts… you can use one class to define the size and width, and another class to define the positioning. Hope this helped!

Editor’s Note: Keep in mind that, in some of the older browsers – meaning older than IE 6 – this “multiple classes” method will fail. What are your thoughts on this approach? P.S. Have a killer quick tip? Email us and we’ll talk.

Tags: tips
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.itcamefromthelab.com/ sean steezy

    I tried doing it this way for a website once… it was a nightmare. My inheritance sucked and I had multiple style overwriting eachother… I need to get schooled on hierarchy and inheritance.

  • Ethan

    I’m strange in the way I switch classes and ida.

  • Greg

    @Dino – its not actually a typo but a bug in the presentation widget used to present the code sample. i’ve seen this before on other blog articles.

  • Susan Litton

    I found this tip to be very helpful. I understand the ideas behind the notion that classes should be named to reflect content instead of styles. However, I’m working on a very large site and I keep forgetting the hex codes of my primary and secondary colors. I write them on scraps of paper on my desk but I can never find them when I need them. Creating .primaryColor and .secondaryColor classes could be helpful.

  • Alex

    All this complaining about descriptive named classes, did the code examples change since this article was published?

    Seems like..

    .primaryColor{

    }

    .articleImage{

    }

    define structure/are named in a way that makes them reusable.

  • Pingback: Web Design and Development Tutorials | Serene Destiny

  • http://maddyyard.com Mandip

    Its awesome way :)
    it helped me a lot
    thanks for the post