

This Bootstrap offcanvas sidebar uses one navigation element in two layouts: an overlay below 992 pixels and a persistent sidebar on larger screens. The example keeps Bootstrap’s focus and Escape behavior, adds a nested section and marks the current destination explicitly.
Open the working preview · Compare the related templates and components
Build the example
- Save index.html and open it through a local server. Bootstrap 5.3.8 CSS and its JavaScript bundle load from the pinned CDN URLs shown in the source.
- Connect the opener to the sidebar with data-bs-target and aria-controls. Give the panel a labelled heading and a close button with an accessible name.
- Use the offcanvas-lg class for the breakpoint. The same links become persistent navigation on desktop; no duplicate hidden menu needs to be maintained.
- Use a disclosure button for the Reports section, keeping aria-expanded and aria-controls in sync through Bootstrap Collapse. A dropdown or disclosure is an action, not a link to a fake URL.
- When a section link is selected on mobile, close the panel and move focus to the chosen heading after the hide event. Escape keeps the normal return to the menu opener.
Behavior and limits
- The sample links navigate to actual sections of the example page. Replace them with application routes and set aria-current for the real current route.
- A visible or hidden navigation link does not enforce a permission. Server-side authorization remains necessary for protected application sections.
- The sidebar depends on the listed Bootstrap bundle for its overlay, focus handling and nested collapse. Do not label this version CSS-only.
Complete source files
Save the complete HTML file below. Its styles and behavior are included; the offcanvas example additionally loads its pinned Bootstrap files. Copy only the relevant elements, styles and script when integrating with a larger template.
Complete index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Accessible Bootstrap offcanvas sidebar — uiCookies example</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
/>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font:
17px/1.6 system-ui,
sans-serif;
color: #24343f;
background: #eff3f5;
}
a {
color: #175474;
text-underline-offset: 3px;
}
button,
input,
select {
font: inherit;
}
button,
select {
min-height: 44px;
}
button {
cursor: pointer;
}
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
[tabindex]:focus-visible {
outline: 3px solid #b54c12;
outline-offset: 3px;
}
.app-header {
background: #173e52;
color: white;
padding: 20px 24px;
display: flex;
justify-content: space-between;
align-items: center;
}
.app-header p {
margin: 0;
}
.app-header button {
background: white;
color: #173e52;
border: 0;
padding: 8px 16px;
border-radius: 4px;
}
.layout {
display: flex;
max-width: 1300px;
margin: auto;
}
.offcanvas-lg {
--bs-offcanvas-width: 300px;
background: white !important;
}
.offcanvas-body {
display: block !important;
padding: 24px;
}
.side-nav a,
.side-nav button {
display: block;
width: 100%;
padding: 10px 14px;
margin: 4px 0;
border: 0;
background: none;
text-align: left;
border-radius: 4px;
color: #263e4b;
font-size: 17px;
text-decoration: none;
}
.side-nav a[aria-current="page"] {
background: #e5eff4;
font-weight: 700;
}
.side-nav ul {
list-style: none;
padding-left: 16px;
}
.content {
min-width: 0;
flex: 1;
padding: 30px;
}
.content section {
background: white;
border: 1px solid #ccd6dc;
padding: 24px;
margin: 0 0 24px;
scroll-margin-top: 20px;
}
.content h1 {
font-size: 34px;
}
.content h2 {
font-size: 25px;
}
.eyebrow {
font-size: 14px;
font-weight: 700;
}
.side-nav button[aria-expanded="true"] {
font-weight: 700;
}
@media (min-width: 992px) {
.offcanvas-lg {
width: 300px;
flex-shrink: 0;
min-height: calc(100vh - 84px);
}
}
@media (max-width: 600px) {
.content {
padding: 20px;
}
}
</style>
</head>
<body>
<header class="app-header">
<p>FIELD NOTES / BOOTSTRAP EXAMPLE</p>
<button
class="d-lg-none"
type="button"
data-bs-toggle="offcanvas"
data-bs-target="#sidebar"
aria-controls="sidebar"
>
Open menu
</button>
</header>
<div class="layout">
<div
class="offcanvas-lg offcanvas-start"
tabindex="-1"
id="sidebar"
aria-labelledby="sidebar-title"
>
<div class="offcanvas-header d-lg-none">
<h2 class="offcanvas-title fs-5" id="sidebar-title">
Example navigation
</h2>
<button
type="button"
class="btn-close"
data-bs-dismiss="offcanvas"
data-bs-target="#sidebar"
aria-label="Close navigation"
></button>
</div>
<div class="offcanvas-body">
<nav class="side-nav" aria-label="Application sections">
<a href="#overview" aria-current="page">Overview</a
><a href="#orders">Orders</a
><button
type="button"
data-bs-toggle="collapse"
data-bs-target="#report-links"
aria-expanded="false"
aria-controls="report-links"
>
Reports +
</button>
<ul class="collapse" id="report-links">
<li><a href="#sales">Sales report</a></li>
<li><a href="#returns">Returns report</a></li>
</ul>
<a href="#settings">Settings</a>
</nav>
</div>
</div>
<main class="content">
<p class="eyebrow">SAMPLE INTERFACE · NO CUSTOMER RECORDS</p>
<h1>One sidebar, two screen sizes.</h1>
<p>
Below 992 pixels the sidebar is an offcanvas panel. On larger screens
it becomes persistent navigation. Open it on a phone, press Tab, close
it with Escape, and check that focus returns.
</p>
<section id="overview" aria-labelledby="overview-title">
<h2 id="overview-title" tabindex="-1">Overview</h2>
<p>
A landing area for application sections. The links navigate to real
sections on this example page.
</p>
</section>
<section id="orders" aria-labelledby="orders-title">
<h2 id="orders-title" tabindex="-1">Orders</h2>
<p>
Use real loading, empty and error states when connecting this area
to an API.
</p>
</section>
<section id="sales" aria-labelledby="sales-title">
<h2 id="sales-title" tabindex="-1">Sales report</h2>
<p>
This nested destination is controlled by a labelled disclosure
button.
</p>
</section>
<section id="returns" aria-labelledby="returns-title">
<h2 id="returns-title" tabindex="-1">Returns report</h2>
<p>The active destination is marked with aria-current.</p>
</section>
<section id="settings" aria-labelledby="settings-title">
<h2 id="settings-title" tabindex="-1">Settings</h2>
<p>
Authentication and server-side permissions are outside this
navigation example.
</p>
</section>
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
const sidebar = document.querySelector("#sidebar");
let destination = null;
document.querySelectorAll(".side-nav a").forEach((a) =>
a.addEventListener("click", () => {
document
.querySelectorAll(".side-nav a")
.forEach((x) => x.removeAttribute("aria-current"));
a.setAttribute("aria-current", "page");
if (innerWidth < 992) {
destination = document.querySelector(a.hash + "-title");
bootstrap.Offcanvas.getInstance(sidebar)?.hide();
}
}),
);
sidebar.addEventListener("hidden.bs.offcanvas", () => {
if (destination) {
const heading = destination;
destination = null;
queueMicrotask(() => heading.focus());
}
});
</script>
</body>
</html>
What was tested
- Narrow and desktop layouts with no page overflow.
- Opening the panel, keyboard focus inside it, nested disclosure and Escape.
- The panel closes and focus returns to the opener after Escape.
The example interface was checked at 320, 390 and 1440 pixels with keyboard and automated accessibility checks. Backend examples also have isolated server tests; the form and RSVP tests use a test mail transport.


