gpt4 book ai didi

html - 收缩容器以仅适合可见元素

转载 作者:行者123 更新时间:2023-11-28 16:06:19 25 4
gpt4 key购买 nike

是否有一种纯基于 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>

Codepen Link

最佳答案

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>

关于html - 收缩容器以仅适合可见元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39153470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com