CSS Flex溢出和中心

时间:2017-09-05 16:15:34

标签: html css flexbox

我正在尝试水平对齐某些元素。当元素很少时,我希望它们处于中心位置。这里有很多,我想要一个水平滚动。

但是有一个问题,第一和第二个元素都被隐藏了。

.container {
    display: flex;
    flex-direction: row;
    justify-content: center ;
    overflow: scroll;
}

item {
      width: 440px ;
      height: 240px;
      flex-shrink: 0;
}

示例:

.projets {
  display: flex;
  flex-direction: row;
  justify-content: center;
  overflow: scroll;
}

.projets .projet_outter {
  width: 440px;
  height: 240px;
  flex-shrink: 0;
}

.projets .projet {
  border-radius: 10px;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.15);
  background-color: white;
  width: 400px;
  height: 200px;
  margin: 20px auto;
}

.projets .projet .num {
  margin: 0px auto;
  background-color: #005FB9;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  line-height: 40px;
  color: white;
  vertical-align: middle;
  text-align: center;
  transform: translateY(-50%);
}
<div class="projets">
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        1
      </div>
      projet aez
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        2
      </div>
      projet
    </div>
  </div>

  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        3
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        4
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        5
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        6
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        7
      </div>
      projet
    </div>
  </div>

</div>

https://codepen.io/alexandrepetrillo/pen/prBpOQ

3 个答案:

答案 0 :(得分:2)

你需要通过伪来解决justify-content:center;何时显示:

删除:justify-content:center;

并添加

&::before , 
    &::after {
      content:'';
      flex:1;
    }

&#13;
&#13;
.projets {
  display: flex;
  flex-direction: row;
  overflow: scroll;
}

.projets::before,
.projets::after {
  content: '';
  flex: 1;
}

.projets .projet_outter {
  width: 440px;
  height: 240px;
  flex-shrink: 0;
}

.projets .projet {
  border-radius: 10px;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.15);
  background-color: white;
  width: 400px;
  height: 200px;
  margin: 20px auto;
}

.projets .projet .num {
  margin: 0px auto;
  background-color: #005FB9;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  line-height: 40px;
  color: white;
  vertical-align: middle;
  text-align: center;
  transform: translateY(-50%);
}
&#13;
<div class="projets">

  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        1
      </div>
      projet aez
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        2
      </div>
      projet
    </div>
  </div>
</div>
<hr/>

<div class="projets">

  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        1
      </div>
      projet aez
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        2
      </div>
      projet
    </div>
  </div>

  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        3
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        4
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        5
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        6
      </div>
      projet
    </div>
  </div>
  <div class="projet_outter">
    <div class="projet">
      <div class="num">
        7
      </div>
      projet
    </div>
  </div>

</div>
&#13;
&#13;
&#13;

https://codepen.io/anon/pen/EvJQaV

答案 1 :(得分:0)

1)删除justify-content:center;

2)添加。projets .projet_outter{margin: 0 auto;}

答案 2 :(得分:0)

您可以使用

display: flex;
flex-direction: row;
justify-content: space-around;
overflow: auto;

P.S:使用Flex(http://flexbox.help/

进行游戏和学习的网站