Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect the user’s browser and apply a specific CSS file?

Tags:

css

user-agent

I need to detect the browser and apply the matched CSS file.

I have created 3 css files: __ie.css, ff.css, opera.css. Now I need to detect the browser in order to include the good one.

I know this

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

But how do I do the same with Firefox and Opera/Chrome?

like image 270
Sourav Avatar asked Apr 11 '11 13:04

Sourav


People also ask

How do you determine if the browser supports a certain feature in CSS?

The concept of feature detection The idea behind feature detection is that you can run a test to determine whether a feature is supported in the current browser, and then conditionally run code to provide an acceptable experience both in browsers that do support the feature, and browsers that don't.

How do you tell which browser a user is using?

To detect user browser information we use the navigator. userAgent property. And then we match with the browser name to identify the user browser. Now call this JS function on page load, and this will display the user browser name on page load.

How do I display a specific HTML browser?

Open the Google Chrome page Open the Google Chrome page of the specific HTML that you want to inspect. Press "Control" + "U" on the keyboard and a separate page with the source code appears.


2 Answers

The closest you can come with pure CSS is with feature queries. Instead of detecting the browser type/version, it allows you to check if a specific property/value combinations are supported by the browser.

The following is an example of using the transform property for vertical centering. If the browser doesn't support transform, then we don't want to adjust its positioning:

@supports (transform: translateY(-50%)) {
    .foo {
        position: relative;
        top: 50%;
        transform: translateY(-50%);
    }
}

Browser support for Feature Queries

like image 104
cimmanon Avatar answered Oct 24 '22 12:10

cimmanon


If you have to detect browsers just to apply CSS, then you might want to rethink your CSS before going to browser-specific stylesheets. All it takes is for one browser to mimic another's user agent string, or a new version to be released, and everything breaks. Use the current standards and validate your code (http://validator.w3.org/), and you'll have to worry about far fewer cross-browser issues. Even just using <!--[if IE]><![endif]--> without a version number could break the layout in later versions.

That being said, if you want to style the page differently based on what CSS features are available, take a look at Modernizr. This way, you're only checking features, which won't be broken if a new version of the browser is released.

If all else fails and you really need to detect the visitor's browser, try jquery.browser. It's built into jQuery, and is simple to use. http://api.jquery.com/jQuery.browser/.

like image 33
derekerdmann Avatar answered Oct 24 '22 11:10

derekerdmann