CSS Specificity and Inheritance

后端 未结 3 625
独厮守ぢ
独厮守ぢ 2020-12-12 05:30

Given the following code:



        
相关标签:
3条回答
  • 2020-12-12 06:02

    The * takes precedence over the parent selector as you did for container. You should be able to reach your point adding to the p

    font-size: inherit;
    

    Or also this should work:

    .container * {
        font-size: inherit;
    }
    
    0 讨论(0)
  • 2020-12-12 06:08

    The .container rule doesn't match the p element. So specificity is irrelevant here. Inheritance and specificity are separate concepts and the only time they interact is when more specific/less specific rules contain declarations with inherit. That is not the case here.

    As far as the p element is concerned, only the * rule applies, and the * contains its own font-size declaration, and so the specified font size follows that declaration.

    If the * rule didn't have its own font-size declaration, then the p element would inherit from .container.

    If you want descendants of .container to take after its font size, you will need an additional .container * rule. Be very careful with the inherit keyword when it comes to relative values, though — you probably meant to keep all descendants the same size, so 1em or 100% is more appropriate here than inherit:

    * {
      font-size: 18px;
    }
    
    .container {
      font-size: 50%;
    }
    
    .container * {
      font-size: 1em;
    }
    <div class="container">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate explicabo fugiat laborum minus ullam? Amet delectus facilis id quam temporibus.
      </p>
    </div>

    0 讨论(0)
  • 2020-12-12 06:14

    The star * is the universal selector for CSS. It matches a single element of any type. Please see this link, has a good explanation (why) is the CSS star selector considered harmful?

    0 讨论(0)
提交回复
热议问题