我的想法是给出一个自适应div宽度。当我只有一个项目时,宽度应该与容器div中的项目相同。
有关如何执行此操作的任何建议吗?
示例:http://codepen.io/tomasfrancisco/pen/PNvLLw
HTML:
<div id="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div id="container">
<div class="item"></div>
</div>
CSS:
#container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
max-width: 200px;
padding: 10px;
margin: 20px;
background-color: blue;
}
#container .item {
width: 80px;
height: 50px;
background-color: red;
margin: 10px;
}
答案 0 :(得分:22)
您可以使用display: inline-flex
#container {
display: inline-flex;
flex-direction: row;
flex-wrap: wrap;
max-width: 200px;
padding: 10px;
margin: 20px;
background-color: blue;
}
#container .item {
width: 80px;
height: 50px;
background-color: red;
margin: 10px;
}
&#13;
<div id="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div id="container">
<div class="item"></div>
</div>
&#13;