为什么 2 个内联块 div 的组合宽度等于父级的宽度并排放置?
why dont 2 inline-block divs with combined width equalling that of the parent fit side by side?
我想不通。
我可以通过浮动让它工作,但我想了解为什么会这样。
代码:
.container {
width: 1000px;
height: 400px;
background-color: purple;
position: relative;
}
.item {
height: 100px;
display: inline-block;
}
.item.left {
width: 70%;
background-color: green;
}
.item.right {
width: 30%;
background-color: orange;
}
<div class="container">
<div class="item left"></div>
<div class="item right"></div>
</div>
如果您的意思是为什么 不 它们并排放置,那是因为内联元素对代码中的白色 space 敏感。删除 div 之间的 space,它们并排在同一行上:
.container {
width: 1000px;
height: 400px;
background-color: purple;
position: relative;
}
.item {
height: 100px;
display: inline-block;
}
.item.left {
width: 70%;
background-color: green;
}
.item.right {
width: 30%;
background-color: orange;
}
<div class="container">
<div class="item left"></div><div class="item right"></div>
</div>
我想不通。
我可以通过浮动让它工作,但我想了解为什么会这样。
代码:
.container {
width: 1000px;
height: 400px;
background-color: purple;
position: relative;
}
.item {
height: 100px;
display: inline-block;
}
.item.left {
width: 70%;
background-color: green;
}
.item.right {
width: 30%;
background-color: orange;
}
<div class="container">
<div class="item left"></div>
<div class="item right"></div>
</div>
如果您的意思是为什么 不 它们并排放置,那是因为内联元素对代码中的白色 space 敏感。删除 div 之间的 space,它们并排在同一行上:
.container {
width: 1000px;
height: 400px;
background-color: purple;
position: relative;
}
.item {
height: 100px;
display: inline-block;
}
.item.left {
width: 70%;
background-color: green;
}
.item.right {
width: 30%;
background-color: orange;
}
<div class="container">
<div class="item left"></div><div class="item right"></div>
</div>