user3421904
user3421904

Reputation:

Conditions in CSS based on the browser

How can I have CSS conditions based on the browser?

Just for example if the browser is IE:

div{

    [if IE ] background-color: yellow;  
}

Thanks

Upvotes: 1

Views: 412

Answers (3)

Kerry Smyth
Kerry Smyth

Reputation: 165

I found this page useful for selecting/isolating different browsers (http://browserhacks.com/)

Upvotes: 1

Manoj
Manoj

Reputation: 73

Syntax for conditional CSS:

IE-6 ONLY

* html #div { 
    height: 300px;
}


IE-7 ONLY

*+html #div { 
    height: 300px;
}


IE-8 ONLY

#div {
  height: 300px\0/;
}


IE-7 & IE-8

#div {
  height: 300px\9;
}


NON IE-7 ONLY:

#div {
   _height: 300px;
}


Hide from IE 6 and LOWER:

#div {
   height/**/: 300px;
}

html > body #div {
      height: 300px;
}

OR you can create two different css files one for IE specific and other one is common for other browsers.

If browser is not IE 6:

<!--[if !IE 6]><!-->
  <link rel="stylesheet" type="text/css" media="screen, projection" href="REGULAR-STYLESHEET.css" />
<!--<![endif]-->

If browser is greater than IE 7:

<!--[if gte IE 7]>
  <link rel="stylesheet" type="text/css" media="screen, projection" href="REGULAR-STYLESHEET.css" />
<![endif]-->

If browser is less than IE 6:

<!--[if lte IE 6]>
  <link rel="stylesheet" type="text/css" media="screen, projection" href="http://universal-ie6-css.googlecode.com/files/ie6.0.3.css" />
<![endif]-->

Upvotes: 0

Denis Tsoi
Denis Tsoi

Reputation: 10404

CSS is limited as it is not a programming language (it's a markup language)

If you are using a CSS preprocessing language like sass-lang, scss or less, you can get around that when you are in development. SASS Lang - CSS Preprocessing.

However, in your case, the condition is according to the browser type, (specifically IE).

Now there are some work arounds, such as Conditional Stylesheets [mentioned here CSS Tricks Conditional Stylesheets], or browser detection with javascript (on the front end, but this is considered bad practice).

The Caveat of Conditional Stylesheets is that it is applicable for IE 9 and under (the compatibility was removed for IE10)

Another thing you should also consider is whether you want to provide conditional stylesheets from your backend (and based on your request header, you can determine the browser type).

Upvotes: 0

Related Questions