Skip to content
Tutorials Center
My learning

Topics

Lesson 128 of 139 ยท Projects

Project: Responsive Restaurant Website

Build a complete one-page restaurant website in seven stages, mobile-first, with a sticky header, a hero, a menu grid with container queries and a booking form.

Most small businesses need the same kind of website: a header with a few links, a big welcoming hero, the thing they sell, a short story about who they are, opening hours, a way to get in touch and a footer. Build that once, properly, and you have a template for a bakery, a bike shop or a dentist. This project builds it for a restaurant, because a menu is a perfect place to try one of the best modern CSS features: components that adapt to the space they get.

You will build the page mobile-first. Every stage starts with the styles for a narrow phone screen, which need no media queries at all, and then adds a few min-width queries for larger screens (Mobile-First CSS). The menu cards go one step further and use container queries, so the same card can be compact in a narrow column and roomy when it spans the whole row. As in the other projects, every stage contains the complete code so far, so you can jump in anywhere.

The restaurant is Juniper Table, a fictional bistro in a fictional town. The dishes, address and phone number are all made up.

Stage 1: The markup

Good HTML comes first, because it decides what screen readers, search engines and the CSS can work with. The page has these landmarks and parts:

  • A skip link as the very first element, so keyboard users can jump past the header straight to the content. It is visible for now; Stage 7 hides it until it receives focus.
  • A <header> with the brand, a <nav> with an aria-label and a "Book a table" call to action.
  • A <main> with four parts: the hero, the menu, the about section and a "visit" area with the hours and the booking form. Each <section> is named by its heading through aria-labelledby.
  • Each dish is an <article> with the same parts in the same order: a decorative plate (aria-hidden="true", since it carries no information), a name and price, a description and a list of tags. The first dish has an extra dish--featured class.
  • The hours are a description list (<dl>), because they are name-value pairs. Each pair sits in a <div>, which HTML allows inside <dl> and which gives the CSS something to lay out as a row.
  • The address uses <address>, and the phone number is a tel: link that opens the dialer on a phone.
  • A <footer> with a second, smaller navigation.

The booking form must not send anything from a preview, so a tiny script cancels the submission and prints the data on the page instead. On a real site, the form would post to your server.

Stage 1: markup

It's plain, but it already works: every link jumps to its section, the form validates the required fields and the image scales down with the screen. Everything from here on only changes how it looks.

Stage 2: Tokens, base styles and buttons

Start with the decisions that affect everything. The color palette lives in custom properties on :root: a warm cream page, dark brown ink instead of pure black, a paprika red accent and an olive green for small details. Every later rule uses these names, so changing the whole mood of the site is a matter of editing one block.

Next come the base styles:

  • Headings use a serif (Georgia, which nearly every system has), while body text stays in the system sans-serif. Two families are plenty. text-wrap: balance keeps multi-line headings from ending with a single lonely word (Wrapping, Overflow and Hyphens).
  • Fluid heading sizes come from clamp(). The h2 size grows with the viewport but never drops below 1.6rem or exceeds 2.4rem (Fluid Typography).
  • .wrap centers the content with one line: width: min(100% - 2.5rem, var(--wrap)). On a phone, that is the full width minus 1.25rem on each side. On a wide screen, it stops at 68rem. No media query is needed, because min() picks whichever value is smaller (calc(), min(), max() and clamp()).
  • .section spacing also uses clamp(), so sections have less padding on phones and more on desktops.

The .button class works on both <a> and <button> elements. Buttons don't inherit fonts by default, which is why the rule sets font: inherit before font-weight. Two modifiers add a ghost style, with a transparent background and a border in currentColor, and a small size for the header (Styling Buttons and Links).

Stage 2: tokens and base

The page now has a personality, even though nothing is laid out yet. Notice how little CSS that took: most of the work is done by good defaults.

Stage 3: A sticky header that rearranges itself

The header has three parts: brand, navigation and button. On a phone, all three don't fit on one row, so the layout is a small grid with two rows: the brand and the button on top, and the navigation underneath across the full width. On screens 48em and wider, the same three parts move into a single row, with the navigation centered between them. Because each part has a grid-area name, the media query only redraws the map; it doesn't touch the individual items (Grid Template Areas).

CSS
.header-inner {
  grid-template-areas:
    "brand cta"
    "nav   nav";
}

@media (min-width: 48em) {
  .header-inner {
    grid-template-areas: "brand nav cta";
  }
}

