admin管理员组文章数量:1325235
I have 2 CSS files for my site, one for all normal browsers and other for the retarded Internet Explorer. I am including either of the CSS file depending on a JavaScript which checks the browser capabilities. What would be the best option in case the user has disabled JavaScript! I should be including some CSS without which all the pages look ... well... naked. Any ments appreciated.
I have 2 CSS files for my site, one for all normal browsers and other for the retarded Internet Explorer. I am including either of the CSS file depending on a JavaScript which checks the browser capabilities. What would be the best option in case the user has disabled JavaScript! I should be including some CSS without which all the pages look ... well... naked. Any ments appreciated.
Share Improve this question asked Jun 9, 2009 at 5:28 theranemantheraneman 1,6304 gold badges20 silver badges32 bronze badges6 Answers
Reset to default 8you mean
<!--[if IE 6]>
and
<![endif]-->
don't work without javascript?
BTW, here are the first two google search results for "browser specific css":
- http://www.webmonkey./tutorial/Browser-Specific_CSS_Hacks
- http://www.sitepoint./article/browser-specific-css-hacks/
cheers!
What you need is conditional CSS statements. These are specially formatted HTML ments that only IE recognizes, they allow you to target different versions of IE with different CSS files.
There is a simple if statement
that that is put in the ment that IE is programmed to recognize
<!--[if IE]>
<link type="text/css" rel="stylesheet" media="all" href="css/ie.css" />
<![endif]-->
The above code will target every version of IE. You can specify which version by adding the version number
<!--[if IE 5.5]>
<link type="text/css" rel="stylesheet" media="all" href="css/ie.css" />
<![endif]-->
You can also make it apply to versions below or equal to a certain version number
<!--[if lte IE 6]>
<link type="text/css" rel="stylesheet" media="all" href="css/ie.css" />
<![endif]-->
The lte
is less then or equal to; you can also use gte
for greater than or equal to.
Using conditional ments is normally used to add CSS files but you can also add links to JS files that you may only need for certain IE versions.
Another thing if you end up using multiple stylesheets for different versions of IE is remember to name the file so it includes the version number you're targeting (IE-6.css
for example).
Try this hack:
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<!--[if IE]>
<link rel="stylesheet" type="text/css" media="screen" href="ie.css" />
< ![endif]-->
Using Javascript for browser detection is an extremely bad idea. There's no reliable way to do this. I presume you are using the navigator
object - note that this is very unreliable, as various browsers send the 'wrong' information or allow users to customise the information they send. Plus, as you note, there's the issue with non-Javascript enabled users.
The way to include browser-specific CSS, as everyone else has pointed out, is via IE's conditional ments.
<noscript>
cannot be used inside <head>
Also <noscript>
only detects if javascript was disabled by the browser (could be blocked by a firewall) so it cannot be guaranteed to work.
As for your two CSS files, the better strategy would be to have a base file that is always included and then an CSS file that has the changes/hacks needed for IE. Use conditional ments to load the ie CSS when the browser is IE.
<!--[if IE]>
<link rel="stylesheet" type="text/css" media="screen" href="ie.css" />
< ![endif]-->
<noscript>
<link rel="stylesheet" href="/global/mse/en/us/RenderingAssets/stuHome-IE.css"
type="text/css" media="screen, projection">
</noscript>
本文标签: Including Browser specific CSS file when JavaScript disabledStack Overflow
版权声明:本文标题:Including Browser specific CSS file when JavaScript disabled? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742165430a2425736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论