Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 122 Β· Page Structure and Layout

Page Layout

Build a complete page layout from semantic HTML elements, then arrange it with CSS Flexbox and Grid, including a footer that sticks to the bottom.

In the previous lesson you learned which elements describe the regions of a page. Now it is time to put them in place: a header across the top, navigation, a main column, a sidebar and a footer at the bottom.

HTML gives the page its structure; CSS decides where things go. Modern CSS has two layout tools built for this job β€” Flexbox for arranging items in a row or a column, and Grid for arranging them in rows and columns at once. You do not need to master either one to follow along. This lesson shows you just enough to build solid, real-world layouts.

Start with the HTML

A good layout begins with clean, semantic markup in a sensible reading order. Here is the skeleton we will style throughout the lesson:

HTML
<body>
  <header>…site name and navigation…</header>
  <main>…the article…</main>
  <aside>…related links…</aside>
  <footer>…copyright…</footer>
</body>

Notice there are no wrapper <div> elements with names like left-column. The HTML says what each part is. Whether the sidebar appears on the left, on the right or below the article is purely a CSS decision, and it can change with screen size without touching the HTML.

Source order matters. Screen readers and keyboard users move through the page in the order of your HTML, not in the order it appears on screen. Put content in a logical reading order first, then use CSS to position it.

Normal flow: the layout you get for free

Without any layout CSS, block elements such as <header> and <main> stack from top to bottom, each one as wide as its container. That is called normal flow (see Block and Inline Elements). On a narrow phone screen, normal flow is often exactly what you want. Layout CSS is mostly about what to do when there is more room.

Flexbox: a row of items

Flexbox lines items up along one direction. You turn it on with display: flex on the parent; its direct children become flex items. It is perfect for a header with a logo on one side and links on the other.

A header with Flexbox

The properties that do the work:

  • display: flex β€” makes the children sit side by side.
  • justify-content β€” spreads items along the row (flex-start, center, space-between, …).
  • align-items β€” lines them up across the row (center is the classic "vertically center" trick).
  • gap β€” space between items, with no margin hacks.
  • flex-wrap: wrap β€” lets items move to a new line instead of overflowing.

Notice the nested Flexbox: the <header> lays out the name and the <nav>, and the <ul> inside the nav lays out the links. Nesting flex containers is normal and common.

Grid: the whole page

Grid is designed for two-dimensional layouts. You describe columns and rows on the parent, then tell each child where to go. The most readable way is named areas:

A full page layout with Grid

Read grid-template-areas like a little map of the page: each quoted string is a row, each word a cell. The header spans both columns, main and aside share the middle row, and the footer spans the bottom.

grid-template-columns: 1fr 160px creates two columns: a fixed 160-pixel sidebar, and a main column that takes one fraction (fr) of whatever space is left. Try changing it to 160px 1fr and swapping the words in the map so the sidebar moves to the left. The HTML does not change at all.

Flexbox or Grid?

Use Flexbox when… Use Grid when…
Items flow in one direction (a row of buttons, a nav bar, a card's footer). You need rows and columns lined up (a page skeleton, a photo gallery, a form with aligned labels).
The content decides the sizes. The layout decides the sizes.

Real pages use both: Grid for the overall page, Flexbox inside the header and cards.

On a page with very little content, the footer ends up halfway up the screen, which looks unfinished. A sticky footer stays at the bottom of the window when the content is short, but still gets pushed down normally when the content is long. (Do not confuse it with position: sticky, which keeps an element on screen while you scroll.)

With Grid, it takes three lines:

Sticky footer

The trick is grid-template-rows: auto 1fr auto. The header and footer are as tall as their content (auto), and the middle row takes all the remaining height (1fr). Because min-height is used rather than height, the page can still grow when the content is long. In real projects you will often see min-height: 100vh or 100dvh (the full height of the viewport) on the body instead of the html, body { height: 100% } pair.

Centering and limiting width

Long lines of text are hard to read. A common pattern is to limit the content width and center it:

HTML
<style>
  .container {
    max-width: 70ch;   /* about 70 characters per line */
    margin-inline: auto; /* equal space left and right */
    padding-inline: 16px;
  }
</style>

<main class="container">…</main>

max-width lets the box shrink on small screens but never grow beyond a comfortable reading width. margin-inline: auto splits the leftover space evenly on both sides.

Old layout techniques to avoid

You may come across these in older code. Recognize them, but do not copy them:

  • Tables for layout. Tables are for data. Layout tables confuse screen readers and are painful to maintain.
  • Floats for columns. float was designed to wrap text around images. Using it for whole columns needed "clearfix" hacks that Flexbox and Grid made obsolete.
  • Fixed pixel widths for the whole page. A page that is exactly 960px wide breaks on phones. The next lesson shows how to make layouts flexible.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. You want a header with the logo on the left and the navigation on the right, centered vertically. Which tool fits best?

    It is a single row of items, which is exactly what Flexbox is for. Tables are for data, and floats need workarounds.
  2. 2. What does grid-template-columns: 1fr 200px create?

    fr stands for a fraction of the free space. The second column is fixed at 200px, and the first gets everything else.
  3. 3. Why does the sticky footer example use grid-template-rows: auto 1fr auto?

    The 1fr row absorbs all leftover height, pushing the footer to the bottom when the content is short.
  4. 4. Your sidebar appears before the article on screen, but you want screen-reader users to hear the article first. What should you do?

    Assistive technology follows the source order. Write a logical order in HTML, then let CSS handle the visual placement.

Summary

  • Write semantic HTML in a logical reading order first; layout is CSS's job.
  • Flexbox (display: flex) arranges items in one direction; use gap, justify-content and align-items.
  • Grid (display: grid) arranges rows and columns; grid-template-areas gives you a readable map of the page.
  • A sticky footer is min-height plus grid-template-rows: auto 1fr auto.
  • Limit reading width with max-width and center with margin-inline: auto; avoid layout tables and float columns.

Next lesson: Responsive Web Design β€” making layouts adapt from phones to wide screens.