Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Flex overflow and center [duplicate]

Tags:

html

css

flexbox

I'm trying to align some elements horizontally. When there is few elements, I want they are on center. This is where there is many, I want a horizontal scroll.

But there is a problem, the first and second elements are hidden.

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

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

Example:

.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

like image 842
Alexandre Petrillo Avatar asked Sep 05 '17 16:09

Alexandre Petrillo


2 Answers

You need a work around via pseudos to simulate justify-content:center; when it should show:

remove : justify-content:center;

and add

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

.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%);
}
<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>

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

like image 70
G-Cyrillus Avatar answered Oct 31 '22 01:10

G-Cyrillus


You can use

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

P.S: Awesome website to play and learn with Flex (http://flexbox.help/)

like image 24
rootkill Avatar answered Oct 31 '22 02:10

rootkill