fefe fefe - 5 months ago 34
CSS Question

align flexbox items vertically and keep same width

I have the following flexbox where I'm trying to align items vertically and keep the same width for each column.

let's say I have 10 items in a row but inside container should be visible just 4 items the rest are hidden by overflow. All this items are splitted to the same width in function of the width of parent container

It does not seems to work:



.container {
width: 100%;
}
.container .slider {
position: relative;
overflow: hidden;
margin: auto;
padding: auto;
}
.container .slider[data-columns="1"] ul {
width: 100%;
}
.container .slider[data-columns="1"] ul li {
width: 100%;
}
.container .slider[data-columns="2"] ul {
width: 200%;
}
.container .slider[data-columns="2"] ul li {
width: 50%;
}
.container .slider[data-columns="3"] ul {
width: 300%;
}
.container .slider[data-columns="3"] ul li {
width: 33.33333%;
}
.container .slider[data-columns="4"] ul {
width: 400%;
}
.container .slider[data-columns="4"] ul li {
width: 25%;
}
.container .slider[data-columns="5"] ul {
width: 500%;
}
.container .slider[data-columns="5"] ul li {
width: 20%;
}
.container .slider[data-columns="6"] ul {
width: 600%;
}
.container .slider[data-columns="6"] ul li {
width: 16.66667%;
}
.container .slider[data-columns="7"] ul {
width: 700%;
}
.container .slider[data-columns="7"] ul li {
width: 14.28571%;
}
.container .slider[data-columns="8"] ul {
width: 800%;
}
.container .slider[data-columns="8"] ul li {
width: 12.5%;
}
.container .slider[data-columns="9"] ul {
width: 900%;
}
.container .slider[data-columns="9"] ul li {
width: 11.11111%;
}
.container .slider[data-columns="10"] ul {
width: 1000%;
}
.container .slider[data-columns="10"] ul li {
width: 10%;
}
.container .slider ul {
margin: 0;
padding: 0;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.container .slider ul li {
height: 200px;
list-style: none;
}
.container .slider ul li:nth-child(odd) {
background-color: #34495e;
}

<div class="container">
<div class="slider" data-columns ="4">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>





What am I missing in my code?

Codepen demonstration here: http://s.codepen.io/deroccha/debug/GqvvEm

Answer

To align flex items vertically, switch the flex-direction on the container from row to column.

.container .slider ul {
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column; /* ADJUSTMENT */
    flex-wrap: nowrap;
}

OR, you could allow flex items to wrap when there isn't enough space on the line:

.container .slider ul {
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;    /* ADJUSTMENT */
}

Second answer based on revised question:

Disable shrinking on flex items:

An initial setting of a flex container is flex-shrink: 1. This means that flex items are allowed to shrink (preventing them from overflowing the container when flex-wrap is nowrap).

Add flex-shrink: 0 to disable the shrinking feature.

For more details, see "The flex-shrink factor" section in this answer: http://stackoverflow.com/a/34355447/3597276

.container .slider {
    overflow: hidden;
    margin: auto;
}

.container .slider[data-columns="4"] ul {
    width: 400%;
}

.container .slider[data-columns="4"] ul li {
    flex: 0 0 6.25%; /* don't grow, don't shrink, remain 6.25% width (25% / 4) */
}

.container .slider ul {
    margin: 0;
    padding: 0;
    display: flex;
}

.container .slider ul li {
    height: 200px;
    list-style: none;
    background-color: red;
}

.container .slider ul li:nth-child(odd) {
    background-color: blue;
}
<div class="container">
    <div class="slider" data-columns="4">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>