GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing developer.mozilla.org — flex-wrap CSS property - CSS | MDN
|
| Initial value | nowrap |
|---|---|
| Applies to | flex containers |
| Inherited | no |
| Computed value | as specified |
| Animation type | discrete |
flex-wrap =
nowrap |
[ wrap | wrap-reverse ] || balance
<h4>This is an example for flex-wrap:wrap</h4>
<div class="content">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
<h4>This is an example for flex-wrap:nowrap</h4>
<div class="content1">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
<h4>This is an example for flex-wrap:wrap-reverse</h4>
<div class="content2">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
/* Common Styles */
.content,
.content1,
.content2 {
color: white;
font: 100 24px/100px sans-serif;
height: 150px;
width: 897px;
text-align: center;
}
.content div,
.content1 div,
.content2 div {
height: 50%;
width: 300px;
}
.red {
background: orangered;
}
.green {
background: yellowgreen;
}
.blue {
background: steelblue;
}
/* Flexbox Styles */
.content {
display: flex;
flex-wrap: wrap;
}
.content1 {
display: flex;
flex-wrap: nowrap;
}
.content2 {
display: flex;
flex-wrap: wrap-reverse;
}
balance keywordThis example demonstrates the effects of the balance keyword within the flex-wrap property and different flex-line-count values.
<form>
<div>
<input type="checkbox" id="reverse" name="reverse" />
<label for="reverse">Set <code>wrap-reverse</code>?</label>
</div>
<div>
<input type="checkbox" id="balance" name="balance" checked />
<label for="balance">Set <code>balance</code>?</label>
</div>
<div>
<label for="line-count"><code>flex-line-count</code></label>
<input
type="range"
id="line-count"
name="line-count"
min="1"
max="12"
value="3" />
<output>3</output>
</div>
</form>
<hr />
<p>Currently set: <code>flex-wrap: wrap; flex-line-count: 3;</code></p>
<hr />
We include a container <div> with a class of box, which has ten child <div>s.
<div class="box">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
<div>Eight</div>
<div>Nine</div>
<div>Ten</div>
</div>
We also include controls to enable changing features of the flex container, including:
flex-wrap between the wrap and wrap-reverse values.balance keyword on and off.flex-line-count value.We've hidden the HTML and JavaScript for brevity.
We apply display: flex to the .box to turn it into a flex container, then set some flex-wrap and flex-line-count values to make the flex children wrap evenly over a minimum of three flex lines. You can change the values in the live demo. We also set a flex value of 1 1 150px on the flex children so they will have a base width of 150px, with any remaining space distributed evenly between items on the same flex line.
.box {
display: flex;
flex-wrap: wrap balance;
flex-line-count: 3;
}
.box > * {
flex: 1 1 150px;
}
.box {
width: 100%;
border: 2px dotted rgb(96 139 168);
gap: 10px;
}
.box > * {
border: 2px solid rgb(96 139 168);
border-radius: 5px;
background-color: rgb(96 139 168 / 0.2);
}
* {
box-sizing: border-box;
}
body {
padding: 10px 30px;
}
@supports not (flex-wrap: balance) {
body::before {
content: "Your browser does not support flex-wrap: balance.";
background-color: wheat;
text-align: center;
padding: 1rem 0;
z-index: 1;
position: fixed;
inset: 40% 0 auto;
}
}
We've hidden the rest of the CSS for brevity.
const boxElem = document.querySelector(".box");
const outputElem = document.querySelector("output");
const pCodeElem = document.querySelector("p code");
const reverseInput = document.getElementById("reverse");
const balanceInput = document.getElementById("balance");
const lineCountInput = document.getElementById("line-count");
let wrapValue = "wrap";
let lineCountValue = "3";
function updateCurrentlySet() {
pCodeElem.textContent = `flex-wrap: ${wrapValue}; flex-line-count: ${lineCountValue};`;
}
function setFlexWrap() {
wrapValue = "";
if (reverseInput.checked) {
wrapValue += "wrap-reverse";
} else {
wrapValue += "wrap";
}
if (balanceInput.checked) {
wrapValue += " balance";
}
boxElem.style.flexWrap = wrapValue;
updateCurrentlySet();
}
function setFlexLineCount() {
lineCountValue = lineCountInput.value;
boxElem.style.flexLineCount = lineCountValue;
outputElem.textContent = lineCountValue;
updateCurrentlySet();
}
reverseInput.addEventListener("change", setFlexWrap);
balanceInput.addEventListener("change", setFlexWrap);
lineCountInput.addEventListener("input", setFlexLineCount);
Change the form control settings to see how they affect the output. Note how:
wrap and wrap-reverse changes the direction in which the flex lines are laid out along the cross-axis.balance causes the flex items to be evenly distributed across the available flex lines.flex-line-count value changes the minimum number of lines the items will be distributed across. The flex-line-count property, and therefore the slider, has no effect unless balance is checked.flex-line-count to a value of less than 5 has no effect. Values from 5 to 10 add an extra flex line.flex-line-count values of 10 or greater will all have the same effect — one item will be placed on each of 10 flex lines.| Specification |
|---|
| CSS Flexible Box Layout Module Level 1 # flex-wrap-property |