How to Specify an Element After Which to Wrap in CSS Flexbox

CSS: How can I ensure flex items wrap to the next line--after the first item--at a certain screen width?

Use the media query to apply flex-wrap:wrap on the flex container and flex:0 0 100% on the first child.

This way you don't need additional HTML markup, and no need to change anything on your code but the media query.

@media (max-width: 800px) {
.cart-cb{
flex-wrap:wrap;
}
.cart-cb div{
flex: 0 0 100%;
text-align:right;
}
}

https://jsfiddle.net/378b4yLy/

How to specify when element should wrap with flexbox?

You could use min-width on the blocks. This means they will fully stretch, but when the screen size limits them to being 200px in this example, that breakpoint will lead them to wrap, and their width will never go below 200px.

Another option is to just apply flex-wrap: wrap; on that specific breakpoint you want with a media-query.

For further control, you could also look into flex-basis: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis

EDIT: Responsive image

Genereally it's good to include this line of code on most images: max-width: 100%; height: auto; as this will make images auto-responsive. max-width: 100%; forces the image to never overflow from its container, and height: auto; adjusts the images height so its aspect ratio is correct. Try dragging the screen size and you will see its effect :)

.flex-row {
display: flex;
flex-wrap: wrap;
}

.block1,
.block2 {
min-width: 200px;
}

.block2 img {
max-width: 100%;
height: auto;
}
<div class='flex-row'>

<div class="block1">

<h2>Some title</h2>
<p>Some text</p>
</div>


<div class="block2">

<img src="http://via.placeholder.com/350x350" />

</div>


</div>

Can you specify the order of wrapped elements in CSS flex?

Check this pen out:

http://codepen.io/chriscoyier/pen/YPzyYa

What the pen does --
First gives each element a default class with an order attribute:

.icon {
order: 0 !important;
}
.username {
order: 1 !important;
width: 100%;
margin: 15px;
}
.search {
order: 2 !important;
width: 100%;
}

Then it gives a class to each each flexbox a specific ordering attribute:

.bar-2 {
.username {
order: 2;
}
}
.icon-3 {
order: 3;
}

Apply the sorting classes to your case and you should be good to go.

Changing the order of elements and wrap after specific element with flexbox

You can use flexbox order property

* {  box-sizing: border-box;}
.container { display: flex; flex-wrap: wrap;}
.container div { padding: 10px; border: 5px solid #fff; background: gray; text-align: center;}
.first_container { width: 70%; order: 1;}
.second_container { width: 100%; order: 3;}
.third_container { width: 30%; order: 2;}
<div class="container">  <div class="first_container">1</div>  <div class="second_container">2</div>  <div class="third_container">3</div></div>

How to specify line breaks in a multi-line flexbox layout?

The simplest and most reliable solution is inserting flex items at the right places. If they are wide enough (width: 100%), they will force a line break.

.container {
background: tomato;
display: flex;
flex-flow: row wrap;
align-content: space-between;
justify-content: space-between;
}
.item {
width: 100px;
background: gold;
height: 100px;
border: 1px solid black;
font-size: 30px;
line-height: 100px;
text-align: center;
margin: 10px
}
.item:nth-child(4n - 1) {
background: silver;
}
.line-break {
width: 100%;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="line-break"></div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="line-break"></div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="line-break"></div>
<div class="item">10</div>
</div>

Flexbox - How to split row 50/50 between two elements and wrap if width falls below min-width?

Just change width: 45% to flex-grow: 1, the flex items will try to take up the whole row. flex-basis controls how large they are in relation to eachother, by default they will attempt to stay the same size. Currently they wrap at 332px because of the 16px of padding.

mat-card {
margin: 10px;
min-width: 300px;
flex-grow: 1;
}


Related Topics



Leave a reply



Submit