CSS: CSS Flexbox Items

Flexbox item properties give you fine control over individual children within a flex container.

1. Flex Item Properties

(1) Quick Reference

Property Values Purpose
flex-grow 0, 1, 2, ... How much item grows relative to siblings
flex-shrink 0, 1, 2, ... How much item shrinks relative to siblings
flex-basis auto, 100px, 50% Initial size before grow/shrink
flex 1, 0 0 200px, 1 1 auto Shorthand for grow, shrink, basis
order 0, 1, -1 Visual order (doesn't affect DOM)
align-self auto, flex-start, flex-end, center Override parent's align-items

2. flex-grow

Controls how much a flex item grows relative to other items:

▶ Example

HTML
<style>
.container {
  display: flex;
  gap: 10px;
  background: #f5f5f5;
  padding: 10px;
}
.item-1 { flex-grow: 1; background: #3498db; padding: 10px; color: white; }
.item-2 { flex-grow: 2; background: #e74c3c; padding: 10px; color: white; }
.item-3 { flex-grow: 1; background: #2ecc71; padding: 10px; color: white; }
</style>
<div class="container">
  <div class="item-1">grow: 1</div>
  <div class="item-2">grow: 2 (twice as wide)</div>
  <div class="item-3">grow: 1</div>
</div>
▶ Try it Yourself

3. flex: 1 Shorthand

flex: 1 is the most common shorthand:

▶ Example

HTML
<style>
.container {
  display: flex;
  gap: 10px;
  background: #f5f5f5;
  padding: 10px;
}
.item {
  flex: 1;
  background: #9b59b6;
  padding: 10px;
  color: white;
  text-align: center;
}
</style>
<div class="container">
  <div class="item">Equal</div>
  <div class="item">Equal</div>
  <div class="item">Equal</div>
</div>
▶ Try it Yourself

flex: 1 means flex-grow: 1; flex-shrink: 1; flex-basis: 0% — "grow to fill available space equally."


4. order

Changes visual order without affecting DOM order:

▶ Example

HTML
<style>
.container {
  display: flex;
  gap: 10px;
  background: #f5f5f5;
  padding: 10px;
}
.item {
  background: #f39c12;
  padding: 10px;
  color: white;
}
.item:nth-child(2) { order: -1; } /* Moves to front */
</style>
<div class="container">
  <div class="item">1st in DOM</div>
  <div class="item">2nd in DOM (visually first)</div>
  <div class="item">3rd in DOM</div>
</div>
▶ Try it Yourself

Note: order only changes visual order, not DOM order. Screen readers still follow DOM order.


5. align-self

Overrides parent's align-items for individual items:

▶ Example

HTML
<style>
.container {
  display: flex;
  align-items: center;
  height: 200px;
  background: #f5f5f5;
  gap: 10px;
  padding: 10px;
}
.item {
  background: #e74c3c;
  padding: 10px;
  color: white;
}
.item:nth-child(2) { align-self: flex-end; }
.item:nth-child(3) { align-self: flex-start; }
</style>
<div class="container">
  <div class="item">Center (default)</div>
  <div class="item">Bottom (align-self)</div>
  <div class="item">Top (align-self)</div>
</div>
▶ Try it Yourself

❓ FAQ

Q What does flex: 1 mean?
A flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0% — meaning "grow to fill remaining space." When multiple items all have flex: 1, they equally share the container width.
Q Does order affect DOM structure?
A No. order only changes visual arrangement, DOM order stays the same. Screen readers still follow DOM order, so don't use order for visual-only sorting — it affects accessibility.
Q Which has higher priority: align-self or align-items?
A align-self has higher priority. It acts on individual items, overriding the parent's align-items. For example, parent sets align-items: center for center alignment, but a child can set align-self: flex-start to align to the top individually.

📖 Summary

📝 Exercises

  1. Create a flex container with 5 items. Use order to show the 3rd item first and the 1st item last.
  2. Use flex-grow to implement "three equal columns," then make the center column twice the width of the sides.
  3. Create a "fixed sidebar + adaptive content" layout: sidebar uses flex: 0 0 250px, content uses flex: 1.
  4. In a container, align the first two items to the top and the third to the bottom using align-self.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