Reading history

Project: Build a Responsive School Admin Dashboard

8 min read Updated 0 views

Build a class dashboard with CSS Grid, auto-fit stat cards, a pure CSS bar chart, :has(), a container query and automatic dark mode, with the complete code.

In this project you will build a responsive admin dashboard for a school class, the kind of page a teacher might open each morning. It brings together many skills from this series: CSS Grid for the layout, auto-fitting cards, a bar chart drawn with pure CSS, :has(), a container query, transforms, and an automatic dark mode. No JavaScript and no frameworks are needed.

In this project you will practise

  • A sidebar-and-content layout with CSS Grid
  • Responsive stat cards with repeat(auto-fit, minmax())
  • A CSS bar chart using attr() and pseudo-elements
  • :has(), a container query and hover transforms
  • Theming with variables and prefers-color-scheme

This project uses CSS Grid, container queries, :has() and dark mode.

The Complete Page

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>School Dashboard</title>
  <style>
    :root {
      color-scheme: light dark;
      --bg: #f4f6fb; --panel: #ffffff; --text: #1f2933; --muted: #6b7280; --accent: #2563eb; --line: #e5e7eb;
    }
    @media (prefers-color-scheme: dark) {
      :root { --bg: #0b1220; --panel: #111a2e; --text: #e5e7eb; --muted: #9ca3af; --accent: #60a5fa; --line: #1f2a44; }
    }
    * { box-sizing: border-box; }
    body { margin: 0; font-family: system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }
    .app { display: grid; grid-template-columns: 180px 1fr; min-height: 100vh; }
    .side { background: var(--panel); border-right: 1px solid var(--line); padding: 14px; }
    .side h2 { font-size: 16px; margin: 0 0 12px; }
    .side a { display: block; padding: 6px 8px; border-radius: 6px; color: var(--text); text-decoration: none; }
    .side a:hover, .side a[aria-current] { background: var(--bg); color: var(--accent); }
    main { padding: 16px; display: grid; gap: 14px; align-content: start; }
    .stats { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
    .stat { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px;
            transition: transform 0.2s; }
    .stat:hover { transform: translateY(-3px); }
    .stat b { display: block; font-size: 22px; }
    .stat span { color: var(--muted); }
    .panels { display: grid; gap: 14px; grid-template-columns: 2fr 1fr; }
    .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px;
             container-type: inline-size; }
    .panel h3 { margin: 0 0 8px; font-size: 15px; }
    .bars { display: flex; align-items: end; gap: 8px; height: 110px; margin-bottom: 20px; }
    .bars div { flex: 1; background: var(--accent); border-radius: 4px 4px 0 0; position: relative; }
    .bars div::after { content: attr(data-day); position: absolute; bottom: -18px; left: 0; right: 0;
                       text-align: center; font-size: 11px; color: var(--muted); }
    .list { list-style: none; margin: 0; padding: 0; }
    .list li { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--line); }
    .list li:has(.late) { color: #dc2626; }
    @container (max-width: 220px) { .list li { flex-direction: column; } }
    @media (max-width: 700px) {
      .app { grid-template-columns: 1fr; }
      .side { border-right: 0; border-bottom: 1px solid var(--line); }
      .side nav { display: flex; gap: 4px; overflow-x: auto; }
      .panels { grid-template-columns: 1fr; }
    }
  </style>
</head>
<body>
  <div class="app">
    <aside class="side">
      <h2>Class 9 – A</h2>
      <nav>
        <a href="#" aria-current="page">Overview</a>
        <a href="#">Students</a>
        <a href="#">Attendance</a>
        <a href="#">Results</a>
      </nav>
    </aside>
    <main>
      <section class="stats" aria-label="Summary">
        <div class="stat"><b>42</b><span>Students</span></div>
        <div class="stat"><b>39</b><span>Present today</span></div>
        <div class="stat"><b>93%</b><span>Attendance</span></div>
        <div class="stat"><b>4.21</b><span>Average GPA</span></div>
      </section>
      <section class="panels">
        <div class="panel">
          <h3>Attendance this week</h3>
          <div class="bars" role="img" aria-label="Attendance: Sun 95%, Mon 90%, Tue 93%, Wed 88%, Thu 97%">
            <div data-day="Sun" style="height:95%"></div>
            <div data-day="Mon" style="height:90%"></div>
            <div data-day="Tue" style="height:93%"></div>
            <div data-day="Wed" style="height:88%"></div>
            <div data-day="Thu" style="height:97%"></div>
          </div>
        </div>
        <div class="panel">
          <h3>Homework</h3>
          <ul class="list">
            <li><span>Maths: Ex 4.2</span><span>Done</span></li>
            <li><span>Biology: Cell diagram</span><span class="late">Late</span></li>
            <li><span>English: Essay</span><span>Due Sun</span></li>
          </ul>
        </div>
      </section>
    </main>
  </div>
</body>
</html>

Result

Save the code as dashboard.html and open it in a browser. Make the window narrow to see the mobile layout, and switch your device to dark mode to see the dark theme.

Step 1: The Layout

  • .app is a grid with two columns: a fixed 180px sidebar and a flexible 1fr main area. min-height: 100vh makes it fill the screen.
  • The sidebar uses an <aside> and a <nav>, so assistive technology recognises it as navigation. aria-current="page" marks the active link, and the same attribute is used in CSS to highlight it.
  • main is also a grid, with gap for spacing and align-content: start so its sections stay at the top.

Step 2: Stat Cards

  • grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) fits as many 140px-or-wider cards as the space allows: four in a row on a laptop, two on a phone, with no media query.
  • Each card shows a big number in <b> with display: block and a muted label. The attendance figure is 39 of 42 students, about 93%.
  • transform: translateY(-3px) on hover lifts the card slightly, with a transition for smoothness.

