是否有基于CSS的纯解决方案来缩小容器(在本例中为UL)以仅适合可见(而非溢出)的项目?
右边的输出是我想要的(通过将display:none
设置为溢出项来实现):
.container {
background: yellow;
height: 50px;
width: 240px;
text-align: center;
float: left;
margin-left: 50px;
}
.wrapper {
display: inline-block;
background: red;
}
.foo {
display: table-cell;
vertical-align: top;
}
ul {
display: table-cell;
height: 50px;
margin: 0;
padding: 0;
background: green;
}
li {
list-style: none;
width: 50px;
height: 50px;
border: 1px solid white;
float: left;
margin-right: 5px;
background: blue;
}

<div class="container">
<div class="wrapper">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="foo">Foo</div>
</div>
</div>
<div class="container">
<div class="wrapper">
<ul>
<li></li>
<li></li>
<li></li>
<li style="display:none;"></li>
</ul>
<div class="foo">Foo</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
将overflow: hidden
添加到.container
。
.container {
background: yellow;
height: 50px;
width: 240px;
text-align: center;
float: left;
margin-left: 50px;
overflow: hidden; /* NEW */
}
.wrapper {
display: inline-block;
background: red;
}
.foo {
display: table-cell;
vertical-align: top;
}
ul {
display: table-cell;
height: 50px;
margin: 0;
padding: 0;
background: green;
}
li {
list-style: none;
width: 50px;
height: 50px;
border: 1px solid white;
float: left;
margin-right: 5px;
background: blue;
}
<div class="container">
<div class="wrapper">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="foo">Foo</div>
</div>
</div>