Muffinresearch Labs by Stuart Colville

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.

Post Tools

Comments: Add yours

1. On May 11th, 2005 at 12:23 pm Timbo said:

But Stupot, you haven’t followed this advice comple…

*gets coat*

2. On May 11th, 2005 at 11:42 pm Stuart said:

*Stuart enters stage left with tray*

3. On May 13th, 2005 at 3:33 pm Mike said:

I only discovered this technique a few weeks ago, nice one.

4. On February 19th, 2006 at 4:58 pm John Barry said:

Re reset the styles so that when you apply your stylesheet all browsers are starting from the same place-
Great advice often overlooked.

5. On March 6th, 2006 at 7:38 pm tsuki.es » hacks said:

[...] Una guia de c

6. On May 11th, 2006 at 2:01 pm Yahoo UI - Fonts - Nefarious Designs, Web Standards Design and Development, East Grinstead, West Sussex, UK said:

[...] 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. [...]

7. On May 12th, 2006 at 12:17 pm learner said:

Nice tips

8. On April 18th, 2007 at 6:41 am Brandon Drury said:

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

9. On July 13th, 2007 at 2:18 pm TimH said:

YUI has a section on this. Check out http://developer.yahoo.com/yui/reset/







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Using Loggerhead with mod_wsgi|(0)

Here’s a post I wrote over on the Project Fondue Blog about our use of Loggerhead with mod_wsgi under Apache. Loggerhead is the rather nice branch viewer for bazaar branches as used on Launchpad.net.

If you’re not already subscribed to the Project Fondue blog feed then I can recommend it, as there should be some interesting posts coming out of there in the coming months (yes I’m unashamedly biased!).

Ubuntu: Turn off changing workspace with mouse wheel|(1)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Photos on Flickr

© Copyright 2004-10 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.492s.