Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

css - Is box sizing inherited if declared inside universal selector?

Does this get applied by every element?

EXAMPLE 'A':

*
{
    box-sizing: border-box;
}

I thought box-sizing is not inherited?

This is in regards to box sizing reset:

EXAMPLE 'B':

*, *::before, *::after
{
    box-sizing: inherit;
}

html
{
    box-sizing: border-box;
}

Will both examples have the same effect?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I thought box-sizing is not inherited?

No, it's not. You can check the specification and you will see that inherited is NO. In the example A, you are simply using the universal selector to select all the elements and applying box-sizing:border-box to them but it doesn't target pseudo element so they will not have box-sizing:border-box set by default.

Will both examples have the same effect?

No they won't. Even if we consider that it's the only CSS applied to your document, the example B will also target pseudo element to make them inherit the box-sizing and if we follow the parent-child structure they will logically get box-sizing:border-box since html is having box-sizing:border-box.

The other difference is that using inherit will consider the parent style unlike explicitly setting the value in the example A. In the example A, you have to change box-sizing for each element if you want to override the one set by your code while in the example B, changing box-sizing on the element OR any ancestor will override the code you are using.


A basic example to illustrate the difference.

Using A:

* {
  box-sizing: border-box;
}



/* my code*/
body {
  box-sizing: content-box;
}
.box {
  width:100px;
  height:100px;
  border:10px solid;
  padding:10px;
}

p {
  padding:5px;
  border:2px solid red;
}
<div class="box">
  <p></p>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...