【发布时间】:2016-05-02 00:57:58
【问题描述】:
我没有得到auto 的值。如果应用于height,它将采用孩子的高度,但如果应用于width,它将采用父母的宽度。
auto 值本身没有 MDN 帖子,Google 产生“100% VS auto”命中而不是“width:auto VS height:auto”命中。
对于我目前的需求,我希望将一个元素扩展到其子元素的宽度,但总的来说,我想知道auto 的处理方式。
.divXS {
width: 100px;
height: 100px;
background: green;
}
.divXXS {
width: 50px;
height: 50px;
background: yellow;
}
.divSM {
width: 200px;
height: 200px;
}
#Father {
background: blue;
border: 3px solid #20295E;
}
#Mother {
background: pink;
border: 3px solid #DB6DBE;
}
#Daughter {
width: 100%;
height: 100%;
}
#Son {
width: auto;
height: auto;
}
<div class="divSM" id="Mother">
<div class="divXS" id="Daughter">
<div class="divXXS" id="grandDaughter"></div>
</div>
</div>
<div class="divSM" id="Father">
<div class="divXS" id="Son">
<div class="divXXS" id="grandSon"></div>
</div>
</div>
【问题讨论】:
-
"没有关于 auto 属性本身的 MDN 帖子" 那是因为 auto 不是属性,它是一个值,它不是像 initial 或 inherit 那样的 CSS 范围的关键字,它的行为不同对于不同的属性 - for a reason.
-
“我想要一个元素扩展到其子元素的宽度”您可以通过将父元素设为
inline-block来实现这一点,然后它将适应其子元素。