The flex property for CSS is shorthand for flex-grow, flex-shrink, and flex-basis.
object.style.flex = "value"; <'flex'> = none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] <'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).
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: 0 1 content;
}
</style>
</head>
<body>
<div class="myclass">
<div>content</div>
<div>content</div>
<div>content</div>
<div>content</div>
<div>content</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: 0 1 100px;
}
</style>
</head>
<body>
<div class="myclass">
<div>length</div>
<div>length</div>
<div>length</div>
<div>length</div>
<div>length</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: 0 1 max-content;
}
</style>
</head>
<body>
<div class="myclass">
<div>max-content</div>
<div>max-content</div>
<div>max-content</div>
<div>max-content</div>
<div>max-content</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: 0 1 min-content;
}
</style>
</head>
<body>
<div class="myclass">
<div>min-content</div>
<div>min-content</div>
<div>min-content</div>
<div>min-content</div>
<div>min-content</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: 0 1 20%;
}
</style>
</head>
<body>
<div class="myclass">
<div>percentage</div>
<div>percentage</div>
<div>percentage</div>
<div>percentage</div>
<div>percentage</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: 1 1 auto;
}
.myclass > div:nth-of-type(1)
{
background-image: linear-gradient(135deg, white, yellow);
flex-grow: 2;
}
</style>
</head>
<body>
<div class="myclass">
<div>flex-grow</div>
<div>flex-grow</div>
<div>flex-grow</div>
<div>flex-grow</div>
<div>flex-grow</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: 0 1 25%;
}
.myclass > div:nth-of-type(1)
{
background-image: linear-gradient(135deg, white, yellow);
flex-shrink: 2;
}
</style>
</head>
<body>
<div class="myclass">
<div>flex-shrink</div>
<div>flex-shrink</div>
<div>flex-shrink</div>
<div>flex-shrink</div>
<div>flex-shrink</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
flex: none;
}
</style>
</head>
<body>
<div class="myclass">
<div>none</div>
<div>none</div>
<div>none</div>
<div>none</div>
<div>none</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: flex;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
}
.myclass > div:nth-of-type(1)
{
background-image: linear-gradient(135deg, white, yellow);
}
</style>
</head>
<body>
<button>initial</button>
<button value="0 1 auto">auto</button>
<button value="0 1 content">content</button>
<button value="0 1 100px">length</button>
<button value="0 1 max-content">max-content</button>
<button value="0 1 min-content">min-content</button>
<button value="0 1 20%">percentage</button>
<button value="1 1 auto, 2">flex-grow</button>
<button value="0 1 25%, 2">flex-shrink</button>
<button>none</button>
<div class="myclass">
<div>flex</div>
<div>flex</div>
<div>flex</div>
<div>flex</div>
<div>flex</div>
</div>
<script>
function myfunction(myparameter)
{
const mytarget = myparameter.target;
const myproperty = mytarget.value || mytarget.innerHTML;
for(const mydiv1 of document.querySelectorAll(".myclass > div"))
{
mydiv1.style.flex = myproperty;
if(myproperty.includes(","))
{
const mysplit = myproperty.split(",");
mydiv1.style.flex = mysplit[0];
const mydiv2 = document.querySelector(".myclass > div");
if(mytarget.innerHTML == "flex-grow")
{
mydiv2.style.flexGrow = mysplit[1];
}
else if(mytarget.innerHTML == "flex-shrink")
{
mydiv2.style.flexShrink = mysplit[1];
}
}
}
}
for(const mybutton of document.querySelectorAll("button"))
{
mybutton.addEventListener("mouseover", myfunction);
}
</script>
</body>
</html>