Conditional CSS (almost)

December 3, 2008
Tags: ,

Today I was hacking IE7, working on formatting issues caused by relative positioning in CSS.  IE7 likes to ignore the negative X positions (e.g. left: -87).   While Googling around I stumbled across an elegant hack from Rafael Lima that let’s me specify different CSS depending on the browser.

Add their one line of JavaScript to your global JavaScript file, then use a class selector to specify different CSS options based on the browser. Here’s an example in CSS that will change the color of a rectangle based on the browser you’re using. Brilliant!

  1.  
  2. .ie .example {
  3.   background-color: yellow
  4. }
  5. .ie7 .example {
  6.   background-color: orange
  7. }
  8. .gecko .example {
  9.   background-color: gray
  10. }
  11. .win.gecko .example {
  12.   background-color: red
  13. }
  14. .linux.gecko .example {
  15.   background-color: pink
  16. }
  17. .opera .example {
  18.   background-color: green
  19. }
  20. .konqueror .example {
  21.   background-color: blue
  22. }
  23. .webkit .example {
  24.   background-color: black
  25. }
  26. .example {
  27.   width: 100px;
  28.   height: 100px;
  29. }
  30. .no_js { display: block }
  31. .has_js { display: none }
  32. .js .no_js { display: none }
  33. .js .has_js { display: block }
  34.  

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes