Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flex item full width after it's been wrapped without using media queries

Tags:

Using flexbox here. Is it possible to make overlapping element full width after it has been pushed down without media queries?

See attached plunker: http://plnkr.co/edit/mxACfJ2VR5yNgP9j8WJh?p=preview Resize preview window to less than 425px width. Third element get's pushed down and then I want to make it full width. When it is in the same line as other 2 elements it must not grow.

css:

.flexContainer {     display:flex;     flex-wrap:wrap;     border: 1px solid;   }   .img {     width:110px;     height:110px;     background:red;   }   .flexItem {     flex-basis:200px;     flex-grow:1;     background:#ff1;   }   .wrapItem {     flex-basis:100px;     background:#aab;   } 

html:

<div class="flexContainer">   <div class="img">fixed size img here</div>   <div class="flexItem">This flexes</div>   <div class="wrapItem">This wraps</div> </div> 
like image 526
Jānis Avatar asked Oct 16 '15 15:10

Jānis


People also ask

How do you make flex items take up full width?

When you want a flex item to occupy an entire row, set it to width: 100% or flex-basis: 100% , and enable wrap on the container. The item now consumes all available space.

How do I fill the remaining space in flex element?

You can use the flex-grow property to force an item to fill the remaining space on the main axis of a flex container. The item will expand as much as possible and occupy the free area.

What flexbox property do you use to define how Flex items will be wrapped?

The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked.

Does flexbox need media query?

You can combine flexbox with media queries to create a layout that responds to different sized screens. Media queries are commonly used in responsive designs in order to display a different layout to different devices, depending on their screen size.


2 Answers

You can not do that. But you can get pretty close.

Just set the grow on the flex item to a huge value (9999) and grow on the wrap item to 1

 .flexContainer {      display:flex;      flex-wrap:wrap;      border: 1px solid;    }    .img {      width:110px;      height:110px;      background:red;    }    .flexItem {      flex-basis:200px;      flex-grow:9999;      background:#ff1;    }    .wrapItem {      flex-basis:100px;      flex-grow:1;      background:#aab;    }
  <body>      <div class="flexContainer">        <div class="img">fixed size img here</div>        <div class="flexItem">This flexes</div>        <div class="wrapItem">This wraps</div>      </div>    </body>
like image 175
vals Avatar answered Oct 07 '22 12:10

vals


You haven't told that element that it is allowed to grow by adding flex-grow:1

.flexContainer {      display:flex;      flex-wrap:wrap;      border: 1px solid;  }  .img {      width:110px;      height:110px;      background:red;  }  .flexItem {      flex-basis:200px;      flex-grow:1;      background:#ff1;  }  .wrapItem {      flex-grow:1;      flex-basis:100px;      background:#aab;  }
<div class="flexContainer">      <div class="img">fixed size img here</div>      <div class="flexItem">This flexes</div>      <div class="wrapItem">This wraps</div>  </div>

JSfiddle Demo

like image 35
Paulie_D Avatar answered Oct 07 '22 12:10

Paulie_D