Step 3: A CSS Bar Chart

  • .bars is a flex row with a fixed height of 110px and align-items: end, so the bars grow upwards from the bottom.
  • Each bar’s inline style="height:95%" sets its value. In a real app, the server or JavaScript would write these numbers.
  • ::after with content: attr(data-day) prints each bar’s label below it, read straight from the HTML attribute. The bottom margin on .bars leaves room for these labels.
  • A chart made of plain boxes means nothing to a screen reader, so the container has role="img" and an aria-label listing all the values.

Step 4: Modern CSS Touches

  • .list li:has(.late) turns a homework row red when it contains a “Late” tag. Change another row’s status to <span class="late"> and it turns red too.
  • Each .panel is a query container. If a panel becomes narrower than 220px, @container (max-width: 220px) stacks each homework row’s text vertically.
  • The media query at 700px changes the page itself: one column, the sidebar on top, and its links in a horizontal scrolling row.
  • All colours come from variables, and a single prefers-color-scheme: dark block swaps them for the dark theme.

Ideas to Extend the Project

  1. Add a manual dark-mode toggle, using the method from the previous lesson.
  2. Add a “Students” table with a sticky header, using what you learned about tables and position: sticky.
  3. Show the attendance percentage when a bar is hovered, using ::before and a data-value attribute.
  4. Run a Lighthouse audit and fix any accessibility or performance warnings.

Common Mistakes

  1. Using fixed widths for cards, which leaves gaps or overflow on other screens.
  2. Forgetting text alternatives for the chart, so screen reader users miss the data.
  3. Hard-coding colours inside components, which breaks the dark theme.
  4. Relying on colour alone for “Late”. The word itself is still shown, which keeps it clear for colour-blind users.

Practice Exercises

Exercise 1. Add a fifth stat card showing “3 Absent”. What else do you need to change in the CSS?

Show answer

Only the HTML: <div class="stat"><b>3</b><span>Absent</span></div>. The auto-fit grid makes room automatically.

Exercise 2. Make the chart bars change colour to orange on hover.

Show answer
.bars div { transition: background 0.2s; }
.bars div:hover { background: #f97316; }

Frequently Asked Questions

Should a real dashboard use a chart library?

For simple bar charts, CSS is light and fast. For line graphs, tooltips, zooming or large data sets, a JavaScript chart library or an SVG chart is a better choice.

Where would the data come from in a real app?

From a server and database. JavaScript would fetch the numbers, usually as JSON, and fill in the cards, bars and list. You will learn how in the JavaScript lessons.

Summary

  • CSS Grid builds the sidebar layout; auto-fit and minmax() make the cards responsive.
  • Flexbox, attr() and ::after create a labelled CSS bar chart.
  • :has(), container queries and media queries each handle a different kind of change.
  • Variables and prefers-color-scheme give the whole dashboard a dark theme.

Next: CSS Logical Properties.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *