Full Stack JS

Chapter 3
0%

Chapter 3: CSS Flexbox & Grid

Master modern CSS layout systems — arrange elements in rows, columns, and grids with ease.

Flexbox Basics
Flex Properties
CSS Grid
Responsive
Quiz

Flexbox Basics

Flexbox is like arranging books on a shelf. You choose the shelf direction (left-to-right or top-to-bottom), then decide how to space the books out — push them to the left, center them, or spread them evenly across the shelf.

Flexbox works with two concepts:

HTML — Flexbox: justify-content & align-items
PropertyWhat It DoesCommon Values
justify-contentHorizontal alignmentflex-start, center, flex-end, space-between, space-around, space-evenly
align-itemsVertical alignmentflex-start, center, flex-end, stretch, baseline
flex-directionDirection of itemsrow (default), column, row-reverse, column-reverse
gapSpace between items10px, 1rem, etc.

Flex Item Properties

Beyond the container, each flex item can control its own behavior with properties like flex-grow, flex-shrink, and flex-wrap.

HTML — flex-grow, flex-wrap, and flex-direction
The shorthand flex: 1 is the same as flex-grow: 1; flex-shrink: 1; flex-basis: 0. It tells an item to take up an equal share of available space. It's the most commonly used flex shorthand!

CSS Grid

If Flexbox is a bookshelf (one-dimensional, items in a row or column), CSS Grid is a spreadsheet (two-dimensional, items in rows AND columns at the same time). Use Flexbox for simple rows/columns, and Grid for complex layouts.
HTML — CSS Grid Layout
Grid PropertyWhat It DoesExample
grid-template-columnsDefines column sizes1fr 1fr 1fr (3 equal)
grid-template-rowsDefines row sizes60px 1fr 50px
grid-columnSpan columns1 / 3 (spans 2 cols)
gapSpace between cells12px
frFraction of free space2fr = twice as wide

Responsive Design with Media Queries

Media queries are like a wardrobe for different weather. You wear shorts in summer and a coat in winter. Similarly, your website wears different styles depending on the screen size — big layouts on desktops, stacked layouts on phones.
HTML — Responsive Media Queries
Always design mobile-first! Start with styles for small screens, then use @media (min-width: ...) to add layout for larger screens. This approach makes responsive design much easier.

🎯 Challenge!

  1. Add a 4th card to the grid
  2. Change the large-screen layout to 4 columns: grid-template-columns: 1fr 1fr 1fr 1fr
  3. Add a breakpoint at 500px for 2 columns

📝 Chapter 3 Quiz

1. What CSS property activates Flexbox on a container?

flex: 1
position: flex
display: flex
flexbox: true

2. Which property distributes items with equal space BETWEEN them?

align-items: space-between
justify-content: space-between
gap: space-between
flex-wrap: space-between

3. How do you create 3 equal columns with CSS Grid?

grid-template-columns: 1fr 1fr 1fr
grid-columns: 3
columns: 3 equal
display: grid-3

4. Which syntax is correct for a media query?

@screen (max-width: 768px) { }
@responsive (768px) { }
@breakpoint 768px { }
@media (max-width: 768px) { }

5. What is the key difference between Flexbox and Grid?

Flexbox is newer than Grid
Grid cannot center items
Flexbox is 1D (row or column), Grid is 2D (rows and columns)
They are identical, just different syntax
← Chapter 2: CSS Chapter 4: JavaScript →