将 Modernizr 与使用多种 css3 样式的 css 类一起使用?

发布于 2024-11-09 03:43:16 字数 1026 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2024-11-16 03:43:16

Modernizr 将类添加到 html 元素(请参阅 Modernizr 的工作原理文档)基于其测试结果。这意味着该元素具有所有这些类,您可以简单地链接多个类选择器来缩小选择范围。

如果浏览器不支持其中一个规则或两者都不支持,您现有的解决方案就会应用该规则,因此这与您正在寻找的内容相反。

请尝试使用此选择器:

.no-borderradius.no-boxshadow div.button {
    background: transparent url(my-button.png) left top;
}

关于您的评论,IE6 无论如何都不支持 border-radiusbox-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:

.no-borderradius.no-boxshadow div.button {
    background: transparent url(my-button.png) left top;
}

Regarding your comment, IE6 doesn't support both border-radius and box-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.

别闹i 2024-11-16 03:43:16

CSS 可以正常工作,因为支持 box-shadow 的浏览器也支持 border-radius [1]

.no-boxshadow div.button { background:transparent url(btn.png) 0 0; }
.boxshadow div.button { border-radius:5px; box-shadow:inset 1px 1px 2px #ccc; }

The CSS would work since the browsers that support box-shadow also support border-radius [1].

.no-boxshadow div.button { background:transparent url(btn.png) 0 0; }
.boxshadow div.button { border-radius:5px; box-shadow:inset 1px 1px 2px #ccc; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文