Section 37: Project 2 — Responsive Admin Dashboard
Build a responsive dashboard with a sidebar, header, statistic cards, and a recent-orders table. This example uses CSS only; interactive menu behavior would require HTML controls with suitable behavior or JavaScript.
Complete runnable project
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin Dashboard</title>
<style>
:root { --sidebar: #172033; --primary: #2563eb; --surface: #fff; --page: #f1f5f9; --text: #172033; --muted: #64748b; }
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; background: var(--page); color: var(--text); }
.dashboard { min-height: 100vh; display: grid; grid-template-columns: 230px minmax(0, 1fr); }
.sidebar { padding: 1.5rem 1rem; background: var(--sidebar); color: white; }
.sidebar h2 { margin-top: 0; }
.sidebar a { display: block; padding: .7rem; margin-block: .25rem; border-radius: .4rem; color: #e2e8f0; text-decoration: none; }
.sidebar a:hover, .sidebar a:focus-visible { background: #334155; outline-offset: 2px; }
.content { min-width: 0; padding: clamp(1rem, 3vw, 2rem); }
.topbar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem; margin-bottom: 1.5rem; }
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1rem; }
.stat, .table-panel { background: var(--surface); border-radius: .75rem; padding: 1.25rem; }
.stat p { margin-bottom: 0; color: var(--muted); }
.stat strong { font-size: 1.6rem; }
.table-panel { margin-top: 1.5rem; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; text-align: left; }
th, td { padding: .8rem; border-bottom: 1px solid #e2e8f0; white-space: nowrap; }
th { background: #f8fafc; }
.status { display: inline-block; padding: .2rem .55rem; border-radius: 999px; background: #dcfce7; color: #166534; }
@media (max-width: 1000px) {
.stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 700px) {
.dashboard { grid-template-columns: 1fr; }
.sidebar { padding: .75rem; }
.sidebar nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.sidebar a { padding: .5rem; }
}
@media (max-width: 420px) {
.stats { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<div class="dashboard">
<aside class="sidebar">
<h2>AdminPanel</h2>
<nav aria-label="Dashboard navigation">
<a href="#overview">Overview</a>
<a href="#orders">Orders</a>
<a href="#customers">Customers</a>
<a href="#reports">Reports</a>
</nav>
</aside>
<main class="content" id="overview">
<header class="topbar">
<div><h1>Dashboard</h1><p>Your business at a glance</p></div>
<span>Administrator</span>
</header>
<section class="stats" aria-label="Business statistics">
<article class="stat"><p>Revenue</p><strong>₹82,500</strong></article>
<article class="stat"><p>Orders</p><strong>248</strong></article>
<article class="stat"><p>Customers</p><strong>1,204</strong></article>
<article class="stat"><p>Pending</p><strong>12</strong></article>
</section>
<section class="table-panel" id="orders" aria-labelledby="orders-title">
<h2 id="orders-title">Recent orders</h2>
<div class="table-scroll">
<table>
<thead><tr><th>Order</th><th>Customer</th><th>Amount</th><th>Status</th></tr></thead>
<tbody>
<tr><td>#1001</td><td>Asha</td><td>₹1,200</td><td><span class="status">Complete</span></td></tr>
<tr><td>#1002</td><td>Ravi</td><td>₹850</td><td><span class="status">Complete</span></td></tr>
<tr><td>#1003</td><td>Meera</td><td>₹2,400</td><td>Processing</td></tr>
</tbody>
</table>
</div>
</section>
</main>
</div>
</body>
</html>Key concepts
- minmax(0, 1fr): allows the main dashboard column and statistic columns to shrink instead of being forced wider by content.
- min-width: 0: permits a grid or flex item to become narrower than its intrinsic content size.
- overflow-x: auto: allows the table to scroll horizontally on narrow screens rather than forcing the entire page to overflow.
- Reusable cards: the same .stat component styles all four statistics.
Practice exercise
Add a search form, a primary action button, a mobile-friendly menu control, and a recent-activity section. If you add a menu toggle, implement its actual behavior and accessible state rather than using a decorative button only.
Knowledge check: 10 MCQs
- Which layout manages the dashboard shell? Answer: CSS Grid. Explanation: It creates sidebar and content columns.
- Why use minmax(0, 1fr)? Answer: To allow a track to shrink below its intrinsic content size. Explanation: This helps prevent overflow from long content.
- What does min-width: 0 do for a grid item? Answer: Allows it to shrink below its automatic minimum size. Explanation: This is useful when child content is wider than the available space.
- Why use overflow-x: auto on the table wrapper? Answer: To allow horizontal scrolling when needed. Explanation: It avoids forcing the entire page to become wider.
- Why reuse a .stat class? Answer: To share consistent card styling. Explanation: One rule can style many statistic cards.
- Which element represents the main page content? Answer: main. Explanation: It identifies the dominant content region.
- Why use a nav label? Answer: To distinguish navigation landmarks when needed. Explanation: An accessible name can clarify a navigation region's purpose.
- What is a responsive breakpoint? Answer: A condition at which styles adapt. Explanation: A media query can change columns at a suitable viewport width.
- Does CSS alone make every button functional? Answer: No. Explanation: Actions and state changes require working HTML behavior or scripting as appropriate.
- What should be checked on a mobile dashboard? Answer: Navigation, content overflow, tables, and touch usability. Explanation: All critical tasks must remain usable on small screens.