views:

88

answers:

2

If a site is looking the same in all required browsers, is it okay if my CSS is not valid?

Specifically, the CSS is not valid because:

  • I'm using vendor specific properties,
  • I'm using IE 6 and 7 hacks,
  • I'm using browser specific hacks

What benefits do I get if I make my CSS fully valid?

+2  A: 

The pros are simple --

  • Your site is more likely to render in multiple browsers in a uniform manner if you follow standards, and your code will be more machine readable.
  • Browser specific hacks are sometimes fine, but beware that you may have to maintain them as later browsers come out that don't support them.
  • (from below:) If you don't use fully CSS compliant code, you'll have to remember which bits are intentionally non-compliant every time you verify your code through a CSS parser/verifier.
  • It will illustrate any obscure bugs you've most likely missed, which have effect on things you won't have tested.

One thing you can do is have a special stylesheet for specific browsers. For example, for IE specific code, you can use special IE-only conditional comments to include the CSS. Here's an example to show a sheet called iestylesheet.css for IE 6 only:

<!--[if IE 6]> <link href="iestylesheet.css" type="text/css" rel="stylesheet" /> <![endif]-->

And obviously, the most important reason to use compliant CSS is that without doing so, you don't get that nifty W3C CSS compliant icon to put on your site. :-)

David Pfeffer
+1  A: 

If you keep your CSS fully valid, you can easily use the validator to check your CSS files for errors. (Because anything invalid will be a mistake.)

If you’ve got some intentional invalidity, you need to remember which bits of invalidity are intentional, and which aren’t. Future developers who work on the code will also need to work out which invalid bits are intentional.

So commenting intentional invalidity is a good idea.

Paul D. Waite
@Paul D. Waite - good point
metal-gear-solid
I do think that’s about it for actual benefits though. Generally, if CSS is invalid, it won’t work.
Paul D. Waite
I agree with the point above about CSS hacks though. CSS hacks are bits of invalid syntax that happen to have an effect in one particular browser. All bets are off in terms of what they might do in future browsers.
Paul D. Waite