Reputation: 383
Is there a way to use different code depending on the user's browser? For example, if the user is using Internet Explorer, is there some sort of way to pass in different code than if the user were using Chrome?
Thank you!
Upvotes: 0
Views: 1488
Reputation: 6355
If you want to target IE
<!--[if IE]> -->
<link rel="stylesheet" href="css/style.css"/>
<!--[endif]-->
you can target firefox with simple commands in your main css file by this command:
@-moz-document url-prefix() {
}
Upvotes: 1
Reputation: 54359
The preferable method in most scenarios is to use feature detection, i.e. determine if a feature is/is not supported and act accordingly. A library like Modernizr can assist with both detecting features and smoothing out some of the differences.
For example, detecting the "touch" feature is much better than looking for a mobile browser user agent string or version.
In practicality, it is often easiest to use conditional comments for IE-specific issues since IE supports them and it's usually possible to group related issues by browser version (versus other scenarios where feature detection is much easier). In other words, when dealing with Internet Explorer, you can often say "apply these hacks/fixes for all IE X users".
Support for conditional comments is removed in IE 10.
Upvotes: 2
Reputation: 8022
Yes there is a way to do it. Please check out Conditional Comments:
http://www.quirksmode.org/css/condcom.html
And CSS Hacks
http://www.quirksmode.org/css/csshacks.html
Upvotes: 3