内嵌 CSS IE 黑客

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/213309/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:03:41  来源:igfitidea点击:

In-line CSS IE hack

css

提问by John Dunagan

Is it possible to create, for instance, a box model hack while using in-line CSS?

例如,是否可以在使用内嵌 CSS 的同时创建一个盒子模型?

For example:

例如:

<div id="blah" style="padding: 5px; margin: 5px; width: 30px; /*IE5-6 Equivalent here*/">

<div id="blah" style="padding: 5px; margin: 5px; width: 30px; /*IE5-6 Equivalent here*/">

Thanks!

谢谢!

回答by Atanas Korchev

You can use the "prefixing" hack in inline styles as well:

您也可以在内联样式中使用“前缀”技巧:

<div style="*background:red"></div>

Just make sure you put the IE hacks at the end of the style attribute. However I second the opinion that inline styles should be avoided when possible. Conditional comments and a separate CSS file for Internet Explorer seem to be the best way to handle such issues.

只要确保将 IE hacks 放在 style 属性的末尾。但是,我认为应尽可能避免内联样式。Internet Explorer 的条件注释和单独的 CSS 文件似乎是处理此类问题的最佳方法。

回答by John Dunagan

I'd go outside - slap a class on that element, or use the ID you have, and handle the styling externally.

我会去外面 - 在该元素上打一个类,或者使用您拥有的 ID,并在外部处理样式。

I'd also concur with the conditional comments answers preceding mine.

我也同意我之前的条件评论答案。

That said: As an absolutelast resort, you can use the following style hacks to target <= IE6, and even IE7. The trouble comes when/if they fix IE8 to defeat your hack.

也就是说:作为绝对的最后手段,您可以使用以下风格黑客来定位 <= IE6,甚至 IE7。当/如果他们修复 IE8 以击败您的黑客时,麻烦就来了。

.foo {
padding: 5px;
^padding: 4px; /* this targets all IE, including 7. It must go first, or it overrides the following hack */
_padding: 3px; /* this targets >= IE6 */
width: 30px;
}

Good luck.

祝你好运。

回答by alexp206

Without arguing for or against CSS hacks, personally if I needed to do something like that, I would prefer to use a conditional comment:

不支持或反对 CSS hacks,如果我需要做类似的事情,我个人更愿意使用条件注释:

<!--[if lt IE 7]>
<style>
#blah {
padding: 5px;
margin: 5px;
width: 30px;
}
</style>
<![endif]-->

回答by eyelidlessness

The most appropriate answer is don't. (Edit: to be clear, I mean don't do it inline, I don't mean don't use CSS hacks.)

最合适的答案是不要。(编辑:要清楚,我的意思是不要内联,我不是说不要使用 CSS hacks。)

Edit: This doesn't work, IE ignores the conditional comment. Leaving the answer here to not be a bastard.

编辑:这不起作用,IE 忽略条件注释。把答案留在这里,不要成为一个混蛋。

The next most appropriate answer is conditional comments:

下一个最合适的答案是条件注释:

<div id="blah" style="padding: 5px; margin: 5px; width: 30px; <!--[if lte IE 6]> ... <![endif]-->">

回答by savetheclocktower

Keep in mind that IE 6 needs the box model hack in quirks modebut not in standards mode. IE 5 and IE 5.5 need the BMH all the time.

请记住,IE 6在 quirks 模式下需要 box model hack 在标准模式不需要。IE 5 和 IE 5.5 一直需要 BMH。

So if you're in standards mode, you'll need to use something like the original voice-familyhack(which targets IE 5.X and notIE 6). If you're in quirks mode, any old IE <= 6 hack will do.

因此,如果您处于标准模式,则需要使用类似于原始voice-familyhack(针对 IE 5.X 而不是IE 6)的东西。如果您处于怪癖模式,任何旧的 IE <= 6 hack 都可以。

(The content of your question suggests to me that your page renders in quirks mode.)

(您问题的内容向我表明您的页面以怪癖模式呈现。)

回答by Paul M

Yeah like everyone above, if you can avoid doing it inline do!! But if you really need to go inline then parser filters are probably your best bet, these are certain characters you can use on properties such as the underscore hack in ie6

是的,就像上面的每个人一样,如果你可以避免内联这样做!!但是,如果您真的需要内联,那么解析器过滤器可能是您最好的选择,这些是您可以在属性上使用的某些字符,例如 ie6 中的下划线 hack

print("code sample");

 style="position:relative;padding:5px; _position:absolute; _padding:10px;" 

ie6 will still get the underscored styles, everyone else will just ignore them!

ie6 仍然会得到下划线的样式,其他人都会忽略它们!

There is also using !important instead of underscore.

还有使用 !important 而不是下划线。

have a play around and see what happens, but again like above, try and avoid like the plague :)

玩一下,看看会发生什么,但还是像上面一样,尽量避免像瘟疫一样:)

回答by Gabriel Hurley

The best solution is to work in Standards Mode rather than Quirks Mode.... that'll eliminate the need for the majority of your box model hacks right away.

最好的解决方案是在标准模式下工作,而不是在 Quirks 模式下工作......这将立即消除大多数盒模型黑客的需要。

Beyond that, conditional comments with an IE-specific stylesheet are far cleaner and more maintainable. That method has been good enough for most every top-notch designer for the last several years... unless there's something specific about your site that requires it all to be inline I suggest taking a step back and looking at the root problems instead of how you can patch these little symptoms as they appear.alt text http://sonicloft.net/im/52

除此之外,带有特定于 IE 的样式表的条件注释更清晰、更易于维护。在过去的几年里,这种方法对于大多数顶尖设计师来说已经足够好了......除非你的网站有一些特定的东西需要全部内联,我建议退一步看看根本问题而不是如何您可以在出现这些小症状时对其进行修补。替代文字 http://sonicloft.net/im/52