Course Outline (Part 8)

Welcome to Part 8 of the CSS Mastery & Responsive Layouts Course. In this part, we will explore Flex Items & Sizing.

While Flexbox containers control the flow and alignments, the sizing mechanics are governed by the properties applied directly to the Flex Items (the children). Properties like flex-grow, flex-shrink, and flex-basis determine how elements grow to fill empty spaces or shrink to prevent container overflows.


Chapter 58: Flex Item Sizing Principles

When items are placed in a flex container, their widths (or heights if column direction) are no longer determined strictly by width/height properties. Instead, the browser calculates their size in three steps:

  1. Check the starting size of the items (Flex Basis).
  2. If the container has leftover space, distribute it according to growth factors (Flex Grow).
  3. If the items are larger than the container, shrink them according to shrink factors (Flex Shrink).

Chapter 59: Flex Grow (flex-grow)

The flex-grow property specifies how much of the container’s leftover space should be allocated to a flex item. The value is a unitless number acting as a ratio.

.main-content {
  flex-grow: 1; /* Grows to occupy any leftover space */
}

59.1 Space Distribution Math

Suppose a container is 800px wide. It contains three items, each with a base width of 100px. The remaining empty space is:

$$800\text{px} - 300\text{px} = 500\text{px}$$

  • If Item A has flex-grow: 1, Item B has flex-grow: 1, and Item C has flex-grow: 3.
  • The total growth parts = $1 + 1 + 3 = 5$ parts.
  • Each part = $500\text{px} / 5 = 100\text{px}$.
  • Item A grows by $100\text{px} \rightarrow$ Final width = $200\text{px}$.
  • Item B grows by $100\text{px} \rightarrow$ Final width = $200\text{px}$.
  • Item C grows by $300\text{px} \rightarrow$ Final width = $400\text{px}$.

Chapter 60: Flex Shrink (flex-shrink - Deep Dive Math)

The flex-shrink property specifies how items shrink when their combined base sizes exceed the container’s width.

.sidebar {
  flex-shrink: 0; /* Prevents the sidebar from shrinking, locking its size */
}
  • Preventing distortion: By default, flex-shrink is set to 1. This means icons, avatars, or sidebars might get squeezed and look distorted. Setting flex-shrink: 0 preserves their exact base sizes.

60.1 Weighted Shrink Calculation

Unlike flex-grow which divides space linearly, flex-shrink uses weighted shrink factors. The browser scales shrinking based on both the shrink factor and the starting size (flex-basis) of the item, ensuring larger items don’t shrink down to zero width too quickly.

Formula: $$\text{Weighted Shrink Value} = \text{flex-basis} \times \text{flex-shrink}$$

Example: Container width = $500\text{px}$.

  • Item A: base width $= 300\text{px}$, flex-shrink: 1. Weighted value $= 300 \times 1 = 300$.
  • Item B: base width $= 300\text{px}$, flex-shrink: 2. Weighted value $= 300 \times 2 = 600$.
  • Combined width $= 600\text{px}$ (exceeds container by $100\text{px}$).
  • Total weighted sum $= 300 + 600 = 900$.
  • Item A shrinks by: $100\text{px} \times (300 / 900) = 33.3\text{px} \rightarrow$ Final width $= 266.7\text{px}$.
  • Item B shrinks by: $100\text{px} \times (600 / 900) = 66.7\text{px} \rightarrow$ Final width $= 233.3\text{px}$.

Chapter 61: Flex Basis (flex-basis)

The flex-basis property defines the default starting size of an element before any grow or shrink calculations are applied.

.card {
  flex-basis: 250px; /* Starting width is 250px */
}
  • auto (Default): The browser checks the element’s width property. If width is not set, it falls back to the size of the inner content.
  • 0: The browser treats the item as having zero width during calculations, distributing the entire container width using flex-grow ratios.
  • content: Sized based strictly on the element’s content, regardless of width properties.
  • Length Units: Explicit sizes in px, rem, em, %, etc.

Chapter 62: The Flex Shorthand (flex)

It is best-practice to declare these three properties using the flex shorthand. It configures sensible defaults and solves common layout bugs automatically.

/* Syntax: flex: [flex-grow] [flex-shrink] [flex-basis] */
.card-item {
  flex: 1 0 200px;
}

62.1 Standard Shorthand Keywords

KeywordShorthand EquivalentDescription
flex: initialflex: 0 1 autoDefault behavior. Does not grow, can shrink, respects base width.
flex: autoflex: 1 1 autoFully fluid. Grows and shrinks as needed, respects base width.
flex: noneflex: 0 0 autoRigid. Cannot grow, cannot shrink, locked base width.
flex: 1flex: 1 1 0%Equal fluid grids. Grows and shrinks, ignores content width.

