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 item wrap to a new line
If you look at this great answer you'll notice that the only cross-browser way (without 2 line break limit) is inserting 100%
-width empty blocks ("line-breaks"). So for similar markup this will look like
.flex { display: flex; flex-wrap: wrap; border: 2px solid red;}
.item { width: 50px; height: 50px; margin: 5px; border: 2px solid blue;}
.line-break { width: 100%;}
<div class="flex"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="line-break"></div> <div class="item"></div> <div class="item"></div> <div class="line-break"></div> <div class="item"></div> <div class="line-break"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div></div>
How to break line in a flex layout?
You can insert a wide pseudo-element at the right position:
.flex-container { display: flex; flex-wrap: wrap;}.flex-container::after { content: ''; width: 100%;}.flex-item:last-child { /* or `:nth-child(n + 4)` */ order: 1;}
<div class="flex-container"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> <div class="flex-item">4</div></div>
break line in a flex layout responsive
Add the following in your media query:
.header-title {
flex-wrap: wrap;
margin: 0 auto;
max-width: 750px;
}
& .header-title h1,
.header-title h2 {
width: 100%;
}
Related Topics
How to Wrap Text Around an Image Using Html/Css
Multiple Submit Buttons in an HTML Form
Ie9 Float With Overflow:Hidden and Table Width 100% Not Displaying Properly
Expanding a Parent ≪Div≫ to the Height of Its Children
Putting Images With Options in a Dropdown List
Twitter Bootstrap 3, Vertically Center Content
Make Iframe to Fit 100% of Container'S Remaining Height
Putting ≪Div≫ Inside ≪P≫ Is Adding an Extra ≪P≫
Html5 - Canvas Element - Multiple Layers
Limit Number of Characters Allowed in Form Input Text Field
How to Fallback to Local Stylesheet (Not Script) If Cdn Fails
Uri Starting With Two Slashes ... How Do They Behave
Why Is a Div With "Display: Table-Cell;" Not Affected by Margin
Masonry-Style Layout Only With Css
How to Use Google Chrome 11'S Upload Folder Feature in My Own Code