我有一个内容div,内容可变,水平。 我希望此内容div的宽度根据其内容的长度自动调整大小。例如,我希望能够删除任意的" 20%"值,并相应地具有div大小。目前从宽度中删除20%的值迫使其调整为100%的容器。我错过了什么CSS?
.wrapper {
border: 2px solid green;
}
.content {
margin: 0 auto;
text-align: center;
border: 2px solid orange;
width: 20%;
}

<div class="wrapper">
<div class="content">
<span>One</span>
<span>Two</span>
<span>Three</span>
<span>Four</span>
</div>
</div>
&#13;
答案 0 :(得分:1)
将text-align:center
放在包装器上,并将内容div的显示设置为内联或内联块:
.wrapper {
border: 2px solid green;
text-align:center;
}
.content {
margin: 0 auto;
text-align: center;
border: 2px solid orange;
display: inline;
}
&#13;
<div class="wrapper">
<div class="content">
<span>One</span>
<span>Two</span>
<span>Three</span>
<span>Four</span>
</div>
</div>
&#13;
答案 1 :(得分:1)
如果您想要液体调整大小的行为,我建议使用flexbox:
.wrapper {
border: 2px solid green;
display: flex;
justify-content: center;
}
.content {
margin: 0 auto;
text-align: center;
border: 2px solid orange;
/*width: 20%;*/
}
&#13;
<div class="wrapper">
<div class="content">
<span>One</span>
<span>Two</span>
<span>Three</span>
<span>Four</span>
</div>
</div>
&#13;
直列:
flexbox: