Dec
12

CSS Continued… Part 2:Reduce, Reuse, Recycle

By april.holle  //  technology reviews, XHTML/CSS  //  2 Comments

Making the web cleaner, one project at a time…

Remember the Cascade To Reduce Selector Use
Try a minimalistic approach to the number of ids and selectors you create. Use the cascade to your advantage by creating child elements of your current class instead of creating a whole new class to describe what you want that additional element to do. So for instance, you have a main body area, and in that area you have a unordered list that has a new list item style. You can code for this one of two ways. You could make two separate selectors (one for the main content area and one for the ul) and have these be totally unrelated to each other and have to reference the ul selector every time you create a ul in that content area, or you could do this:

#contentMain {float: left; width: 660px; padding: 20px;}
#contentMain ul {list-style: disc;}
#contentMain ul li {padding-bottom: 5px;}

This allows you to declare only the <ul> in your xhtml instead of <ul class="ulMain">. This keeps your code really clean and cascading down from the parent class is great because you’re not stuck using those styles everywhere for each ul. This is extremely handy when your dealing with user entered content from content management systems because they don’t have to remember they have to apply particular styles to these elements during development or even worse, when the client is entering the content themselves.

Don’t be afraid to style base xhtml elements, you can always go back and make it specific to a parent selector, or even add a selector to it if they really are necessary.

Create Reusable Selectors
If you notice yourself reusing a lot of css calls in every single selector you create you may want to make a reusable base class. Got a lot of elements that need to float left? Create:

.floatLeft {float: left;}

Then just double up your selectors to reduce the number of times you have to declare the float: left; CSS call in your stylesheet.

<div id="contentMain" class="floatLeft"> </div>

You can create reusable classes for all kinds of things, aligns, floats, widths, etc.

Make Your Classes Carpool
Doubling up your classes can really give you an upper hand when you have elements that need multiple reusable selectors. Instead of creating a selector that will change one aspect of a current class, try doubling up your classes instead.

.product {width: 150px; color: #666;}
.floatLeft {float: left;}

<div class="product floatLeft">Product details here.</div>

Minimize CSS Class Footprint Using Shorthand
You can shorten up your css calls per selector by taking advantage of some of the great efficiency built right into CSS. To help you remember margin and padding short hand use the word: TRouBled. The uppercase letters stand for Top, Right, Bottom, Left, so for instance you’re declaring padding for an element:

p {margin: top right bottom left;}

or you can even shorten it to two entries if your top-bottom and left-right margins are the same, like:

p {margin: topbottom rightleft;}

Another call that is really handy is the font call. So instead of:

p {
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 1.2em;
line-height: 1.6 em;
font-family: Arial, Verdana, San-Serif;}

you get:

p {font: font-style font-variant font-weight font-size/line-height font-family;}

Another smaller short hand notation is three value hex color codes. So for instance instead of #cc0000 you can do #c00.

2 Comments to “CSS Continued… Part 2:Reduce, Reuse, Recycle”

  • Yeah, and how is “.floatLeft” a separation of content and presentation? :-P When you want to change it to flaot right, either you have to change HTML (bad coding, no cookie) or turn “.floatLeft” into {float:right;} — and this will break everything.

  • Maciej,

    Valid point.

    And good catch, while I do try to separate content and presentation as much as possible, I often notice how little we actually reuse the content layer and thus I don’t really get my panties in a twist when I foul up on something like this naming structure.

Leave a comment

What is this?

This little blog happens to be the personal ramblings of one April Holle - I'm female, outspoken, webbie, a community evangelist, and Principal of Made Better Studio. Check out the about section for more info.

What People Are Saying…