CSS guidelines

When I was just getting into CSS a few years back, I was desperately searching for an article, that described how to structure my CSS, what techniques to use when in doubt, and such. While much can be found on the world wide web, I still prefer a good book over a lengthy article. Perhaps only a personal preference, but I tend to trust books more :)

Anyway, over the years I've formulated a few simple ground rules, that make my development much easier.

Getting all browsers on the same page

As you might now, different browsers tend to have their own predefined values for default font sizes and padding and margins for all elements. So to get them all on the same wave length I use a few simple rules:

/* get rid of default margins/paddings */
* {
    padding: 0;
    margin:  0;
}

/* 1em = 10px */
body {
    font-size: 62.5%
}

The first rule is self explanatory. It gets rid of all the default margins and paddings, giving you equal rendering of your page on all browsers.

The second I've just recently picked up. It allows me to make the default font size approximately 10px, wich in turn allows me to assign font sizes using the em unit as if I'm assigning it with px (font-size: 1.2em now equals font-size: 12px). The same can now be used to assign paddings and margins to elements, as the em is unaffected if the visitor decides to increase the size of the text (that is, the proportion remains the same).

Code structure

To separate my CSS into special sections I use flags. They're a nifty way to visualy separate your code, plus searching your code is now much easier, as you can simply jump to any section without the need to muddle your way through all the search results. For example, there can be many rules for an element #container, so by giving all the rules for this element a flag =CONTAINER you can now easily jump to that section by typing Ctrl+S (or any other search shortcut) and typing in =container.

There are many articles and books out there that propagate the use of separate files for each aspect of the site. For example fonts, colors and layout are to be kept in separate files. But I tend to shy away from that. Instead I separate my code into sections according to the XHTML document. I use the same order for elements as in the XHTML document.

...
<body>
<div id="head">...</div> <div id="navigation">...</div> <div id="content">...</div> <div id="footer">...</div> </body>
/* =HEAD
************************/
...

/* =NAVIGATION ************************/ ...

/* =CONTENT ************************/ ...

/* =FOOTER ************************/ ...

This way my CSS is structured like my XHTML, wich makes it easier finding your way around. But keeping it all in a single CSS file can generate an undesired overhead, so I separate my CSS into separate files linking to them only on the sites that need specific rules. Usually different pages use different elements, so there is no need to include those rules every time.

This comes in handy on middle to larger websites, but for small personal pages, you can still leave all of your code in one single file, as your linked CSS file is cached by the browser and not downloaded every time.

Applying margins and paddings

To my knowledge only IE has problems with the box model, so to avoid differences in rendering I use a simple rule. When applying width to an element, I do not apply left/right margins and padding directly to this element (the parent), but rather to subsequent elements (the children). The same is then used for elements with a specified height, this time with top/bottom margins and paddings. This way the widths and heights in IE are calculated according to the correct box model and not the proprietary IE box model, so in esence you avoid all those box model hacks and workarounds. It is a bit tedious work, but I've found it more semantic.

#container {
    /* do not apply paddings/margins to parent */
    width: 300px;
}

/* child of the #container */
#navigation {
    padding: 0 10px;
    margin:  0 20px;
}

Cascade

The cascade is cause for many of the headaches for newcomers to CSS. But when used with care, it can be a real asset. I try to use one or at the most two parent ids when targeting a specific element, wich means I can override the styles down the road with a more specific series of selectors, and avoid using !important rules.

Browser specific hacks

I try to avoid using hacks as much as possible. Most of the time browser pitfalls can be resolved using various workarounds (like the IE hasLayout property). With the IE7 release due soon, a lot of the IE targeting hacks have become outdated. It's a shame the developers at Microsoft have decided to drop the anynmous star (*) element from the DOM root, wich enabled developers to make IE specific hacks with the Holly hack. Those hacks still work for previous versions, but IE7 now overlooks them like all other modern browsers.

Recomended reading

tags:

Social bookmarking:

mikko, posted on 11th of September 2006 @ 23:49

gravataras far as I know

* {
padding: 0;
margin: 0;
}

messes Gecko forms... but I use it anyway :)

Miha, posted on 12th of September 2006 @ 0:17

gravatarThanks for the info mikko, I'll look into that :)

chiuaua, posted on 12th of September 2006 @ 15:44

gravatarAbout the IE hacks and IE7:
I think that some of small hacks for Exploder are quite useful...but to ensure my happy future, i put them into

chiuaua, posted on 12th of September 2006 @ 15:48

gravatarOops, the comment crashes when I put in the IF IE tag, naughty me =)

So the rest of my comment was:

...i put them into *< !- -[ if lte IE 5.5 ] > *(no spaces) and I'm all saved.

However, nice article. Especially liked the body font-size: 62.5%

chiuaua, posted on 12th of September 2006 @ 15:50

gravatarOk, I screwed it again. I meant to write *< !- -[ if lte IE 6 ] > *, offcourse :)

The preview / edit function would be awsome :)

Sorry for the mess

Miha, posted on 12th of September 2006 @ 15:52

gravatarYeah conditional commenting is another thing I try to avoid, but that is only for personal reasons. I don't like messing up my code with inline CSS, Javascript and those nasty looking conditional comments. But I guess that is the best way to go :)

Damn you, I'll have to write a special comment block just because of you ;)

Marko Samastur, posted on 12th of September 2006 @ 16:20

gravatarGood article.

I have nothing against conditional commenting, since I don't consider it a hack. It's an extension of one browser that targets that browser alone.

I don't use them though ;-)

Stuart, posted on 12th of September 2006 @ 17:41

gravatarI found this an interesting read.

Miha, posted on 12th of September 2006 @ 20:03

gravatar@Marko & Stuart: Thanks :)

@chiuaua: don't sweat it. I'm planning on adding a time limit to edit your comments, so you should not run into the same problem. Also I'm thinking of using live preview, just have to figure out how to design it :)

david, posted on 12th of September 2006 @ 21:25

gravatarjust be sure to use DOM or wait for me to implement it :)

very well written, keep 'em coming

Miha Filej, posted on 12th of September 2006 @ 21:31

gravatarHello Miha, I have to disagree with you when you say it's a shame the Holly hack doesn't work in IE7. A lot of issues had been fixed now and if they didn't remove the anonymous element coders would have to rewrite existig stylesheets for the new IE to render sites properly.

Miha, posted on 12th of September 2006 @ 22:19

gravatar@Miha Filej: Yeah that is true, didn't consider that :) Just hope that the final release will be bug free, because I have already found a few minor bugs in the beta version. But it is only a beta and doesn't mean they will be present in the final release.

@david: And what are you waiting for? :)

david, posted on 13th of September 2006 @ 21:03

gravatarspring :)

Post your comment

Comments are clossed (more than one month has passed since this entrie was published). If you still wish to comment the entrie, please do not hesitate to contact me.


Recent entries

Star entries

Currently reading

Advanced PHP Programming

Worth a look

Flickr

Pediatricna klinika