flex
The flex CSS property is shorthand for flex-grow, flex-shrink, and flex-basis.
CSS
JS
object.style.flex = "value";
Values
<'flex'> = none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
none
Expands to 0 0 auto.
<'flex-grow'> = <number [0,∞]>
<number [0, ∞]>
An integer or zero or more decimal digits followed by a dot (.) followed by one or more decimal digits. The first character may be preceded by a positive sign (+). The last character may be succeeded by an exponent (e or E) and an integer.
<'flex-shrink'> = <number [0,∞]>
<number [0, ∞]>
An integer or zero or more decimal digits followed by a dot (.) followed by one or more decimal digits. The first character may be preceded by a positive sign (+). The last character may be succeeded by an exponent (e or E) and an integer.
<'flex-basis'> = content | <'width'>
content
Indicates an automatic size based on the flex item's content.
<'width'> = auto | <length-percentage> | min-content | max-content | fit-content( <length-percentage> )
auto
Automatically specified by the user agent.
<length-percentage> = [ <length> | <percentage> ]
<length>
Specifies the length using a number followed by a unit of measurement.
<percentage>
Specifies the percentage using a number followed by a percent sign (%).
min-content
Represents the largest min-content contribution of the items occupying the track.
max-content
Represents the largest max-content contribution of the items occupying the track.
fit-content()
Represents the formula max(minimum, min(limit, max-content)), where minimum represents an auto minimum (which is often, but not always, equal to a min-content minimum), and limit is the track sizing function passed as an argument to fit-content(). This is essentially calculated as the smaller of minmax(auto, max-content) and minmax(auto, limit).
Initial
flex-basis | auto
flex-basis | content
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: 0 1 content;
}
</style>
</head>
<body>
<div id="myid">
<div>content</div>
<div>content</div>
<div>content</div>
<div>content</div>
<div>content</div>
</div>
</body>
</html>
flex-basis | length
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: 0 1 100px;
}
</style>
</head>
<body>
<div id="myid">
<div>length</div>
<div>length</div>
<div>length</div>
<div>length</div>
<div>length</div>
</div>
</body>
</html>
flex-basis | max-content
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: 0 1 max-content;
}
</style>
</head>
<body>
<div id="myid">
<div>max-content</div>
<div>max-content</div>
<div>max-content</div>
<div>max-content</div>
<div>max-content</div>
</div>
</body>
</html>
flex-basis | min-content
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: 0 1 min-content;
}
</style>
</head>
<body>
<div id="myid">
<div>min-content</div>
<div>min-content</div>
<div>min-content</div>
<div>min-content</div>
<div>min-content</div>
</div>
</body>
</html>
flex-basis | percentage
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: 0 1 20%;
}
</style>
</head>
<body>
<div id="myid">
<div>percentage</div>
<div>percentage</div>
<div>percentage</div>
<div>percentage</div>
<div>percentage</div>
</div>
</body>
</html>
flex-grow
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: 1 1 auto;
}
#myid > div:nth-of-type(1) {
background-image: linear-gradient(135deg, white, yellow);
flex-grow: 2;
}
</style>
</head>
<body>
<div id="myid">
<div>flex-grow</div>
<div>flex-grow</div>
<div>flex-grow</div>
<div>flex-grow</div>
<div>flex-grow</div>
</div>
</body>
</html>
flex-shrink
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: 0 1 25%;
}
#myid > div:nth-of-type(1) {
background-image: linear-gradient(135deg, white, yellow);
flex-shrink: 2;
}
</style>
</head>
<body>
<div id="myid">
<div>flex-shrink</div>
<div>flex-shrink</div>
<div>flex-shrink</div>
<div>flex-shrink</div>
<div>flex-shrink</div>
</div>
</body>
</html>
none
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
flex: none;
}
</style>
</head>
<body>
<div id="myid">
<div>none</div>
<div>none</div>
<div>none</div>
<div>none</div>
<div>none</div>
</div>
</body>
</html>
JS
<!doctype html>
<html>
<head>
<style>
#myid {
display: flex;
}
#myid > div {
background-image: linear-gradient(135deg, white, gray);
}
#myid > div:nth-of-type(1) {
background-image: linear-gradient(135deg, white, yellow);
}
</style>
<script>
function mouseover(value) {
var elements = document.getElementById("myid").getElementsByTagName("div");
for (var i = 0; i < elements.length; ++i) {
elements[i].style.flex = value;
}
}
function flexGrow(value) {
document.getElementById("myid").getElementsByTagName("div")[0].style.flexGrow = value;
}
function flexShrink(value) {
document.getElementById("myid").getElementsByTagName("div")[0].style.flexShrink = value;
}
</script>
</head>
<body>
<button onmouseover="mouseover(value)" value="0 1 auto">auto</button>
<button onmouseover="mouseover(value)" value="0 1 content">content</button>
<button onmouseover="mouseover(value)" value="0 1 100px">length</button>
<button onmouseover="mouseover(value)" value="0 1 max-content">max-content</button>
<button onmouseover="mouseover(value)" value="0 1 min-content">min-content</button>
<button onmouseover="mouseover(value)" value="0 1 20%">percentage</button>
<button onmouseover="mouseover(value);flexGrow(2)" value="1 1 auto">flex-grow</button>
<button onmouseover="mouseover(value);flexShrink(2)" value="0 1 25%">flex-shrink</button>
<input onmouseover="mouseover(value)" type="button" value="none">
<div id="myid">
<div>flex</div>
<div>flex</div>
<div>flex</div>
<div>flex</div>
<div>flex</div>
</div>
</body>
</html>
Internal
External