Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First-child full-width in Flexbox

Tags:

css

flexbox

How can I set the first-child of flexbox in full-width and all of the other childs set to flex:1(for split space)?

Like this:

enter image description here

like image 412
Sajad Avatar asked Jan 22 '17 09:01

Sajad


People also ask

What does Flexgrow 1 mean?

“For example, if all items have flex-grow set to 1, every child will set to an equal size inside the container. If you were to give one of the children a value of 2, that child would take up twice as much space as the others.”


2 Answers

You can set the :first-child to a width of 100%, and the rest of the childs :not(:first-child) to flex: 1.

To put them on multiple lines, use flex-wrap: wrap on the container:

.container {   display: flex;   justify-content: space-between;   flex-wrap: wrap;   background: #e2eaf4;   padding: 10px; }  .child {   display: inline-block;   font-family: "Open Sans", Arial;   font-size: 20px;   color: #FFF;   text-align: center;   background: #3794fe;   border-radius: 6px;   padding: 20px;   margin: 12px; }  .child:first-child {   width: 100%; }  .child:not(:first-child) {   flex: 1; }
<div class="container">   <div class="child">Child</div>   <div class="child">Child</div>   <div class="child">Child</div> </div>
like image 185
TheYaXxE Avatar answered Oct 04 '22 11:10

TheYaXxE


Add width: 100%; for your first item. And flex: 1; for others.

.flex {    display: flex;    flex-wrap: wrap;  }    .flex-item:not(:first-child) {    flex: 1;  }    .flex-item:nth-child(1) {    width: 100%;  }    /* Styles just for demo */  .flex-item {    background-color: #3794fe;    margin: 10px;    color: #fff;    padding: 20px;    border-radius: 5px;  }
<div class="flex">    <div class="flex-item">      Child    </div>    <div class="flex-item">      Child    </div>    <div class="flex-item">      Child    </div>  </div>
like image 39
Vadim Ovchinnikov Avatar answered Oct 04 '22 09:10

Vadim Ovchinnikov