A beginner’s guide to CSS hack avoidance | Comments (9)
Posted in CSS on 10th May 2005, 7:04 pm by Stuart
Whilst working on some CSS designs recently I realised that by zeroing the padding and margin of various elements in I was avoiding my pages having major differences when viewed in different browsers. Often these differences in rendering are caused by the default styles applied to pages by the browser. It’s these styles that come into play whenever you view a page without a stylesheet.
Once you learn how to use CSS hacks if you are at all inclined to be lazy you might find yourself utilising CSS hacks even when there’s no real need. So before you use that hack you must ask yourself the question is there an easier way?
To illustrate the point I knocked up a quick page to test out the theory that all browsers are different before you even start to apply CSS rules. In addition here is an screenshot to show the difference side-by-side for IE 6.0, Firefox, and Opera 8.0.
As you can clearly see there’s a need to reset the styles so that when you apply your stylesheet all browsers are starting from the same place. One way to do this would be to use global rules on all of the tags that you are going to use. Like so:
p, h1, h2, h3, h4, h5, h6, ul, ol, fieldset, form {
margin : 0;
padding : 0;
}
In doing this you will have to then specify the paddings and margins on each element that you are styling manually. Which is no big deal, however, you could specify a global rule including your own defaults and work forward from that:
p, h1, h2, h3, h4, h5, h6, ul, ol, fieldset, form {
margin : 1em 0 0 0;
padding : 0;
}
Alternatively for browsers that understand it you could create a global rule for all elements like so:
* {
margin : 0;
padding : 0;
}
Thus saving you from having to add each element separately. See the links below for more on this method.
Now before anyone comments that I haven’t followed this advice completely myself, well that’s true but I’ve only implemented this where I thought it most important, how far you go with this is entirely up to you!
Now before I posted this article I did a quick google to see if anyone’s been here before I have, and there are a couple of articles that touch on the same area, so kudos goes to them for their endeavours.
- Richard Rutter (Clagnut) – Resetting default padding and margin
- Eric Meyer – Really Undoing html.css

But Stupot, you haven’t followed this advice comple…
*gets coat*
*Stuart enters stage left with tray*
I only discovered this technique a few weeks ago, nice one.
Re reset the styles so that when you apply your stylesheet all browsers are starting from the same place-
Great advice often overlooked.
[...] Una guia de c
[...] I’m not going to get up on my soap box about this (neither do I wish to pontificate – you never know when you might be wrong), but I’m pretty certain most of us are aware that hacks are a bad method to adopt when trying to solve cross-browser issues. If you’re interested in reading a little more, there’s some informative posts at Vitamin, Digital Web and Muffin Research. [...]
Nice tips
Thanks! While I usually think to zero out elements that are giving me problems, it’s very helpful to go ahead and take care of it right from the start.
Brandon
YUI has a section on this. Check out http://developer.yahoo.com/yui/reset/