Resetting your CSS and your sanity

Written by That Web Guy on 14th June 2009. 9 comments

Resetting your CSS and your sanity
  • Avant Innovations
  • Advertise Here

Did you ever notice how browsers seems to have slightly different ideas on how certain HTML elements are displayed? If you're like me then you once lost the same amount of time it takes for breakfast cereal to go soggy before you figured out what was going on.

The image below perfectly demonstrates the small inconsistencies between elements in different browsers, yet these are often more than enough to throw your pixel perfect design into chaos.

Browser Differences

You can save your sanity by resetting chosen HTML elements so that they act the same across different browsers. Because even despite their differences, one thing all browsers agree on is that zero is still zero.

At the beginning of your CSS file just include the elements you want to have reset.

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
}

This is a typical reset that I would use but you can of course specify any elements you like. With the reset in place the browsers finally agree on something. Here's proof:

Browser Differences Reset

Right away you can see your CSS life just got a whole lot easier.

What's the catch?

As with some great ideas, there's a trade-off. You've probably already worked out that anything you include in the reset will have to be manually styled further down your style sheet. So taking UL as an example, now that it's reset it means you're going to have to declare padding, margins etc for UL's further into your style sheet. But that's the whole point of this exercise - to give you that control and avoid cross-browser discrepancies.

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Greg Molyneux

Greg Molyneux

Excellent tip, I'll be resetting in my style sheets from now on. Someday in the mythical future we can only hope all browsers will be created equally (someday...).

Monday 15th June 2009 | 06:13 AM Reply Comment URL Profile Back to top

Harun Smrkovic

Harun Smrkovic

This seems like a good deal..
But, is it really this simple to fade away all the cross-browser compatibility problems ?...

Monday 15th June 2009 | 07:57 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Harun Smrkovic

Ha ha no that would be too easy. This is more about design consistency across different browsers but it has the added bonus of eliminating other potential problems.

Monday 15th June 2009 | 08:07 AM Reply Comment URL Profile Back to top

Harun Smrkovic

Harun Smrkovic

Responding to this comment by That Web Guy

Anyway, it's a good tip ;)

Monday 15th June 2009 | 05:50 PM Reply Comment URL Profile Back to top

Kim OJ

Kim OJ

Hey! I think this may be just what I needed to deal with a headache.

Thursday 18th June 2009 | 05:08 AM Reply Comment URL Profile Back to top

Not a Member

Stefan van Zanden

Just wondering, why not save the "hassle" of defining and forgetting some elements and just use something like the following:

* {
margin: 0;
padding: 0;
}

?

Friday 31st July 2009 | 04:30 PM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by Stefan van Zanden

Yes can do, and that resets everything as you know. It's also considered by some as bad practice, which I'm inclined to agree with. Although I've not personally experienced any issues associated with using it, it's still a valid point.

http://www.christianmontoya.com/2007/02/01/css-techniques-i-use-all-the-time/

Friday 31st July 2009 | 07:17 PM Reply Comment URL Profile Back to top

Stefan van Zanden

Stefan van Zanden

Responding to this comment by That Web Guy

Thanks for pointing out that link to me,
made me look at it with a whole new perspective,
it never occured to me that it could cause a (altough minor) performance hit and resetting elements that don't need to be resetted is pretty much a waist :-) (which indeed is a bad practice).

Friday 31st July 2009 | 09:12 PM Reply Comment URL Profile Back to top

Josiah

Josiah

Nice post mike, nothing like a good reset.

Tuesday 22nd December 2009 | 03:17 PM Reply Comment URL Profile Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login