Flexbox始终保持在外部容器的100%

时间:2017-08-15 17:09:48

标签: html css html5 css3 flexbox

我在flexbox中有5列div。问题是,随着屏幕变小,flexbox开始走出白色容器。如果我希望所有列继续变小,该怎么办?为什么flexbox决定在某个点开始移动到外部容器之外,而不是继续变小?我如何改变它以使它们变小?

JSFiddle



.container {
  display: block;
  background-color: #fff;
  padding-left: 30px;
  padding-right: 30px;
}

.row {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-flow: row nowrap;
  flex-flow: row nowrap;
  margin-left: -30px;
  margin-right: -30px;
  padding: 15px 0 60px;
}

.cat {
  border: 1px solid black;
  padding-left: 30px;
  padding-right: 30px;
}

<div class="container">
  <div class="row">
    <div class="cat">
      Hello
    </div>
    <div class="cat">
      This is a group of flex columns
    </div>
    <div class="cat">
      Isn't that interesting?
    </div>
    <div class="cat">
      This is a group of flex columns
    </div>
    <div class="cat">
      Isn't that interesting?
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

弹性项目的默认min-widthauto,这意味着它不会缩小到低于其内容。

如果您只是将min-width: 0;添加到cat规则中,它们将缩小以适应其容器,而不是在其外部生长。

根据您可能想要添加overflow: hidden的文字,以便文字不会溢出。

注意,overflow: hiddenmin-width具有相同的效果,因此如果您愿意,您只能使用

.container {
  display: block;
  background-color: #fff;
  padding-left: 30px;
  padding-right: 30px;
}

.row {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-flow: row nowrap;
  flex-flow: row nowrap;
  margin-left: -30px;
  margin-right: -30px;
  padding: 15px 0 60px;
}

.cat {
  border: 1px solid black;
  padding-left: 30px;
  padding-right: 30px;
  min-width: 0;
}
<div class="container">
  <div class="row">
    <div class="cat">
      Hello
    </div>
    <div class="cat">
      This is a group of flex columns
    </div>
    <div class="cat">
      Isn't that interesting?
    </div>
    <div class="cat">
      This is a group of flex columns
    </div>
    <div class="cat">
      Isn't that interesting?
    </div>
  </div>
</div>