【发布时间】:2018-01-18 16:04:43
【问题描述】:
【问题讨论】:
-
外部 div 上没有宽度/高度,但 display:inline-block 代替...完成。
-
谢谢!我将学习和研究这个 inline-block 选项。
【问题讨论】:
外部容器不需要width 和height 属性。如果是这种情况,如果子元素 (.div2) 具有 margin,则外部容器 (.div1) 将展开。请参阅代码示例以更好地理解:)
.div1 {
border: 4px solid #111;
width: auto; /* remove width */
height: auto; /* remove height */
display: inline-block; /* make it more flexible */
}
.div2 {
width: 100px;
height: 100px;
background: cyan;
}
.div2.with-margin {
margin: 20px;
}
<div class="div1">
<div class="div2"></div>
</div>
<div class="div1">
<div class="div2 with-margin"></div>
</div>
【讨论】: