将 Modernizr 与使用多种 css3 样式的 css 类一起使用?
我正在使用 Modernizr 为不支持 css3 的浏览器创建替代样式。它工作得很好,但我还没有找到使用多个 css3 样式的 css 类的解决方案。
示例 1:
div.button {
box-shadow: inset 1px 1px 2px #ccc; /* first css3 style */
border-radius: 5px; /* second css3 style */
}
/* fallback for browsers that don't support css3 */
.no-borderradius div.button {
background: transparent url(my-button.png) left top;
}
这里的问题是,如果浏览器支持 box-shadow 但不支持 border-radius,您将会遇到问题。在这种情况下,两个按钮都将使用 box-shadow,忽略 border-radius 并 将 no-borderradius 类应用于背景图像。我可以通过创建两个后备来解决这个问题。
示例2:
div.button {
box-shadow: inset 1px 1px 2px #ccc; /* first css3 style */
border-radius: 5px; /* second css3 style */
}
/* fallback for browsers that don't support css3 */
.no-borderradius div.button, .no-boxshadow div.button /* no-boxshadow added */ {
background: transparent url(my-button.png) left top;
}
这里的问题是,如果浏览器不支持 border-radius 和 box-shadow 这两个类将用于设置元素的样式,我不禁认为这会导致问题?
I am using modernizr to create alternative styles for browsers that don't support css3. It works great but i haven't found a solution with css classes that use more than one css3 style.
Example 1:
div.button {
box-shadow: inset 1px 1px 2px #ccc; /* first css3 style */
border-radius: 5px; /* second css3 style */
}
/* fallback for browsers that don't support css3 */
.no-borderradius div.button {
background: transparent url(my-button.png) left top;
}
The problem here is if the browser does support box-shadow but doesn't support border-radius you will run into problems. In this case both the button will use the box-shadow, ignore the border-radius and aply the no-borderradius class with the background image. I could solve it by creating two fallbacks.
Example 2:
div.button {
box-shadow: inset 1px 1px 2px #ccc; /* first css3 style */
border-radius: 5px; /* second css3 style */
}
/* fallback for browsers that don't support css3 */
.no-borderradius div.button, .no-boxshadow div.button /* no-boxshadow added */ {
background: transparent url(my-button.png) left top;
}
The problem here is that if the browser does'nt support border-radius and box-shadow both classes will be used to style the element, i can't help but thinking this can cause problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Modernizr 将类添加到
html
元素(请参阅 Modernizr 的工作原理文档)基于其测试结果。这意味着该元素具有所有这些类,您可以简单地链接多个类选择器来缩小选择范围。如果浏览器不支持其中一个规则或两者都不支持,您现有的解决方案就会应用该规则,因此这与您正在寻找的内容相反。
请尝试使用此选择器:
关于您的评论,IE6 无论如何都不支持
border-radius
和box-shadow
属性,因此应该没关系。当它将选择器读取为.no-boxshadow div.button
时,它仍然会应用该规则。查看我对这个问题的回答为例。Modernizr adds classes to the
html
element (see How Modernizr works in the documentation) based on its test results. That means that element has all those classes, and you can simply chain multiple class selectors to narrow down the selection.Your existing solution applies the rule if the browser doesn't support either one of the rules or both, so it'd be contrary to what you're looking for.
Try this selector instead:
Regarding your comment, IE6 doesn't support both
border-radius
andbox-shadow
properties anyway, so it shouldn't matter. It will still apply the rule as it reads the selector as.no-boxshadow div.button
. See my answer to this question for an illustration.CSS 可以正常工作,因为支持
box-shadow
的浏览器也支持border-radius
[1]。The CSS would work since the browsers that support
box-shadow
also supportborder-radius
[1].