Chuyển đến nội dung

Bài 52 · Projects

Project: Admin Dashboard

Build a responsive admin dashboard with Bootstrap 5: an offcanvas sidebar that becomes permanent on desktop, stat cards, a data table, progress bars and tabs.

Dashboards are where Bootstrap earns its keep. They are dense, built from repeated parts (cards, tables, badges) and must work on a wide monitor as well as a phone. In this project you build the shell of an admin panel for an online shop: a sidebar, a row of statistic cards, an orders table, goal bars and a tabbed report area.

The interesting design problem is the sidebar. On a desktop it is always visible; on a phone it must slide in on demand. Bootstrap 5.2 added a responsive offcanvas for exactly this, and you will use it without writing a single line of JavaScript. Review Offcanvas, Tables and Navs and Tabs if any of them feel rusty.

The layout plan

The page has two regions: a fixed-width sidebar and a flexible main area. A top bar inside the main area holds the toggle button for small screens and the page title.

flowchart LR
  subgraph Page
    direction LR
    S["Sidebar<br/>offcanvas-lg<br/>visible from lg"] --- M["Main area"]
  end
  M --> TB["Top bar + toggle<br/>d-lg-none"]
  M --> ST["Stat cards<br/>row-cols"]
  M --> TA["Table, progress, tabs"]

The sidebar is an offcanvas panel below lg and a normal column from lg up.

The page wrapper is d-flex. The sidebar has a fixed width and does not shrink (flex-shrink-0), and the main area uses flex-grow-1 plus a one-line style="min-width: 0" that stops wide tables from forcing the page to overflow.

Step 1: sidebar and stat cards

The key class is offcanvas-lg. Below lg it behaves like an offcanvas panel: hidden, slid in by a button with data-bs-toggle="offcanvas". From lg up it becomes a plain block in the flow, and the header (with its close button) is hidden by d-lg-none. The placement class offcanvas-start decides which side it enters from.

Each stat card is a small card with a label, a large number and a trend badge. They sit in a row row-cols-1 row-cols-sm-2 row-cols-xl-4 g-3, so the number of cards per row adapts to space: one on phones, two on tablets, four on wide screens.

Sidebar and stat cards

View the page in a narrow window (below 992 px) to see the Menu button and open the panel. Two details deserve attention. The dark bg-dark color is set on the panel, so the same markup works as the permanent sidebar and as the slide-in drawer. And the heading and the close button live in offcanvas-header, which is automatically hidden at lg, while a separate d-none d-lg-block title replaces it on desktop.

Why min-width: 0 on the main area? A flex item will not shrink below the width of its content by default. Without the rule, a wide table pushes the whole page sideways instead of scrolling inside its wrapper.

Step 2: table, progress bars and tabs

The lower part of a dashboard shows details. For the orders table, add the table table-hover align-middle classes, and wrap it in table-responsive so it scrolls horizontally on phones rather than breaking the layout. Status values read well as colored badges.

Progress bars show goals. Always add role="progressbar" with aria-valuenow, aria-valuemin and aria-valuemax (Bootstrap's markup needs them for screen readers), and put a text label beside each bar.

Tabs let one area hold several reports without scrolling. The tab buttons use data-bs-toggle="tab" and the panes sit inside tab-content (see Navs and Tabs).

Orders, goals and reports

The grid does the responsive work: col-12 col-xl-8 and col-12 col-xl-4 make the table and the goals card share a row only on extra-large screens, and stack on anything smaller. h-100 makes the neighbors the same height.

Joining the steps

To assemble the final dashboard, put the content of the second example inside the <main> element of the first, below the stat cards, and keep the script tag once at the end. A few finishing touches make it feel like a product:

  • Mark the current page with active and aria-current="page" on the sidebar link.
  • Give the table caption text or a visually-hidden heading so assistive technology can name it.
  • Add text-nowrap to cells whose values should not wrap, such as money amounts.
  • Test at 360, 768, 992 and 1400 px. At 360 px nothing should force a horizontal page scroll.

Real dashboards load numbers from a server. That work happens outside Bootstrap; Bootstrap's job is the frame the numbers sit in.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which class makes a sidebar a slide-in drawer below lg but a normal block from lg up?

    The responsive offcanvas classes (offcanvas-sm to offcanvas-xxl) switch between drawer and static content at that breakpoint.
  2. 2. Why is a table wrapped in table-responsive?

    The wrapper sets overflow-x: auto, so a wide table scrolls inside the wrapper instead of stretching the page.
  3. 3. What does a Bootstrap progress bar need so screen readers can announce it?

    The visual bar is only a styled div; the ARIA attributes carry the value for assistive technology.
  4. 4. How do you show four stat cards per row on xl screens but one on phones?

    row-cols-1 is the mobile default and row-cols-xl-4 raises it to four from the xl breakpoint.

Summary

  • d-flex with a fixed-width offcanvas-lg sidebar and a flex-grow-1 main area gives a classic dashboard frame.
  • A d-lg-none toggle with data-bs-toggle="offcanvas" opens the drawer on small screens; no custom JavaScript is needed.
  • Stat cards use row-cols-*; tables use table-responsive; progress bars need ARIA attributes; tabs organize reports.
  • min-width: 0 on the main flex item prevents wide content from breaking the layout.
  • Use h-100 and breakpoint-specific col-* classes to keep cards aligned and stacked correctly.

Next lesson: Project: Blog Layout.

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.