Learn Frontend Part 4: CSS Layout - Flexbox, Grid and Responsive Design

Learn Frontend Part 4: CSS Layout - Flexbox, Grid and Responsive Design - CodingHumans

Layout used to mean floats, clearfix hacks and tables. Today two systems handle almost everything: Flexbox for arranging items in one direction and CSS Grid for two-dimensional layouts. Add a mobile-first responsive strategy and you can build any modern interface with surprisingly little CSS.

This is Part 4 of the Learn Frontend series. We cover Flexbox, Grid, when to use each, responsive design with media and container queries, and a few patterns that come up in interviews and machine-coding rounds.

1. Flexbox: one-dimensional layout

Setting display: flex on a container lays its children out along a main axis (a row by default) with a perpendicular cross axis.

Property (container)What it does
flex-directionrow, row-reverse, column, column-reverse
justify-contentDistributes items along the main axis (start, center, space-between, space-around, space-evenly)
align-itemsAligns items on the cross axis (stretch, center, flex-start, baseline)
flex-wrapLets items wrap onto new lines
gapSpace between items, no margins needed

On the items, flex: 1 (shorthand for grow 1, shrink 1, basis 0) makes them share free space equally; flex-shrink: 0 stops an item from shrinking; margin-left: auto pushes an item to the far end.

/* Navbar: logo left, links right, vertically centred */
.navbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
}
.navbar .logo  { font-weight: 700; }
.navbar .links { margin-left: auto; display: flex; gap: 12px; }

/* Perfect centring */
.center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100dvh;
}

2. CSS Grid: two-dimensional layout

Grid defines rows and columns at the same time, so it is ideal for page layouts, galleries and dashboards.

  • grid-template-columns: 200px 1fr 1fr creates three columns; fr units share the remaining space.
  • repeat(auto-fill, minmax(220px, 1fr)) fits as many columns as possible, each at least 220px wide: a fully responsive card grid with no media queries.
  • grid-template-areas names regions so the layout reads like a picture in your CSS.
  • Items can span with grid-column: span 2 or be placed exactly with line numbers.
/* Responsive card grid: no media queries needed */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
}

/* Page layout with named areas */
.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  gap: 16px;
}
.page header { grid-area: header; }
.page aside  { grid-area: sidebar; }
.page main   { grid-area: main; }
.page footer { grid-area: footer; }

3. Flexbox or Grid?

Use Flexbox whenUse Grid when
Items flow in one direction (a navbar, a toolbar, a row of buttons)You control rows and columns together (page shell, gallery, dashboard)
Item sizes should come from their contentThe layout defines the sizes and items fill them
You need to centre one thing quicklyYou need items aligned in both directions across rows

They work well together: a Grid page layout whose header uses Flexbox, for example. The classic "centre a div" interview question is answered with either display: flex; justify-content: center; align-items: center; or display: grid; place-items: center;.

4. Responsive design

Responsive pages adapt to any screen size. Start with the viewport meta tag (Part 2), then follow a mobile-first approach: write base styles for small screens and add min-width media queries as space grows.

/* Mobile first: one column by default */
.page {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas: "header" "main" "sidebar" "footer";
}

/* Wider screens: add the sidebar column */
@media (min-width: 768px) {
  .page {
    grid-template-columns: 240px 1fr;
    grid-template-areas:
      "header  header"
      "sidebar main"
      "footer  footer";
  }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
  • Fluid by default: use percentages, fr, max-width and clamp() so most things adapt without breakpoints.
  • Responsive images: max-width: 100%; height: auto; plus srcset and the <picture> element for art direction.
  • Container queries: @container (min-width: 400px) lets a component adapt to the space its parent gives it rather than to the whole viewport, which is ideal for reusable cards.
  • User preferences: prefers-color-scheme for dark mode and prefers-reduced-motion to tone down animations.

5. Layout patterns worth practising

  • Sticky footer: make the body a column flex container with min-height: 100dvh and give main flex: 1.
  • Sidebar layout: grid-template-columns: 260px 1fr, collapsing to one column on small screens.
  • Card grid: repeat(auto-fill, minmax(...)) with gap.
  • Truncating text: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; (in a flex child, also add min-width: 0).
  • Aspect ratio boxes: aspect-ratio: 16 / 9 for video embeds and thumbnails.
Interview tip: machine-coding rounds often ask for a responsive navbar, a product grid or a modal. Practise building each with Flexbox and Grid without a framework.
Key takeaways
  • Flexbox arranges items along one axis; justify-content works on the main axis and align-items on the cross axis.
  • Grid controls rows and columns together; fr units, repeat() and minmax() build flexible layouts.
  • Use Flexbox for components and content-sized rows, Grid for page shells and galleries, and combine them freely.
  • Go mobile-first with min-width media queries, fluid units and clamp(); use container queries for reusable components.
  • Practise sticky footers, sidebars, card grids, truncation and aspect-ratio boxes.

Practice problems

Build each of these without any CSS framework. They are common machine-coding round tasks.

  1. Centre a box horizontally and vertically in three different ways.
  2. Build a responsive navbar that collapses into a menu button below 768px.
  3. Create a product card grid that shows 1, 2, 3 or 4 columns depending on width, without media queries.
  4. Build a dashboard shell with header, sidebar, main and footer using grid-template-areas.
  5. Make a footer that stays at the bottom on short pages but flows normally on long ones.
  6. Create a card component that switches to a horizontal layout using a container query.
  7. Play Flexbox Froggy and Grid Garden to drill the properties.

Interview questions and FAQ

What is the difference between Flexbox and Grid?

Flexbox is one-dimensional: it arranges items in a row or a column and sizes them from their content. Grid is two-dimensional: it defines rows and columns together, and items are placed into that structure.

How do you centre a div?

The simplest modern ways are display: flex with justify-content: center and align-items: center on the parent, or display: grid with place-items: center. For horizontal centring of a block with a width, margin: 0 auto also works.

What does flex: 1 mean?

It is shorthand for flex-grow: 1, flex-shrink: 1 and flex-basis: 0%. Items with flex: 1 ignore their content size and share the available space equally.

What is mobile-first design?

Writing the base CSS for small screens and adding styles for larger screens with min-width media queries. It keeps mobile CSS simple and fast and progressively enhances the layout as space grows.

What are container queries?

They let an element change its styles based on the size of its container instead of the viewport. This makes components truly reusable, since a card can look right in a narrow sidebar and a wide main column.

Comments

Answered 0/0Correct 0