Muffinresearch Labs by Stuart Colville

A beginner’s guide to CSS hack avoidance | 9 Comments

Posted in CSS on 10th May 2005, 7:04 pm by

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

  • http://www.nefariousdesigns.co.uk/ Timbo

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

    *gets coat*

  • http://www.muffinresearch.co.uk Stuart

    *Stuart enters stage left with tray*

  • http://www.iamthepenguin.com Mike

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

  • http://homepage.eircom.net/~westwaterford/index.htm John Barry

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

  • Pingback: tsuki.es » hacks

  • Pingback: Yahoo UI - Fonts - Nefarious Designs, Web Standards Design and Development, East Grinstead, West Sussex, UK

  • learner

    Nice tips

  • http://www.recordingreview.com Brandon Drury

    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

  • TimH

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

GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(2)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

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