Skip to content
Tutorials Center
My learning

Topics

Lesson 130 of 139 Β· Projects

Project: Dashboard Layout

Build a responsive admin dashboard in eight stages with grid template areas, a sidebar, stat cards, a CSS bar chart, container queries and dark mode.

Dashboards are where CSS layout earns its keep. There is a top bar, a sidebar, a grid of cards, a chart and a list, and every one of them has to rearrange itself sensibly from a phone to a wide monitor. A dashboard is also used for hours at a time, often late in the evening, so a comfortable dark mode is not optional.

This final project brings the whole course together. You will build the overview page of a small online shop in eight stages, using grid template areas for the page shell, flexbox for the bars and rows, container queries for components that adapt to their own width, and custom properties for a theme that switches to dark automatically. As always, every stage contains the complete code so far and links back to the lessons it uses.

The shop is Cobble Coffee, a fictional coffee roaster. All the numbers are made up.

Stage 1: The markup

The page is one .app wrapper with three children, and each one has a clear role:

  • A <header> top bar with the brand, a search field and the signed-in person's initials. The initials have role="img" and an aria-label, so screen readers announce a name instead of "M S".
  • A <nav> sidebar with an aria-label, so it is a named landmark, and aria-current="page" on the current link.
  • A <main> area with a heading, four stat cards and two panels: a chart and a list of recent orders.

The bar chart is an ordinary list. Each item holds the day and the number as text, and passes the number to CSS as a custom property in its style attribute: style="--value: 74". The data stays in the HTML and the drawing will happen in CSS. Screen readers simply read the list: "Mon 74, Tue 81…"

Stage 1: markup

It is long, but scroll through it once: every part you will style is already there, and each part is something you have built before in a smaller form.

Stage 2: The app shell with grid areas

The overall layout is a job for CSS Grid. grid-template-areas lets you draw the layout as a little text map, then assign each child to a named area with grid-area.

On small screens, the map is a single column: header, then nav, then main. The media query at 36em switches to two columns. The header spans both columns (its name appears twice in the first row), the sidebar takes the narrow left column, and the main content fills the rest. Each row of the map must have the same number of cells, and the extra spaces in "nav main" are only there to line the words up so the map is easy to read.

Two details prevent common grid bugs:

  • minmax(0, 1fr) instead of 1fr. A plain 1fr track can't shrink below the minimum width of its content. The sidebar list will soon be a wide, sideways-scrolling row on phones, and with a plain 1fr it would stretch the whole page. minmax(0, 1fr) lets the track shrink and the list scroll instead (Grid Tracks).
  • grid-template-rows: auto 1fr. The header row is as tall as its content and the second row takes all remaining height. Together with min-height: 100vh on .app, the sidebar always reaches the bottom of the screen.

A temporary dashed outline on each area shows the layout while there is nothing else to see.

Stage 2: grid areas

If your preview is wider than 36em (576 pixels), you see the sidebar on the left; otherwise, the three areas are stacked. Change 36em to 20em or 60em to see the other layout. Notice that moving the sidebar from the top to the left took no changes to the HTML and no grid-area changes either: only the map was redrawn. That is the superpower of Grid Template Areas.

Stage 3: The top bar and sidebar

The outline is gone, and the two navigation areas get their real styles.

The top bar is a flex row (Flexbox Patterns). The brand mark is a ::before pseudo-element with a gradient, just like in the navigation bar project. The search field gets flex: 1 so it takes the free space, but max-width: 20rem stops it from growing too wide. min-width: 0 lets it shrink on very narrow screens, since inputs otherwise refuse to get smaller than their default size. margin-inline-start: auto pushes the search and the avatar to the far end of the bar. Auto margins in flexbox absorb all the free space on one side (Flex Item Properties).

The sidebar is one list styled two ways:

  • On small screens it is a horizontal row of links with overflow-x: auto, so you can swipe through them if they don't fit. white-space: nowrap keeps each label on one line.
  • Inside the 36em media query, the same list switches to flex-direction: column. The border moves from the bottom edge to the inline end (the right side in left-to-right languages, thanks to logical properties).

The current page link uses the accent color on a pale accent background, selected with a[aria-current="page"].

Stage 3: top bar and sidebar

If your preview is narrow, scroll the row of sidebar links sideways. The row scrolls on its own, and the rest of the page stays still, because the grid track was allowed to shrink in Stage 2.

Stage 4: Responsive stat cards

