收缩容器仅适合可见物品

时间:2016-08-25 19:38:43

标签: html css

是否有基于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;
&#13;
&#13;

Codepen Link

1 个答案:

答案 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>