How to make a breadcrumb using bootstrap
What is a breadcrumb?
A breadcrumb is a visual component that displays the current page's location within a hierarchical structure. For example:
Syntax
We implement the breadcrumb as an ordered list using the <ol> tag within a <nav> tag. The generalized syntax is as follows:
<nav><ol class="breadcrumb"><li class="breadcrumb-item"><a href="#"> Item </a></li><li class="breadcrumb-item active"> Current Item </li><ol></nav>
The ordered list element uses the breadcrumb class, while each item in the list uses the breadcrumb-item class. Each list item also contains a hyperlink to its respective web page.
For the final item in the breadcrumb, we use the active class in addition to the breadcrumb-item class. We don't create a hyperlink for the last item as the user is already on the concerned page. The active class differentiates the styling for this item, making the current page easily identifiable.
Example
Explanation
- Line 4: We'll use a
to load the bootstrap styles.content delivery network (CDN) A network of servers from where users can access resources like CSS files - Line 7: We create a
<nav>tag to contain the breadcrumb. Thearia-labelis set to "breadcrumb" to let assistive technologies, like screen readers. Please know that the web page will display a breadcrumb. - Line 8: We make an ordered list using the
<ol>tag. We use thebreadcrumbclass provided by bootstrap, which provides the necessary styling for the separators. - Lines 9–10: We list each item in a
<li>tag. We hyperlink each item to its respective web page using an<a>tag. - Line 11: We use the
<li>tag to create the last item on our breadcrumb or current page. We include theactiveclass so that the current page is visually discernible. Finally, thearia-currentattribute is set to "page" to indicate that this item refers to the current page.
Note: Click here to learn more about ARIA in HTML.
Change the separator
Bootstrap allows us to choose custom separators. These custom separators can be a string or an
We'll use the custom style property --bs-breadcrumb-divider on the <nav> tag to specify the new separator.
Custom string separator
We can set the separator between breadcrumb items to any string in the following manner:
- Line 7: The
--bs-breadcrumb-dividerstyle property is set to '=>' using inline CSS.
Custom SVG separator
We can also set the separator to a custom SVG element, such as a simple horizontal line. We have placed the SVG code as a separate CSS class for simplicity.
- Lines 5–9: We create a
custom-dividerclass within the<style>tag. In this class, we set the--bs-breadcrumb-dividerproperty to a reference to an SVG element. - Line 12: We apply the newly created class on the
<nav>tag.
Note: Click here to learn more about the HTML SVG element.
Free Resources