The four key numbers sit in a grid that needs no media query at all:

CSS
grid-template-columns: repeat(auto-fit, minmax(min(100%, 9.5rem), 1fr));

Read it from the inside out. Each column must be at least 9.5rem wide, and 1fr lets the columns share any extra space. auto-fit creates as many columns as fit: four on a wide screen, two on a medium one, one on a very narrow one. The min(100%, 9.5rem) part protects tiny screens: if the container is narrower than 9.5rem, the minimum becomes 100% so nothing overflows (Grid Patterns).

Each card has three lines: a muted label, a big number with tabular-nums, and a trend badge. The badge color comes from its data-trend attribute via attribute selectors: green for up, red for down. A small triangle is added with ::before, and the alternative text syntax content: "β–²" / "Up"; makes screen readers say "Up 12% vs last week" instead of reading out a geometric shape. Older browsers that don't support the slash syntax fall back to the plain content declaration above it.

Stage 4: stat cards

Color is never the only signal here. The arrow points up or down, and the text says "Up" or "Down" to screen readers. People who can't tell red from green still get the message (Accessible CSS).

Stage 5: Panels and a bar chart in pure CSS

The two panels share the auto-fit trick with a larger minimum of 22rem: side by side on wide screens, stacked on narrow ones. Each panel is a white card with a flex header that puts the title on the left and a small label or link on the right.

The bar chart is the fun part. The list becomes a flex row with align-items: flex-end, so every column sits on the same baseline. Each item is a vertical flex column holding three things:

  1. The bar is the item's ::before pseudo-element. Its height is calc(var(--value) / var(--max) * var(--scale)). A value of 70 with a --max of 140 gives half of --scale, so 4.5rem. The --value comes from the inline style attribute; --max and --scale are set once on the chart.
  2. The number has order: -1, which moves it above the bar.
  3. The day has order: 1, which moves it below the bar.

order changes only the visual position, so screen readers still read the day before the number, which is the more natural order when listening.

The best day has a data-peak attribute and gets the full accent color, while the other bars use the softer --bar token. This draws the eye to the one number that matters.

Stage 5: panels and chart

Change a --value in the HTML, for example Wednesday's from 68 to 130, and the bar grows immediately. To make all bars taller, change --scale. Custom properties used inside calc() like this are what makes CSS-only charts possible (calc(), min(), max() and clamp()).

Stage 6: The orders list and container queries

Here is a problem that media queries can't solve. The orders panel is full width on a tablet but only half width on a big monitor, where it sits next to the chart. Its layout should depend on the space the panel actually has, not on the size of the screen. That is exactly what container queries are for.

Each order is a small grid with named areas. The default, for narrow panels, is a compact two-line layout:

CSS
grid-template-areas:
  "customer total"
  "item     status";

Then .panel becomes a query container with container: panel / inline-size, which gives it the name panel and lets its children ask about its width. Inside @container panel (min-width: 30rem), the order rows switch to a single line with four columns, like a table. The rule is written once and works wherever the panel ends up: full width, half width, or in a sidebar on another page.

The stat cards get container query treatment too. Each .stat is a container, and the number's size is clamp(1.4rem, 13cqi, 2.25rem). The cqi unit is 1% of the container's inline size, so the number grows with its card, not with the screen (Fluid Typography).

Two more details in the list: the item description is cut off with an ellipsis when it doesn't fit (overflow: hidden, white-space: nowrap and text-overflow: ellipsis, see Wrapping, Overflow and Hyphens), and each status pill takes its colors from a data-status attribute.

Stage 6: container queries

To watch the container query switch, add max-width: 26rem; to the .panel rule. The orders panel shrinks below 30rem and drops back to the two-line layout, even though the screen didn't change size at all.

A container can't query its own size, only its descendants can react to it. That's why the query container is the .panel and the rules inside @container style the .order rows within it. Size container queries and cqi units work in all current browsers.

Stage 7: Dark mode

Because every color in the dashboard comes from a token, dark mode is one block of CSS. Inside @media (prefers-color-scheme: dark), the :root tokens get new values, and the whole interface follows: page, cards, text, borders, bars, badges and status pills (Dark Mode).

A good dark theme is more than inverted colors:

  • Surfaces get lighter as they rise. The page is the darkest color, cards are a step lighter, and inputs a step lighter again. Shadows are hard to see on dark backgrounds, so --shadow becomes none and the lighter surfaces do the work.
  • Text is off-white, not pure white, which is easier on the eyes in a dark room.
  • Accents get lighter and softer. The light theme's deep blue and green would look dim on a dark background, so the dark theme uses brighter versions. The soft badge backgrounds become semi-transparent tints, so they blend with any surface.

