Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can flexbox detect when a flex item wraps?

Tags:

I am hoping to get rid of media queries and just use flexbox to solve my problem, but I'm not sure if this is possible. As the window gets smaller, I want the columns to shrink until they hit their minimum width. Once they hit that, the middle column will jump down for the wrap.

I think my question boils down to this- can flexbox detect when it wraps? If so, can I prompt it to change the order of the items on wrap with strictly CSS?

Here is a codepen of what I"m trying to accomplish using media queries.

.wrapper {    display: flex;    justify-content: center;    margin: 5px;    flex-wrap: wrap;  }    .red {    background-color: red;    height: 240px;    margin: 5px;    width: 500px;    order: 0;  }    .yellow {    background-color: yellow;    margin: 5px;    height: 240px;    min-width: 240px;    max-width: 400px;    flex: 1;    order: 1;  }    .blue {    background-color: blue;    height: 240px;    margin: 5px;    min-width: 350px;    max-width: 400px;    flex: 1;    order: 2;  }    @media (max-width:1130px) {    .yellow {      order: 4;    }  }
<div class="wrapper">    <div class="red"></div>    <div class="yellow"></div>    <div class="blue"></div>  </div>
like image 799
Lexie Avatar asked Apr 14 '17 23:04

Lexie


People also ask

How do you know if an element is wrapped?

@ChrisCashwell Calculate width of each element, whilst looping, accumulate element widths, and when accumulated width exceeds the container width, you know that element has wrapped.

What happens with flex-wrap?

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.

Which property specifies whether Flex items should wrap or not?

The flex-wrap property specifies whether flex items should wrap or not.

Is flex-wrap responsive?

The flex-wrap property is a quick way to make parent elements more responsive on various screen sizes. As with flexbox in general, it simplifies page layouts so you don't have to manually set breakpoints or manage the page overflow yourself.


1 Answers

Can flexbox detect when a flex item wraps?

No.

In CSS, once the browser renders the page on the initial cascade, it doesn't reflow the document when an element wraps. As a result, parent elements don't know when their children wrap.

That's why containers don't shrink-to-fit their content after wrapping.

That's why you need media queries or JavaScript.

Here are some more details:

  • Make container shrink-to-fit child elements as they wrap
  • How to center a flex container but left-align flex items
like image 54
Michael Benjamin Avatar answered Oct 04 '22 13:10

Michael Benjamin