Aral Code

Layout Lesson 20 of 33

CSS Flexbox — Flex Items, flex-grow and Real Layouts

Control individual flex items with flex-grow, flex-shrink, flex-basis, the flex shorthand, align-self and order, then build cards, a sidebar layout and a sticky footer.

In the last lesson, you controlled the container. Now you'll control the items: how much each one grows or shrinks, its starting size, and its individual alignment. With these, you can build real page layouts.

flex-grow: who takes the extra space?

When the container has leftover space, flex-grow decides how it's shared. The default is 0 (don't grow). An item with flex-grow: 1 takes all the extra space. If several items have flex-grow, they share the space according to their numbers.

Example flex-grow
<style>
  .row { display: flex; gap: 6px; background: #f3f6f4; padding: 6px; margin-bottom: 10px; border-radius: 8px; }
  .row div { background: #0f5c4d; color: white; padding: 10px; border-radius: 6px; font: 14px monospace; }
</style>
<div class="row"><div>none</div><div>none</div><div>none</div></div>
<div class="row"><div>none</div><div style="flex-grow: 1;">grow: 1</div><div>none</div></div>
<div class="row"><div style="flex-grow: 1;">grow: 1</div><div style="flex-grow: 2;">grow: 2</div><div style="flex-grow: 1;">grow: 1</div></div>

A very common use is a search bar: the input box grows to fill the space, and the button stays its natural size.

Example A search bar
<style>
  .search { display: flex; gap: 8px; font-family: Arial, sans-serif; }
  .search input { flex-grow: 1; padding: 10px; border: 2px solid #0f5c4d; border-radius: 8px; font-size: 16px; }
  .search button { padding: 10px 16px; background: #0f5c4d; color: white; border: 0; border-radius: 8px; font-size: 16px; }
</style>
<form class="search">
  <input type="search" placeholder="Search lessons…">
  <button>Search</button>
</form>

flex-shrink

When there's not enough space, items shrink. flex-shrink controls how much. The default is 1 (shrink equally). Set flex-shrink: 0 on something that must never get smaller, like a logo or a profile picture.

flex-basis: the starting size

flex-basis sets an item's size before extra space is shared out. It's like width for a row (or height for a column).

.card { flex-basis: 250px; }   /* start at 250px, then grow or shrink */

The flex shorthand

In practice, people write all three in one property, flex: grow shrink basis. These are the most common values:

You writeMeaning
flex: 1;Grow and shrink equally, starting from 0. Items become equal width.
flex: 1 1 250px;Start at 250px, grow and shrink as needed. Great for responsive cards.
flex: none;Stay at natural size, never grow or shrink.
Example Equal columns with flex: 1
<style>
  .cols { display: flex; gap: 10px; font-family: Arial, sans-serif; }
  .cols > div { flex: 1; background: #e3f1ec; padding: 14px; border-radius: 10px; }
</style>
<div class="cols">
  <div><strong>Vision</strong><br>A learning school for every Filipino child.</div>
  <div><strong>Mission</strong><br>Quality, accessible education.</div>
  <div><strong>Values</strong><br>Maka-Diyos, Maka-tao, Makakalikasan, Makabansa.</div>
</div>

Responsive cards without media queries

Combine flex-wrap: wrap with flex: 1 1 200px. Each card wants to be at least about 200px wide. If three fit, you get three in a row. On a phone, they wrap to one or two per row automatically:

Example Cards that wrap by themselves
<style>
  .cards { display: flex; flex-wrap: wrap; gap: 12px; font-family: Arial, sans-serif; }
  .card { flex: 1 1 200px; border: 1px solid #e3e0d6; border-radius: 12px; padding: 16px; background: white; }
  .card h3 { margin: 0 0 6px; color: #0f5c4d; }
  .card p { margin: 0; color: #5b6662; }
</style>
<div class="cards">
  <div class="card"><h3>📚 Library</h3><p>Open 7 AM to 5 PM, Monday to Friday.</p></div>
  <div class="card"><h3>💻 Computer Lab</h3><p>Reserve a slot with your ICT teacher.</p></div>
  <div class="card"><h3>🏥 Clinic</h3><p>The school nurse is in from 8 AM to 4 PM.</p></div>
  <div class="card"><h3>🍱 Canteen</h3><p>Healthy meals from ₱25.</p></div>
</div>

Drag the editor narrower, or view it on your phone, to watch the cards rearrange.

align-self

align-self overrides the container's align-items for one item only. For example, in a row where everything is at the top, one item can sit at the bottom with align-self: flex-end.

order

order changes where an item appears without changing the HTML. Items with a lower number come first. The default is 0.

margin-left: auto — the push trick

In a flex container, an auto margin takes all the free space on that side. So margin-left: auto on one item pushes it, and everything after it, to the far right:

Example Push the login button to the right
<style>
  .nav { display: flex; gap: 14px; align-items: center; background: #1b2421; padding: 10px 14px; font-family: Arial, sans-serif; border-radius: 10px; }
  .nav a { color: #dfe7e3; text-decoration: none; }
  .nav .login { margin-left: auto; background: #f2b33d; color: #1d2321; padding: 6px 14px; border-radius: 999px; font-weight: bold; }
</style>
<nav class="nav">
  <a href="#"><strong>Aral</strong></a><a href="#">Courses</a><a href="#">About</a>
  <a class="login" href="#">Log in</a>
</nav>

Here's a classic page built only with Flexbox. The page is a column: header, middle, footer. The middle section grows to push the footer to the bottom, and inside the middle, a fixed-width sidebar sits next to content that fills the rest.

Example A complete Flexbox page layout
<style>
  * { box-sizing: border-box; }
  body { margin: 0; font-family: Arial, sans-serif; }
  .page { display: flex; flex-direction: column; min-height: 100vh; }
  header, footer { background: #0f5c4d; color: white; padding: 12px 16px; }
  .middle { flex: 1; display: flex; }
  aside { flex: 0 0 140px; background: #e3f1ec; padding: 12px; }
  main { flex: 1; padding: 12px 16px; }
</style>
<div class="page">
  <header>Grade 10 – Rizal</header>
  <div class="middle">
    <aside>Subjects<br>Schedule<br>Projects</aside>
    <main>Welcome to our class page! The footer stays at the bottom even when this content is short.</main>
  </div>
  <footer>© 2026 Grade 10 – Rizal</footer>
</div>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Three items have flex: 1. How wide are they?
2. In a search bar, which property makes the input fill the leftover space?
3. A logo keeps getting squished on small screens. What helps?
4. In a flex row, how do you push the last button to the far right?

By Sir Reyel · Last updated 2026-12-04