color-scheme: light dark on :root tells the browser that the page supports both themes. The browser then draws built-in parts, such as scrollbars, the search field's clear button and its placeholder text, in the matching style.

Stage 7: dark mode

If your system is set to light mode, you can test the dark theme without changing it. In the browser's DevTools, find the rendering options and emulate prefers-color-scheme: dark. Or, for a quick check, temporarily change (prefers-color-scheme: dark) to (prefers-color-scheme: light) in the code above, which applies the dark tokens on a light system.

Modern browsers also support the light-dark() function, which puts both values side by side in one declaration. It only works when color-scheme is set, and it is a compact alternative to the media query:

CSS
:root {
  color-scheme: light dark;
  --surface: light-dark(#fff, #111827);
  --ink: light-dark(#0f172a, #e2e8f0);
}

See Modern Color for more about light-dark().

Stage 8: The finishing touches

The final stage adds the details that people notice without knowing why.

A sticky sidebar. On wide screens, the sidebar list gets position: sticky; top: 0, so the navigation stays in view while a long page scrolls past (Positioning). The <nav> still stretches the full height of its grid area; only the list inside it sticks.

Visible focus. Every link and the search field get a 3-pixel accent outline on :focus-visible. In a dense interface like a dashboard, keyboard users depend on it (Outlines).

A growing chart. When the page loads, each bar grows up from the baseline. The animation uses transform: scaleY(0) as its starting frame with transform-origin: bottom, so the bar grows from the bottom edge, and it is wrapped in prefers-reduced-motion: no-preference, so it only runs for people who are happy to see motion (Keyframe Animations and Motion and Performance). Animating transform instead of height keeps it smooth, because the browser doesn't have to recalculate the layout on every frame.

Stage 8: the finished dashboard

Press Run to replay the chart animation. Then step back and look at what you built: one HTML file and a single stylesheet with no frameworks, no images and no JavaScript, yet the page responds to the screen size, to the size of each component, to the system theme and to motion preferences.

Ideas to take it further

  • Add a manual theme switch: a checkbox in the top bar plus :root:has(#dark-mode:checked) to apply the dark tokens (selector functions). Remembering the choice between visits needs a little JavaScript.
  • Collapse the sidebar to icons only between 36em and 60em, with the labels hidden accessibly (Accessible CSS).
  • Organize the stylesheet into @layer blocks for tokens, layout and components (Cascade Layers).
  • Add a print stylesheet that hides the navigation and prints the stats and orders cleanly (Print Styles).

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. In grid-template-areas, how does the header span both columns on wide screens?

    A named area covers every cell where its name appears. Writing "header header" makes the area two columns wide.
  2. 2. Why does the shell use minmax(0, 1fr) instead of 1fr?

    A plain 1fr track has an automatic minimum based on its content. With minmax(0, 1fr), the minimum is 0, so wide content scrolls or wraps instead of widening the whole layout.
  3. 3. How does each bar in the chart get its height?

    The data lives in the HTML as a custom property. One CSS rule turns any value into a proportional height.
  4. 4. Why is a container query better than a media query for the orders list?

    The same panel gets different amounts of space in different layouts. A container query lets it respond to the space it actually has.
  5. 5. What does color-scheme: light dark do?

    The color changes come from your tokens. color-scheme lets the browser style its own built-in parts to match, and it is also required for light-dark().

Summary

  • grid-template-areas draws the page shell as a text map; a media query redraws the map without touching the HTML.
  • minmax(0, 1fr) stops wide content from stretching a grid track.
  • repeat(auto-fit, minmax(min(100%, X), 1fr)) builds responsive card grids with no media queries.
  • Custom properties in style attributes plus calc() turn plain lists into bar charts; order changes the visual order only.
  • Media queries are for the page layout; container queries and cqi units are for components that live in different places.
  • With every color in tokens, dark mode is one prefers-color-scheme block plus color-scheme: light dark.
  • Sticky navigation, visible focus and motion that respects prefers-reduced-motion finish the job.

Congratulations, you have built a complete, responsive dashboard! One chapter is left: practice and review. Next: CSS Cheat Sheet, a one-page summary of the course, followed by exercises and interview questions.