A rugalmas tároló közvetlen gyermekelemei automatikusan rugalmas (flex) elemekké válnak.
A fenti elem négy kék flex elemet jelöl egy szürke flex tárolóban.
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>Flexible Items</h1>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<p>All direct children of a flexible container becomes flexible items.</p>
</body>
</html>
A rugalmas elemek tulajdonságai a következők:
order
flex-grow
flex-shrink
flex-basis
flex
align-self
Az order
tulajdonság határozza meg a rugalmas elemek sorrendjét.
A kód első rugalmas elemének nem kell az elrendezés első elemeként megjelennie.
A rendelés értékének számnak kell lennie, az alapértelmezett érték 0.
Az order tulajdonság megváltoztathatja a rugalmas elemek sorrendjét:
<div class="flex-container">
<div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container>div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The order Property</h1>
<p>Use the order property to sort the flex items as you like:</p>
<div class="flex-container">
<div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
</div>
</body>
</html>
A flex-grow
tulajdonság megadja, hogy egy rugalmas elem mennyivel nő a többi rugalmas elemhez képest.
Az értéknek számnak kell lennie, az alapértelmezett érték 0.
A harmadik rugalmas elem nyolcszor gyorsabban nőjön, mint a többi rugalmas elem:
<div class="flex-container">
<div style="flex-grow: 1">1</div>
<div style="flex-grow: 1">2</div>
<div style="flex-grow:
8">3</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The flex-grow Property</h1>
<p>Make the third flex item grow eight times faster than the other flex items:</p>
<div class="flex-container">
<div style="flex-grow: 1">1</div>
<div style="flex-grow: 1">2</div>
<div style="flex-grow: 8">3</div>
</div>
</body>
</html>
A flex-shrink
tulajdonság megadja, hogy egy rugalmas elem mennyivel zsugorodik a többi rugalmas elemhez képest.
Az értéknek számnak kell lennie, az alapértelmezett érték 1.
Ne hagyja, hogy a harmadik rugalmas elem annyira zsugorodik, mint a többi rugalmas elem:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-shrink:
0">3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container>div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The flex-shrink Property</h1>
<p>Do not let the third flex item shrink as much as the other flex items:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-shrink: 0">3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
</body>
</html>
A flex-basis
tulajdonság egy rugalmas elem kezdeti hosszát adja meg.
Állítsa be a harmadik rugalmas elem kezdeti hosszát 200 képpontra:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-basis: 200px">3</div>
<div>4</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The flex-basis Property</h1>
<p>Set the initial length of the third flex item to 200 pixels:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-basis:200px">3</div>
<div>4</div>
</div>
</body>
</html>
A flex
tulajdonság a rövidített tulajdonsága flex-grow
, flex-shrink
és flex-basis
tulajdonságok.
A harmadik rugalmas elemet ne növeszthetővé (0), ne zsugorodóvá (0) tegye, és egy kezdeti hossz 200 pixel:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex:
0 0 200px">3</div>
<div>4</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
align-items: stretch;
background-color: #f1f1f1;
}
.flex-container>div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The flex Property</h1>
<p>Make the third flex item not growable (0), not shrinkable (0), and with an initial length of 200 pixels:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex: 0 0 200px">3</div>
<div>4</div>
</div>
</body>
</html>
Az align-self
tulajdonság határozza meg a kiválasztott elem igazítását a rugalmas tárolóban.
Az align-self
tulajdonság felülírja a tároló align-items
tulajdonsága által beállított alapértelmezett igazítást.
Ezekben a példákban 200 képpont magas tárolót használunk, hogy jobban szemléltessük a align-self
tulajdonság:
Igazítsa a harmadik rugalmas elemet a tartály közepére:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="align-self:
center">3</div>
<div>4</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-self Property</h1>
<p>The "align-self: center;" aligns the selected flex item in the middle of the container:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="align-self: center">3</div>
<div>4</div>
</div>
<p>The align-self property overrides the align-items property of the container.</p>
</body>
</html>
Igazítsa a második rugalmas elemet a tartály tetejéhez, a harmadik rugalmas elemet pedig a tárolóhoz a tartály alja:
<div class="flex-container">
<div>1</div>
<div style="align-self:
flex-start">2</div>
<div style="align-self:
flex-end">3</div>
<div>4</div>
</div>
Próbálja ki Ön is →
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
height: 200px;
background-color: #f1f1f1;
}
.flex-container > div {
background-color: DodgerBlue;
color: white;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-self Property</h1>
<p>The "align-self: flex-start;" aligns the selected flex item at the top of the container.</p>
<p>The "align-self: flex-end;" aligns the selected flex item at the bottom of the container.</p>
<div class="flex-container">
<div>1</div>
<div style="align-self: flex-start">2</div>
<div style="align-self: flex-end">3</div>
<div>4</div>
</div>
<p>The align-self property overrides the align-items property of the container.</p>
</body>
</html>
A következő táblázat felsorolja a CSS Flexbox Items összes tulajdonságát:
Megadja egy rugalmas elem igazítását (felülbírálja a rugalmas tároló align-items tulajdonságát)
A flex-grow, flex-shrink és a flex-alap rövidített tulajdonsága tulajdonságait
Meghatározza egy rugalmas elem kezdeti hosszát
Meghatározza, hogy egy rugalmas elem mennyit fog növekedni az ugyanabban a tárolóban lévő rugalmas elemekhez képest
Megadja, hogy egy rugalmas elem mennyit zsugorodik az ugyanabban a tárolóban lévő rugalmas elemhez képest
Megadja az ugyanabban a tárolóban lévő rugalmas elemek sorrendjét