CSS 保留父级规则而不是类

发布于 2024-10-05 16:49:03 字数 1504 浏览 4 评论 0原文

在这种情况下:

<ul id="menu_pages">
                      <li class="menu_pages_title">
                        <div id="menu_pages_container">
                            <a href="#">AAA</a>
                            <div class="page_content">

                            <div class="page_content_actions">
                                    <a class="edit" href="#">BBB</a>
                            </div>
                                test 1
                            </div>
                        </div>
                    </li>

</ul>

ul#menu_pages li{
    margin-bottom: 10px;
}

    ul#menu_pages li a:link,ul#menu_pages li a:active,ul#menu_pages li a:visited{
        display:block;
        padding:5px;
        border:1px solid #C3D5DF;
        line-height:25px;
        font-size: 15px;
        font-weight: bold;
        color:#C3D5DF;

}

div.page_content_actions li a.edit:link, div.page_content_actions a.edit:active, div.page_content_actions a.edit:visited {
                    background: url('/site_images/page_edit.png') left no-repeat;
                    display:block;
                    padding:0;
                    border:0;
                    font-size: 11px;
                    font-weight: bold;
                    color:#fff;

                }

.edit 类应该获得自己的规则集(在 .page_content_actions 内),但 .edit 只获取背景,其余部分来自一般的 #menu_pages li a。这是为什么?

in this situation:

<ul id="menu_pages">
                      <li class="menu_pages_title">
                        <div id="menu_pages_container">
                            <a href="#">AAA</a>
                            <div class="page_content">

                            <div class="page_content_actions">
                                    <a class="edit" href="#">BBB</a>
                            </div>
                                test 1
                            </div>
                        </div>
                    </li>

</ul>

ul#menu_pages li{
    margin-bottom: 10px;
}

    ul#menu_pages li a:link,ul#menu_pages li a:active,ul#menu_pages li a:visited{
        display:block;
        padding:5px;
        border:1px solid #C3D5DF;
        line-height:25px;
        font-size: 15px;
        font-weight: bold;
        color:#C3D5DF;

}

div.page_content_actions li a.edit:link, div.page_content_actions a.edit:active, div.page_content_actions a.edit:visited {
                    background: url('/site_images/page_edit.png') left no-repeat;
                    display:block;
                    padding:0;
                    border:0;
                    font-size: 11px;
                    font-weight: bold;
                    color:#fff;

                }

the .edit class should get its own rules set (inside .page_content_actions) but instead from .edit is just getting the background, the rest is coming from the general #menu_pages li a. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

壹場煙雨 2024-10-12 16:49:03

CSS 尝试选择元素的最具体定义,

例如

1.

div span {
    background: yellow
}

2.

div div span {
    background: red
}

(2) 具有优先级,因为它更具体

CSS tries to pick most specific definition of an element

e.g

1.

div span {
    background: yellow
}

2.

div div span {
    background: red
}

(2) has priority because its more specific

倾`听者〃 2024-10-12 16:49:03

您可以使用 !important 来防止覆盖样式

示例:

<p class="foo bar">hello</p>
<style>
.foo
{
   font-size:20px;
}
.bar
{
   font-size: 5px !important;
}
</style>

在上面的示例中,将应用 important 版本。

you can use the !important to prevent overriding of styles

Example:

<p class="foo bar">hello</p>
<style>
.foo
{
   font-size:20px;
}
.bar
{
   font-size: 5px !important;
}
</style>

in the above example the important version would be applied.

泅渡 2024-10-12 16:49:03

CSS 遵循特异性规则,这意味着如果您有两个可能同时适用的类,则会对它们进行权衡并应用更具体的一个。请参阅这篇优秀文章以获得更深入的解释:

http://htmldog.com/guides/cssadvanced/特异性/

在您的情况下,#menu_pages li a 的特异性为 102(id 选择器为 100,2 个 html 元素选择器为 1+1),而 a.edit< /code> 的特异性仅为 11(类选择器为 10,html 选择器为 1)。要么降低第一个的特异性,要么增加第二个的特异性,以达到您想要的效果。

此外,您可以在任何想要覆盖具有更高特异性的规则的规则末尾使用 !important。请注意,这必须单独应用于每种样式。

CSS follows specificity rules, which means that if you have two classes that might both apply, they are weighed out and the more specific one is applied. See this excellent article for a more in-depth explanation:

http://htmldog.com/guides/cssadvanced/specificity/

In your case, #menu_pages li a has a specificity of 102 (100 for an id selector and 1+1 for 2 html element selectors) while a.edit has a specificity of just 11 (10 for a class selector and 1 for an html selector). Either reduce the specificity of the first or increase the specificity of the second to achieve your desired effect.

Additionally, you could use !important at the end of any rule that you want to override another rule with higher specificity. Note that this must be applied to each style individually.

憧憬巴黎街头的黎明 2024-10-12 16:49:03

因为更具体,或者有一个更长的选择器 (#menu_pages li a) 会覆盖简单的 .edit 选择器。

Because being more specific, or having a longer selector (#menu_pages li a) overides the simple .edit one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文