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

10-12 00:21
查看更多