你想要的可以通过组合来完成inline-block
and min-width:100%
。块元素的宽度是根据其父元素(包含块)定义的,而inline-block
其宽度由其内容定义。
添加min-width:100%
将使其表现为块元素。在这种情况下,这不是强制性的,因为您已经有很多内容,因此您一定会覆盖所有宽度:
body {
margin: 4em;
}
.demo {
background-color: #BFC;
box-sizing: border-box;
margin: 0;
padding: 1em;
position: relative;
display:inline-block;
min-width:100%;
}
.demo p:first-child {
margin-top: 0;
}
.other-stuff {
align-items: center;
display: flex;
}
button {
margin-right: 1em;
}
.square {
display: inline-block;
background-color: #699;
height: 80px;
margin-right: 1em;
min-width: 80px;
width: 80px;
}
.circle {
border-radius: 50px;
display: inline-block;
background-color: #969;
height: 100px;
margin-right: 1em;
min-width: 100px;
width: 100px;
}
<div class="demo">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
减少顶部的文字并min-width:100%
将强制具有全宽度行为。
Run the snippet on full page
body {
margin: 4em;
}
.demo {
background-color: #BFC;
box-sizing: border-box;
margin: 0;
padding: 1em;
position: relative;
display:inline-block;
}
.demo p:first-child {
margin-top: 0;
}
.other-stuff {
align-items: center;
display: flex;
}
button {
margin-right: 1em;
}
.square {
display: inline-block;
background-color: #699;
height: 80px;
margin-right: 1em;
min-width: 80px;
width: 80px;
}
.circle {
border-radius: 50px;
display: inline-block;
background-color: #969;
height: 100px;
margin-right: 1em;
min-width: 100px;
width: 100px;
}
<div class="demo">
<p>Lorem ipsum </p>
<div class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
<div class="demo" style="min-width:100%;">
<p>Lorem ipsum </p>
<div class="other-stuff">
<button>One button</button>
<div class="square"></div>
<button>Another button</button>
<div class="circle"></div>
<button>Don't click!</button>
</div>
</div>
From 规格 https://www.w3.org/TR/CSS21/visudet.html:
正常流程中的块级、非替换元素
其他属性的使用值必须满足以下约束:
'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
Note how the content play no role in defining the width.
“内联块”,正常流程中的非替换元素
如果“width”为“auto”,则使用的值为收缩到适合宽度
收缩以适合宽度的计算类似于使用自动表格布局算法计算表格单元格的宽度。粗略:通过格式化计算首选宽度内容除了发生显式换行符之外,不会断行,并且还计算首选的最小宽度,例如,通过尝试所有可能的换行符。第三,找到可用宽度:在本例中,这是包含块的宽度减去使用的“margin-left”、“border-left-width”、“padding-left”、“padding-right”的值, 'border-right-width'、'margin-right' 以及任何相关滚动条的宽度
收缩到合适的宽度是:min(max(preferred minimum width, available width), preferred width)
Note how the content is used to define the width.
这同样适用于任何类型的显示值(grid
, flex
, table
等),诀窍是将其替换为inline-*
相等的 (inline-grid
, inline-flex
, inline-table
, etc)