The pain of cross browser CSS

Diego's picture

It's late at night, you've been coding CSS for hours, and after a while of googling and trying to solve rendering bugs, finally! The design works on Internet Explorer!

Yeah, it's always been a pain writing cross browser CSS, but you can save yourself some pain by knowing and using some helpful tips.

The way you reference HTML elements in your CSS can have a big impact on how IE will behave when it renders your design. I find it helpful to use only ids and classes in my main stylesheet and use tag names and ids/classes in the IE specific CSS...

  1. /* main stylesheet */
  2. #someId { styles... }
  3. .someClass { styles... }
  4.  
  5. /* ie stylesheet */
  6. div#someId { styles... }
  7. p.someClass { styles... }

The cascading nature of CSS makes that style declaration more specific, overriding the one defined in the main stylesheet. IE will ignore the main style and use the IE specific ones instead. So, when you try fix a discrepancy in the the rendering of your HTML, you won't have to scratch your head when you click refresh and nothing happens.

One thing to avoid is chaining your classes in a style declaration...

  1. <!-- HTML -->
  2. <div class="someClass someOtherClass"></div>
  1. /* IE will completely ignore this */
  2. p.someClass.someOtherClass { styles... }

I'll admit it, sometimes I don't even look at IE6 for a while because I'm so cozy in Firefox with Firebug and the Web Developer toolbar's live CSS editor. I mean, all I have to do is right-click and hit inspect element and I can tweak at will! I digress... After you've written a lot of CSS and find lots of bugs when you open the page in IE, it's always best to begin by validating your stylesheet AND your HTML templates. You'll be surprised how much this will clear up. I like to use the W3C validator, as it has lots of other useful tools...

This validator checks the markup validity of Web documents in HTML, XHTML, SMIL, MathML, etc. If you wish to validate specific content such as RSS/Atom feeds or CSS stylesheets, MobileOK content, or to find broken links, there are other validators and tools available. http://validator.w3.org

While we're talking about HTML, it's also good to use a valid DOCTYPE at the top of your HTML templates. I like to use XHTML, since it forces you to write clean markup...

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
  2. Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

...or if you prefer plain old html...

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
  2. Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Now, Doctype is a whole other subject. You can read more about it here -> w3schools.com

Please share your tips in the comments!


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>, <c>, <css>, <drupal6>, <html>, <javascript>, <mysql>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
You're human, right?
3 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

 Check it Out!
Future of Web Apps - Miami 2010

RSS

Syndicate content

RSS, XHTML, CSS

Copyright © 2009 Grey Robot, Inc.