HTML Integration with Bootstrap
By the end of this lesson you will be able to put Bootstrap's grid and components on semantic HTML, write the ARIA attributes its JavaScript expects, connect validation messages to fields properly and re-theme components with CSS variables.
What you should know first: Accessible Navigation Systems, Accessible Modal & Dialog Systems, Accessible Form Error Handling and HTML Integration with Tailwind CSS. This lesson is about the HTML. Bootstrap's classes are taught in the Bootstrap 5 tutorial. The version used is Bootstrap 5.3.8.
Bootstrap is classes plus a contract
Bootstrap has two parts. The CSS is a set of class names such as container, row, col-md-8, btn and card. The JavaScript adds behaviour to components such as the navbar toggler, dropdowns and modals.
The JavaScript works like a contract. Bootstrap promises to open menus, trap focus in modals and update some ARIA states. In return, you promise to give it the right starting markup: the right element, the right id links and the right aria-* attributes. If your side of the contract is wrong, the component may still look right, but keyboard and screen reader users get a broken page.
Think of an Indian Railways reservation form. The clerk processes the ticket, but only if you fill in the train number, date and names in the right boxes. Bootstrap is the clerk.
The grid on semantic elements
The grid classes do not care which element they are on. A col-md-8 can be a <div>, a <main> or an <article>. So use the element that matches the content and add the class to it, instead of wrapping everything in extra <div> elements:
<div class="container">
<div class="row">
<main class="col-md-8" id="main">
<h1>Kochi Spice Market</h1>
<ul class="row list-unstyled g-3" role="list">
<li class="col-sm-6"><article class="card card-body"><h2 class="h5">Black pepper</h2></article></li>
<li class="col-sm-6"><article class="card card-body"><h2 class="h5">Cardamom</h2></article></li>
</ul>
</main>
<aside class="col-md-4" aria-labelledby="offers-title">
<h2 id="offers-title">Today's offers</h2>
</aside>
</div>
</div>Three details matter here. The cards are list items because they are a list of products. The list-unstyled class removes bullets, which is why role="list" is added, for the Safari VoiceOver reason explained in the Tailwind lesson. And the card titles are <h2> elements made smaller with the h5 class: the class sets the look, the element sets the level in the outline. Never pick <h5> just because you want small text.
data-bs-* attributes: behaviour without writing JavaScript
Bootstrap's data API reads data-bs-* attributes from your HTML and connects components for you. You write no JavaScript for the common cases:
| Attribute | Meaning |
|---|---|
data-bs-toggle="collapse", "dropdown", "modal" | Which component this button controls |
data-bs-target="#mainNav" | Which element it controls, as a CSS selector |
data-bs-dismiss="modal" | This button closes the surrounding modal |
data-bs-theme="dark" | Use the dark colour mode on this element and its children |
These are normal data-* attributes from Custom Data Attributes, so they are valid HTML. Older code from Bootstrap 4 uses data-toggle and data-target without bs. Those do nothing in Bootstrap 5, and this is a common reason why a copied navbar never opens. When you need control from code, the same components have a JavaScript API, such as bootstrap.Modal.getOrCreateInstance(element).show(), covered in JavaScript API.
The ARIA Bootstrap expects from you
Bootstrap's documentation lists the attributes each component needs. Here is what you write and what Bootstrap's JavaScript does with it:
| Component | You write | Bootstrap's JavaScript |
|---|---|---|
| Navbar toggler | <button> with aria-controls, aria-expanded="false" and aria-label | Changes aria-expanded when the menu opens or closes |
| Dropdown | <button> with data-bs-toggle="dropdown" and aria-expanded="false" | Changes aria-expanded, supports arrow keys and Esc |
| Modal | tabindex="-1", aria-labelledby (the title's id), aria-hidden="true" | Adds role="dialog" and aria-modal="true", removes aria-hidden, keeps focus inside |
| Close button | <button class="btn-close" aria-label="Close"> | Nothing: the label is yours |
Two warnings. First, Bootstrap's dropdown is not an ARIA menu. Its documentation says it does not add role="menu" because a dropdown can contain anything, even a form. For a list of navigation links that is right: links in a list are already understood. Do not add role="menu" to navigation. Second, Bootstrap 5.3 builds modals from a <div>, not the native <dialog> element you used in Dialog Element, so the ARIA in your markup is doing real work.
Live coding: navbar, dropdown and modal
Kabir is building a page for a music school in Jaipur. Every interactive element below is a real <button>, and a small log shows the ARIA changes Bootstrap makes.
Step 1: a skip link with visually-hidden-focusable, which is Bootstrap's class for "hidden until focused".
Step 2: a <nav aria-label="Main"> navbar. The toggler and the "Courses" dropdown are buttons with aria-expanded="false", and the current page link has aria-current="page".
Step 3: a modal with aria-labelledby pointing at its title, and a close button with aria-label.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Jaipur Music School</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<a class="visually-hidden-focusable" href="#main">Skip to main content</a>
<header>
<nav class="navbar navbar-expand-md bg-body-tertiary" aria-label="Main">
<div class="container">
<a class="navbar-brand" href="#">Jaipur Music School</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mainNav"
aria-controls="mainNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="mainNav">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Home</a></li>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" type="button" id="coursesBtn"
data-bs-toggle="dropdown" aria-expanded="false">Courses</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Tabla</a></li>
<li><a class="dropdown-item" href="#">Sitar</a></li>
<li><a class="dropdown-item" href="#">Vocal</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
</header>
<main id="main" class="container py-4">
<h1>Learn classical music</h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#trial">Book a free trial</button>
<h2 class="h6 mt-4">ARIA changes made by Bootstrap</h2>
<ol id="log" class="small"></ol>
</main>
<div class="modal fade" id="trial" tabindex="-1" aria-labelledby="trial-title" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title fs-5" id="trial-title">Book a free trial</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Trial classes run every Saturday at 11 am in Malviya Nagar.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Not now</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"></script>
<script>
const log = document.querySelector("#log");
new MutationObserver((changes) => {
for (const change of changes) {
const el = change.target;
const li = document.createElement("li");
li.textContent = "#" + el.id + ": " + change.attributeName + " = " +
(el.getAttribute(change.attributeName) ?? "(removed)");
log.append(li);
}
}).observe(document.body, {
subtree: true, attributes: true,
attributeFilter: ["aria-expanded", "aria-modal", "aria-hidden", "role"]
});
</script>
</body>
</html>Open "Courses", then press Esc. Click "Book a free trial", then "Not now". The log shows #coursesBtn: aria-expanded = true, then false. For the modal it shows aria-hidden = (removed), aria-modal = true, role = dialog on open, and the reverse on close. Bootstrap updated every state, but only because the starting attributes and id links were correct. Narrow the preview to see the toggler change too.
A common mistake in copied navbars is <a class="nav-link dropdown-toggle" href="#"> for the dropdown trigger. It opens the menu, but it announces itself as a link that goes nowhere. A <button type="button"> says what it really does.
Validation: native rules, Bootstrap styles
Bootstrap does not validate anything. The browser does that with required, type, pattern and minlength, as you learned in Constraint Validation API. Bootstrap only styles the result:
novalidateon the<form>stops the browser's own bubbles, so Bootstrap's messages show instead.- Adding
was-validatedto the form makes:invalidfields red and shows each sibling.invalid-feedback. - For errors found by the server, add
is-invalidto the field yourself.
Bootstrap does not link the message to the field, and does not set aria-invalid. Without these, a screen reader user hears a red field with no explanation. A field's aria-describedby is read even when the message is hidden, so connect each message only while it applies.
Step 1: native rules on each input, and a .invalid-feedback element with an id after it.
Step 2: on submit, if the form is invalid, stop it, add was-validated, and set aria-invalid and aria-describedby on each field from its validity state.
Step 3: theme the button with Bootstrap's own CSS variables.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admission enquiry</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
:root { --bs-primary-rgb: 15, 118, 110; }
.btn-primary {
--bs-btn-bg: #0f766e;
--bs-btn-border-color: #0f766e;
--bs-btn-hover-bg: #115e59;
--bs-btn-hover-border-color: #115e59;
}
</style>
</head>
<body>
<main class="container py-4" style="max-width: 32rem">
<h1 class="h3 text-primary">Lucknow Science Academy</h1>
<form id="enquiry" novalidate>
<div class="mb-3">
<label for="name" class="form-label">Student name</label>
<input id="name" class="form-control" required autocomplete="name">
<div id="name-error" class="invalid-feedback">Enter the student's name.</div>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Mobile number</label>
<input id="phone" class="form-control" type="tel" required pattern="[6-9][0-9]{9}" autocomplete="tel">
<div id="phone-error" class="invalid-feedback">Enter a 10-digit mobile number starting with 6, 7, 8 or 9.</div>
</div>
<button class="btn btn-primary">Send enquiry</button>
<p id="status" class="mt-3" role="status"></p>
</form>
</main>
<script>
const form = document.querySelector("#enquiry");
form.addEventListener("submit", (event) => {
event.preventDefault();
form.classList.add("was-validated");
for (const field of form.querySelectorAll("input")) {
const bad = !field.validity.valid;
field.setAttribute("aria-invalid", String(bad));
if (bad) field.setAttribute("aria-describedby", field.id + "-error");
else field.removeAttribute("aria-describedby");
}
const first = form.querySelector(":invalid");
if (first) first.focus();
else document.querySelector("#status").textContent = "Thank you. We will call you within two days.";
});
</script>
</body>
</html>Press "Send enquiry" with empty fields: both turn red, the messages appear and focus moves to "Student name". Type a name and 98765 as the number and press it again. Only the phone field stays red, and now only it has aria-describedby. The heading is teal through --bs-primary-rgb, the button through --bs-btn-bg.
The real setup
The CDN links are fine for learning and small sites. Pin the version, as above, and add Subresource Integrity from Subresource Integrity (SRI) when you use a CDN in production. In a Vite project you install the package and import only what you use:
npm install bootstrap@5.3.8 @popperjs/core// src/main.js
import "bootstrap/dist/css/bootstrap.min.css";
import { Collapse, Dropdown, Modal } from "bootstrap";Importing the classes is enough for the data-bs-* attributes to work. Get Started: CDN, npm & Vite covers Sass and smaller builds.
Common mistakes
- Bootstrap 4 attributes in Bootstrap 5.
data-togglemust bedata-bs-toggle. - Links as dropdown or modal triggers. Use
<button type="button">for actions. - Choosing heading levels by size. Use the right
<h2>and make it smaller with a class such ash5. - Icon-only close buttons with no name. Keep
aria-label="Close"on.btn-close. role="menu"on navigation dropdowns. Bootstrap does not provide the keyboard model that role promises.- Validation messages not linked to fields. Set
aria-invalidandaria-describedbyyourself.
Interview questions
1. What does data-bs-toggle="modal" data-bs-target="#trial" do?
It uses Bootstrap's data API. When the button is clicked, Bootstrap finds the element matching #trial and opens it as a modal, with no JavaScript written by you. The attributes are ordinary data-* attributes, so the HTML stays valid.
2. Which ARIA attributes do you write on a Bootstrap modal, and which does Bootstrap add?
You write tabindex="-1", aria-labelledby pointing at the modal title, and aria-hidden="true", plus aria-label on the close button. When the modal opens, Bootstrap's JavaScript adds role="dialog" and aria-modal="true", removes aria-hidden and manages focus.
3. Why does Bootstrap not add role="menu" to dropdowns?
ARIA menus are for application-style menus of actions and allow only menu items inside. Bootstrap dropdowns can hold links, forms or text, so the documentation leaves menu roles to the author. For navigation links, a plain list of links inside a dropdown is correct.
4. What is the difference between visually-hidden and visually-hidden-focusable?
visually-hidden always hides content visually while keeping it for screen readers. visually-hidden-focusable hides it only until it, or something inside it, receives focus. That makes it the right class for skip links.
5. How do Bootstrap validation classes relate to native HTML validation?
The browser checks the rules from attributes such as required and pattern. Bootstrap's was-validated class styles fields that match :invalid or :valid, and is-invalid lets you mark server errors. Linking messages with aria-describedby and setting aria-invalid is still your job.
Key points
- Put grid and component classes on semantic elements; the class sets the look, the element sets the meaning.
data-bs-*attributes connect components without JavaScript; Bootstrap 4'sdata-toggledoes nothing in Bootstrap 5.- Write the starting ARIA (
aria-expanded,aria-controls,aria-labelledby,aria-label); Bootstrap updates states from there. - Use
<button type="button">for toggles, dropdowns and modal triggers. - Bootstrap only styles validation; native attributes check, and you link messages with
aria-describedby. - Theme components through their own CSS variables, such as
--bs-btn-bg, not only the global--bs-primary-rgb.
Exercise
Ananya copied this Bootstrap 4 snippet into a Bootstrap 5.3 site in Rohtak: <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#fees">Fee details</a> and <div class="modal fade" id="fees"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">Fee structure</h5><button class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body">...</div></div></div></div>. The modal never opens. Fix the trigger, add every attribute the modal and close button need, and choose a sensible heading level.
Show solution
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#fees">Fee details</button>
<div class="modal fade" id="fees" tabindex="-1" aria-labelledby="fees-title" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title fs-5" id="fees-title">Fee structure</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">...</div>
</div>
</div>
</div>The trigger is now a button using Bootstrap 5's data-bs-toggle and data-bs-target, so the modal opens. The modal gets tabindex="-1", aria-labelledby linked to the title and aria-hidden="true". The close button gets type="button" and a name. The title is an <h2> (a dialog starts its own section) made smaller with fs-5.
Quick Quiz
1. Why does data-toggle="collapse" do nothing in Bootstrap 5.3?
Bootstrap 5 added the bs namespace to all data API attributes.
2. What does Bootstrap's JavaScript add to a .modal when it opens?
You still write aria-labelledby; Bootstrap adds the role and modal state.
3. Which class is right for a skip link?
It hides the link until it receives keyboard focus.
4. You set --bs-primary-rgb to teal but .btn-primary stays blue. Why?
Override the button's component variables or rebuild with Sass.
Learn HTML with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