A few details make it robust:

  • The link row scrolls sideways on very small screens instead of wrapping into a messy pile. overflow-x: auto on the list does that, and white-space: nowrap keeps each link on one line. The <nav> needs min-width: 0, because grid items refuse to shrink below their content's width by default, which would push the whole page wider instead of letting the list scroll.
  • position: sticky; top: 0 keeps the header at the top while the page scrolls (Positioning). A slightly transparent background plus backdrop-filter: blur(8px) lets the content show through softly as it passes underneath (Filters). z-index: 10 keeps it above the menu cards, which get a hover effect later.
  • The brand mark is a ::before pseudo-element: a small square with two rounded corners (border-radius: 50% 0), which reads as a leaf (Pseudo-elements). No image file needed.
Stage 3: header and navigation

Scroll the preview and the header stays put. If your preview is narrower than 48em (768 pixels), you see the two-row layout; change 48em to 20em to see the single-row version.

Stage 4: The hero

The hero is the first thing people see, and it should answer three questions in a few seconds: what is this place, why would I like it and what do I do next? The markup already has those parts: an eyebrow line, a heading, a short lead paragraph and two buttons.

The CSS gives it presence without any image:

  • Three layered gradients form the background: a soft red glow in the top right corner, an olive glow in the bottom left and a warm linear gradient underneath. Layers listed first are painted on top (Multiple Backgrounds and Gradients). The glows use colors with transparency, written as rgb(181 69 42 / 0.22), which fade to transparent.
  • The heading grows from 2.2rem on a phone to 4.2rem on a large screen, again with clamp(). max-width: 16ch stops it from running across the whole screen; ch is roughly the width of one character, so the limit scales with the font size.
  • The buttons sit in a flex row with flex-wrap: wrap and a gap, so on a very narrow phone the second button simply moves under the first (Flexbox Patterns).
  • The eyebrow is small, uppercase and spaced out with letter-spacing, a classic way to label a heading without competing with it.
Stage 4: the hero

The padding uses clamp(4rem, 2rem + 10vw, 8rem), so the hero breathes on a desktop without pushing everything else off a phone screen.

Stage 5: The menu grid with container queries

The menu is the heart of the page, and it is where this project gets interesting. There are two separate questions: how many columns should the grid have, and how should each card lay itself out?

The grid answers the first question with the classic responsive pattern, with no media queries (Grid Patterns):

CSS
.menu-grid {
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 16rem), 1fr));
}

Each column is at least 16rem wide, and the browser fits in as many as it can. The featured dish spans every column with grid-column: 1 / -1.

The card answers the second question, and here a media query would be the wrong tool. A card in the regular grid might be 16rem wide on a huge monitor, while the featured card is 60rem wide on the same monitor. The screen size tells you nothing about the space a card actually has. A container query does:

CSS
.dish {
  container: dish / inline-size;
}

@container dish (min-width: 30rem) {
  .dish__card {
    grid-template-columns: auto 1fr;
  }

  .dish__plate {
    grid-row: span 3;
    width: 8rem;
  }
}

The .dish article becomes a size container named dish, and the rule inside @container applies when that container is at least 30rem wide. The card itself is a grid. When it's narrow, there is one column and everything stacks. When it's wide, the plate takes a first column and spans three rows, while the name, description and tags flow into the second column next to it. Notice that the query targets .dish__card, a child of the container: an element can't query its own size, which is why the markup has the extra wrapper.

The other menu details:

  • The plate is pure CSS: two radial gradients, a colored "food" circle on top of a white plate with a slightly darker rim. Its hue comes from a --hue custom property set in each dish's style attribute, so every dish gets its own color from a single rule.
  • The dotted leader between the name and the price, as on a printed menu, is a ::after pseudo-element with flex: 1 and a dotted bottom border. order: 1 and order: 2 put the leader before the price, even though the pseudo-element comes last in the box tree.
  • The dish name uses the container query unit cqi: clamp(1.15rem, 0.9rem + 2cqi, 1.75rem). One cqi is 1% of the container's inline size, so the featured dish gets a larger heading than the small cards, without any extra rule.
Stage 5: menu and container queries

Look at the featured dish: it uses the wide layout while the cards below it use the compact one, side by side on the same screen. Make your browser narrower and the featured card switches to the compact layout once it gets below 30rem. That is the core idea of container queries: components respond to their own space.

Stage 6: About, hours and the booking form

The remaining sections share one approach: a single column on phones, and a two-column grid from 48em up.

About places the image and the story side by side. The image has aspect-ratio: 4 / 3 with object-fit: cover, so it keeps a pleasant shape whatever its natural size, cropping instead of stretching (Sizing Images: object-fit and aspect-ratio).

