如果需要,Flexbox带有包装和垂直滚动条

时间:2017-10-02 09:30:38

标签: html css css3 flexbox overflow

我正在尝试为从左到右包装其项目的面板编写css代码,如果有很多项目,则应在flex面板中显示垂直滚动条以查看所有项目。 / p>

我正在添加属性 overflow-y:auto; ,但它对我的布局没有影响。

.main-card-flex-container {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
  overflow-y: auto;
}

.card-flex-container {
  -ms-box-orient: horizontal;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
  display: flex;
}

.wrap {
  -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
  max-height: 100%;
}

.card {
  -webkit-order: 0;
  -moz-order: 0;
  -ms-order: 0;
  order: 0;
  -webkit-flex: 0 0 auto;
  -moz-flex: 0 0 auto;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  -webkit-align-self: stretch;
  -moz-align-self: stretch;
  -ms-align-self: stretch;
  align-self: stretch;
}

.card-body {
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
  background-color: #001117;
  color: #ffffff;
  border-radius: 0;
  height: 150px;
  width: 250px;
  position: relative;
  margin-right: 10px;
  margin-bottom: 10px;
}

**HTML**
<div class="main-card-flex-container">
  <div class="card-flex-container wrap">
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item1</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item2</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item3</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item4</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item5</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item6</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item7</span>
      </div>
    </div>
  </div>
</div>

任何想法?

1 个答案:

答案 0 :(得分:3)

height: 100vh代替min-height添加到main-card-flex-container,并可能将body边距重置为零 - 请参阅下面的演示:

body {
  margin: 0; /* ADDED */
}

.main-card-flex-container {
  display: flex;
  /*min-height: 100vh;*/
  height: 100vh; /* ADDED */
  flex-direction: column;
  overflow-y: auto;
}

.card-flex-container {
  -ms-box-orient: horizontal;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
  display: flex;
}

.wrap {
  -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
  max-height: 100%;
}

.card {
  -webkit-order: 0;
  -moz-order: 0;
  -ms-order: 0;
  order: 0;
  -webkit-flex: 0 0 auto;
  -moz-flex: 0 0 auto;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  -webkit-align-self: stretch;
  -moz-align-self: stretch;
  -ms-align-self: stretch;
  align-self: stretch;
}

.card-body {
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
  background-color: #001117;
  color: #ffffff;
  border-radius: 0;
  height: 150px;
  width: 250px;
  position: relative;
  margin-right: 10px;
  margin-bottom: 10px;
}

**HTML**
<div class="main-card-flex-container">
  <div class="card-flex-container wrap">
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item1</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item2</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item3</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item4</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item5</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item6</span>
      </div>
    </div>
    <div class="card">
      <div class="card-body">
        <i class="remove-icon"></i>
        <span class="icon"></span>
        <span class="text">Item7</span>
      </div>
    </div>
  </div>
</div>