CSS Chrome 条件注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1292258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome conditional comments
提问by Vince Panuccio
Is there such a thing as conditional comments for Chrome?
Chrome 有条件注释之类的东西吗?
I have a page that renders differently in Chrome when compared to Firefox.
与 Firefox 相比,我有一个在 Chrome 中呈现不同的页面。
Thanks
谢谢
回答by George Wiscombe
You can target WebKit based browsers using this in your CSS
您可以在 CSS 中使用它来定位基于 WebKit 的浏览器
@media screen and (-webkit-min-device-pixel-ratio:0) {
Body {}
}
Perhaps this will help?
也许这会有所帮助?
回答by DmitryK
<!--[if IE 8]><div id="bodyContainer" class="IE8"><![endif]-->
<!--[if !IE]>--><div id="bodyContainer" class="W3C"><!--<![endif]-->
<script type="text/javascript">
if (navigator.userAgent.toLowerCase().match('chrome') && document.getElementById('bodyContainer'))
document.getElementById('bodyContainer').className = document.getElementById('bodyContainer').className + " chrome";
</script>
Then you use CSS to tweak your styles specifically for Chrome.
然后您使用 CSS 专门为 Chrome 调整您的样式。
回答by dpq
Both HTML conditional commentsand Javascript conditional compilation directivesare only supported by Internet Explorer 4-8 to the best of my knowledge.
回答by Lorenzo816
<!--[if !IE]>-->
This isn't just a Chrome conditional comment - this hits all browser that aren't IE... firefox, safari, etc. If using PHP - try this:
这不仅仅是 Chrome 的条件注释 - 这会影响所有不是 IE 的浏览器...... firefox、safari 等。如果使用 PHP - 试试这个:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Browsser Detection</title>
<link rel="stylesheet" href="Main.css" type="text/css">
<?php
$msie = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false;
$firefox = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
$safari = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
$chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
if ($msie) {
echo '
<!--[if IE 7]>
<link rel="stylesheet" href="ie7.css" type="text/css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" href="ie8.css" type="text/css">
<![endif]-->
';
}
if ($safari) {
echo '<link rel="stylesheet" href="safari.css" type="text/css">';
}
?>
</head>
<body>
<br>
<?php
if ($firefox) { //Firefox?
echo 'you are using Firefox!';
}
if ($safari || $chrome) { // Safari?
echo 'you are using a webkit powered browser';
}
if (!$msie) { // Not IE?
echo '<br>you are not using Internet Explorer<br>';
}
if ($msie) { // IE?
echo '<br>you are using Internet Explorer<br>';
}
?>
<br>
</body>
</html>
Credit to John for posting: http://www.killersites.com/forums/topic/2731/firefox-google-chrome-browser-detecion-using-conditional-comments-hack/
感谢 John 发布:http: //www.killersites.com/forums/topic/2731/firefox-google-chrome-browser-detecion-using-conditional-comments-hack/
回答by p.g.l.hall
The conditional operation will work because other browsers will parse the If IE8 block as an HTML comment, but not the !IE block because the inside of it is wrapped in --> and
条件操作会起作用,因为其他浏览器会将 If IE8 块解析为 HTML 注释,而不是 !IE 块,因为它的内部包含在 --> 和
Therefore, for all non-IE browsers the body class will indeed equal W3C.
因此,对于所有非 IE 浏览器,body 类确实等于 W3C。
This is all by the way, though, because the IE comment block is not needed to identify the browser specifically as chrome - the JS block on its own will do that, provided the user has JS turned on of course.
不过,这都是顺便说一下,因为不需要 IE 注释块来专门将浏览器识别为 chrome - JS 块自己会这样做,当然前提是用户打开了 JS。