Hours turns each <div> inside the <dl> into a flex row with justify-content: space-between: the day on the left, the time on the right, and a thin line under each row. A table would also be acceptable here, but a description list matches the content better: there are only names and values, no columns of data to compare.

The booking form is a grid with the same auto-fit + minmax() trick as the menu, but with a smaller 11rem minimum. On a phone the fields stack; in a wider column, Name and Date share a row, then Time and Guests. The notes field, the button and the result message span every column with grid-column: 1 / -1 (Styling Forms). Two more details:

  • Every field gets font: inherit and width: 100%, because form controls come with their own small font and a fixed width.
  • The status message is hidden with :empty while it has no text. After a submission, the script fills it in and it appears. Because it has role="status", screen readers announce the message.

The two-column visit__grid uses 2fr 3fr, giving the form a little more room than the hours.

Stage 6: about, hours and booking

Fill in the form and press Request a booking to see the result line appear. Try submitting it empty, too: the browser's built-in validation catches the required fields before the script runs.

The footer uses dark ink as its background, and the links inherit the light text color with color: inherit. Its grid uses auto-fit once more. The small print spans all columns, with a faint border line made with a transparent white, so it works on any dark background.

Then come the details that separate a finished site from a demo:

  • Anchor links land in the right place. With a sticky header, clicking "Hours" would scroll the heading under the header. scroll-padding-top on html reserves room at the top of the scroll area: 7rem for the two-row header on phones, 5rem for the one-row header on wider screens.
  • Smooth scrolling, only for people who want motion. scroll-behavior: smooth and the card lift on hover are wrapped in @media (prefers-reduced-motion: no-preference), so people who asked their system to reduce motion get instant jumps and still cards (Motion and Performance). The lift uses the individual translate property, so it can be transitioned on its own.
  • A clear focus ring. :focus-visible gets a thick accent outline with an offset. It appears for keyboard users and stays out of the way for mouse clicks (Accessible CSS).
  • The skip link hides until it's needed. It is positioned above the top of the page and slides into view when it receives focus. Click into the preview and press Tab once to see it.
Stage 7: the finished website

That's a complete, responsive restaurant website in about 500 lines of CSS (counting blank lines), with no framework and only a few lines of JavaScript for the demo form. Try it at different widths: the header rearranges at 48em, the menu adds columns as space appears, the featured dish switches layouts by itself, and the about and visit sections become two columns.

Ideas to take it further

  • Add a dark theme by redefining the tokens in a prefers-color-scheme: dark block, as in the dashboard project and the Dark Mode lesson.
  • Split the menu into Starters, Mains and Desserts, and make the category headings stick to the top of the screen as you scroll through each group.
  • Replace the scrolling link row with a disclosure menu built with <details> on phones, as in the navigation bar project.
  • Add a print stylesheet that prints only the menu, with the dotted leaders, on a single sheet (Print Styles).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does width: min(100% - 2.5rem, var(--wrap)) do on a 400px-wide phone, if --wrap is 68rem?

    min() picks the smaller value. On a phone, 100% minus 2.5rem is far smaller than 68rem, so it wins. Math inside min() doesn't need calc().
  2. 2. Why does the dish card use a container query instead of a media query?

    The screen size says nothing about how wide a single card is. A container query lets each card respond to the space it actually has.
  3. 3. Why does the CSS query .dish but style .dish__card?

    A container query applies to elements inside the container. That's why the article is the container and the card wrapper inside it changes layout.
  4. 4. The header is sticky. What stops anchor links from scrolling headings underneath it?

    scroll-padding-top reserves space at the top of the scroll area, so the target lands below the sticky header. scroll-behavior only changes how the jump is animated.
  5. 5. Why does the navigation element get min-width: 0?

    Grid and flex items have an automatic minimum size based on their content. Setting it to 0 lets the item shrink so its scrolling list can do its job.

Summary

  • Build mobile-first: base styles for phones with no media queries, then a few min-width queries for larger screens.
  • Keep colors and sizes in custom properties, and use min() and clamp() for widths, spacing and type that adapt without breakpoints.
  • grid-template-areas lets a media query rearrange the header without touching its items; min-width: 0 lets a scrolling row shrink.
  • repeat(auto-fill, minmax(min(100%, 16rem), 1fr)) builds the menu grid; grid-column: 1 / -1 makes a featured item span it.
  • Container queries and cqi units let each card adapt to its own width, so one component works in a narrow column and a wide row.
  • Finish with scroll-padding-top for a sticky header, :focus-visible, a skip link and motion only under prefers-reduced-motion: no-preference.

Next: Project: Documentation Layout, where you build a docs page with a sticky sidebar, an "On this page" box and readable code blocks.