Jin Yong
Jin Yong

Reputation: 43768

browser type detect in css

Does anyone know how can I detect the browser type in css? What I trying to do is when the user viewing the page by using IE 8, then will set class 'black2' to { font-size:0.75em; color:#1e549b;} in css else will set it to { font-size:0.95em; color:#1e549b;}

Upvotes: 0

Views: 6438

Answers (4)

David Andres
David Andres

Reputation: 31781

I am mentioning this for the sake of completeness, but I strongly advise that you go a different route because CSS expressions impact performance and may not be cross-platform-friendly:

You can use a CSS expression coupled with JavaScript to dynamically set your styles. An example:

<style>
 P 
 {
   background-color: expression( getBackgroundColor(this) );
 }
</style>

<script type="text/javascript">
  function getBackgroundColor(elem)
  {
    return "green";
  }
</script>

You can use this JavaScript function to sniff out the browser version (also not the best idea) and color accordingly.

Upvotes: 0

Jersey Dude
Jersey Dude

Reputation: 607

If you are running server-side code (php, asp.net, asp, ..), you would be better off detecting the browser on the server and including an additional style sheet in the header for browser specific code.

Otherwise, the are a number of browser hacks you could use to inject CSS for different browser types and version. As they are "hacks", the css gets ugly fast.

Upvotes: 0

meder omuraliev
meder omuraliev

Reputation: 186562

<!--[if IE 8]>
  <style>.black2 { font-size:0.75em; color:#1e549b;} </style>
<![endif]-->

Not really recommended...but why are you serving a different font size? You can't get them to be consistent? Have you tried setting an initial percent, then use ems?

Upvotes: 2

Gavin H
Gavin H

Reputation: 10482

See this similar question

You can use special commenting for IE detection in the CSS, or you can do the logic using javascript and apply the CSS classes programmatically (for example using JQuery).

Upvotes: 5

Related Questions