CSS precedence based on parent div

拈花ヽ惹草 提交于 2020-03-22 03:47:54

问题


If you view the following code in a browser the link appears red. I would expect it to be green because the secondary div is nested inside the primary div. It appears the color is determined by the order of the elements in the css file. If I move .secondary after .primary the link is green.

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        .secondary a {
            color: green;
        }

        .primary a {
            color: red;
        }
    </style>
</head>
<body>
    <div class="primary">
        <div class="secondary">
            <a href="http://www.google.com">test</a>
        </div>
    </div>
</body>
</html>

Other than changing the order in the file how can I make the link respect the color from its parent div?

EDIT: This is a very simplified example. In practice there could be many other div tags between the primary and secondary classes.

Link to codepen


回答1:


The issue is that the two selector have the same specificity. The only thing that CSS knows to do with selectors of the same specificity is to choose the most recent one

Thus, you need to make the specificity of the child more than the firsts, one way is to put

.primary .secondary a {
    color:green;
}

Another way would be to put the element type in addition to the class

This is the reason why it is proper formatting to structure your CSS as the page it is laid out in the HTML, with parents coming before children

For more information as to how specificity is determined, check here




回答2:


If you used .primary > a as the selector, then it would only match anchors that are immediate children of the class.

Of course then you couldn't do something like:

<div class="primary">
  <div class="wrapper">
    <a href="#">test</a>
  </div>
</div>



回答3:


Interesting question. In fact the second selector overrides the first one because they both have the same specificity value:.

In terms of value:
Inline Style > IDs > Classes > Attributes, and Pseudo-classes > Element Types and Pseudo-elements.

You can use a selector with higher specificity value to override the CSS declaration.

For instance:

div.secondary a { /* Specificity value = 12 */
    color: green;
}

Or:

.primary .secondary a {  /* Specificity value = 21 */
    color: green;
}

Here is an online tool to calculate the CSS Specificity.




回答4:


If you want the selector to respect the direct parent, you need to use the direct descendant selector >:

.primary > a {
    color: red;
}

Which translates to anchors only with a direct parent with class .primary




回答5:


Have a look at this great article about css selector specificity

Since your two rules are equivalent the one that comes last takes precidence. In order to prevent that you can make them more specific using the following:

div.secondary a {
    color: green;
}

-

.primary .secondary a {
    color:green;
}

etc




回答6:


I just come to know !important is not a good solution. So this is the next solution that works.

.primary .secondary a {
    color:green;
}


来源:https://stackoverflow.com/questions/22259735/css-precedence-based-on-parent-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!