Chapter 63: Alignment Overrides & Ordering

63.1 Align Self (align-self)

Allows a single flex item to override the container’s align-items alignment:

.parent-container {
  display: flex;
  align-items: center;
}
.self-top-item {
  align-self: flex-start; /* Aligns to top edge, overriding center setting */
}

63.2 Order (order)

Determines the visual rendering order of flex items without modifying the HTML structure.

  • The default order value is 0 for all items.
  • Items are sorted from lowest value to highest value.
  • Negative numbers are allowed (items with negative values move to the front).
.first-visual-card {
  order: -1; /* Moves card to the visual front of the row */
}

Chapter 64: Flex Items Practice & Self-Check

64.1 Practice Exercises

  1. Exercise 1: Sidebar Layout: Design a sidebar layout where the sidebar remains rigid at 250px and the main feed stretches to fill the rest of the page.
    .wrapper {
      display: flex;
    }
    .sidebar {
      flex: 0 0 250px; /* Rigidity */
    }
    .main-feed {
      flex: 1; /* Grows and shrinks dynamically */
    }
  2. Exercise 2: Icon Preservation: Prevent a circular user icon from shrinking inside an inline author card list.
    .author-icon {
      flex-shrink: 0;
      width: 40px;
      height: 40px;
      border-radius: 50%;
    }
  3. Exercise 3: Re-ordering Banners: Move a responsive promo banner to the top of the stack on mobile viewports.
    @media (max-width: 600px) {
      .promo-banner {
        order: -1; /* Renders visually before other list items */
      }
    }

64.2 Self-Check Questions

  1. What is the default value of the flex-grow property?
    • Answer: 0 (does not grow).
  2. What is the default value of the flex-shrink property?
    • Answer: 1 (shrinks during container overflow).
  3. How do you prevent an image or icon from distorting under Flexbox container compression?
    • Answer: Set its flex-shrink property to 0.
  4. What property defines the starting size of a flex item before distribution?
    • Answer: flex-basis.
  5. What does flex: none expand to?
    • Answer: flex: 0 0 auto.
  6. Which property overrides the align-items container rule on a single child?
    • Answer: align-self.
  7. Can you assign negative values to the order property?
    • Answer: Yes, negative values are valid and move items to the front.
  8. What is the difference between flex: auto and flex: 1?
    • Answer: flex: auto uses the item’s content width as flex-basis, whereas flex: 1 uses 0% as the basis, dividing space equally regardless of text lengths.
  9. What is the default order value for all flex items?
    • Answer: 0.
  10. If a flex container direction is column, does flex-basis control width or height?
    • Answer: Height, since it operates relative to the active main axis direction.
  11. Does align-self: stretch work on an item with a fixed height?
    • Answer: No, explicit height constraints prevent height stretching.
  12. How is leftover space calculated for flex-grow?
    • Answer: The browser subtracts the sum of all items’ flex-basis (plus margins and gaps) from the total container width.
  13. What does the shorthand flex: 1 0 auto mean?
    • Answer: Grow = 1, Shrink = 0, Basis = auto.
  14. Does changing the order property affect screen readers or keyboard tab orders?
    • Answer: No. It only modifies visual rendering; the DOM order remains unchanged, meaning tab navigation and screen readers still follow the original HTML sequence.
  15. Can you use flex-grow and flex-shrink together on the same item?
    • Answer: Yes, this is standard practice and is typically declared using the flex shorthand.
  16. Why does flex-shrink use a weighted calculation instead of a linear ratio?
    • Answer: To prevent larger elements from losing too much size compared to smaller ones during container overflow.
  17. Under what condition does flex-basis ignore element widths?
    • Answer: When flex-basis is explicitly configured to values other than auto (e.g. 0, content, or direct size units).
  18. What happens if you set flex-grow: 0 on all items but the container is wider than the items?
    • Answer: The items render at their baseline sizes, leaving empty space at the end of the main axis (aligned by justify-content).
  19. Can order overrides cause visual accessibility issues?
    • Answer: Yes, because they decouple screen reader text reading order from the visual layout order, creating confusion for keyboard users.
  20. What is the difference between flex-basis: 0 and flex-basis: auto?
    • Answer: flex-basis: 0 allocates all space dynamically using grow ratios, while flex-basis: auto reserves the item’s base content size first before growing.

Discussion

Loading comments...