Keyboard navigation helps people use websites without a mouse. It makes your site easier and friendlier for everyone.
Keyboard navigation support in Bootsrap
Use Bootstrap components that support keyboard by default, like: - <button> with data-bs-toggle attributes - <a> tags with role="button" - tabindex="0" to make elements focusable Example: <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">Open Modal</button>
Bootstrap components like modals, dropdowns, and tabs come with built-in keyboard support.
Use tabindex="0" to make custom elements focusable by keyboard.
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Open Modal</button>
role="button" and tabindex="0".<a href="#" role="button" class="btn btn-secondary" tabindex="0">Clickable Link Button</a>
<ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item" role="presentation"> <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button> </li> <li class="nav-item" role="presentation"> <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button> </li> </ul>
This page has a button and a link styled as a button. You can use the keyboard Tab key to focus them and Enter or Space to activate. The modal that opens also supports keyboard navigation, including closing it with the Escape key or the close button.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Keyboard Navigation Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container py-4"> <h1>Keyboard Navigation Demo</h1> <p>Use Tab to focus buttons and Enter or Space to activate.</p> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal"> Open Modal </button> <a href="#" role="button" class="btn btn-secondary ms-2" tabindex="0"> Link Button </a> <!-- Modal --> <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal Title</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This modal can be opened and closed using keyboard. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Always test keyboard navigation by using the Tab key and Enter/Space keys.
Bootstrap handles most keyboard support for its components, but custom elements need tabindex and ARIA roles.
Use semantic HTML and ARIA attributes to improve accessibility.
Keyboard navigation lets users move through your site without a mouse.
Bootstrap components come with built-in keyboard support.
Use tabindex and ARIA roles for custom interactive elements.
Practice
Solution
Step 1: Understand keyboard navigation
Keyboard navigation means users can move through a website using keys like Tab, Enter, and arrow keys without a mouse.Step 2: Identify Bootstrap's support
Bootstrap components are designed to support keyboard navigation for accessibility and ease of use.Final Answer:
To allow users to navigate the website using only the keyboard -> Option DQuick Check:
Keyboard navigation = navigate site with keyboard [OK]
- Thinking it adds visual effects
- Confusing with color or style changes
- Assuming it disables mouse input
Solution
Step 1: Identify attribute for focus
Thetabindexattribute allows elements to receive keyboard focus and be reachable by Tab key.Step 2: Confirm Bootstrap usage
Bootstrap usestabindexto enable keyboard navigation on custom interactive elements.Final Answer:
tabindex -> Option AQuick Check:
Focusable elements use tabindex [OK]
- Confusing aria-label with focus control
- Using data-toggle which is for JS behavior
- Using role which defines semantics, not focus
<button class="btn btn-primary">Click Me</button>
What happens when you press the Tab key repeatedly on the page?
Solution
Step 1: Understand default button behavior
Native buttons are focusable by default and receive keyboard focus when tabbing.Step 2: Bootstrap styling does not remove focus
Bootstrap styles do not disable keyboard focus or activation on buttons.Final Answer:
The button will receive focus and can be activated with Enter or Space -> Option BQuick Check:
Native buttons are keyboard accessible [OK]
- Thinking Bootstrap disables keyboard focus
- Assuming buttons skip focus by default
- Confusing visual style with behavior
Solution
Step 1: Check focusability of toggle
For keyboard navigation, the dropdown toggle must be focusable, usually withtabindex="0"or a native focusable element.Step 2: Identify common mistakes
Missing tabindex or using tabindex="-1" prevents keyboard focus on toggle, making dropdown unreachable.Final Answer:
Missing tabindex="0" or focusable attribute on the toggle element -> Option CQuick Check:
Focusable toggle needs tabindex=0 [OK]
- Using tabindex=-1 disables focus
- Hiding menu with aria-hidden disables access
- Disabling menu instead of toggle
Solution
Step 1: Make card focusable
Addingtabindex="0"makes the card reachable by keyboard Tab key.Step 2: Define semantic role and label
Usingrole="button"tells assistive tech the card is interactive, andaria-labeldescribes it.Final Answer:
tabindex="0" role="button" aria-label="Card details" -> Option AQuick Check:
Focusable + role=button + aria-label = accessible card [OK]
- Using tabindex=-1 disables keyboard focus
- Using role=presentation hides semantics
- Marking aria-disabled true disables interaction
Ready to prove it?
Pass the test and this pattern moves unknown
