Sub topic 1
Building a Navigation Bar with HTML and CSS
Prerequisites
Assume you know basic HTML elements and attributes, CSS selectors and properties, and the box model, including margin, padding, and border.
Start with semantic HTML: nav, ul, li and a
A navigation bar is a group of links that helps users move around a site. Use the nav element as a landmark for this group. Inside it, use an unordered list, ul, because navigation items are a list. Each item belongs in an li element. Use an a element for links that navigate to another URL.
<nav aria-label="Main">
<ul class="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>The aria-label gives the landmark a name for screen reader users. The aria-current="page" attribute identifies the link for the page currently displayed.
Remove default list styling
Browsers add bullets, left padding, and margins to unordered lists. Reset those styles before creating the visual bar.
.nav-list {
margin: 0;
padding: 0;
list-style: none;
}Resetting the list does not remove its meaning. Assistive technology can still interpret the original HTML structure.
Lay the links out in a row with Flexbox
Flexbox is a CSS layout system for arranging items along a row or column. Set display: flex on the list. The list items become flex items, and gap adds consistent space between them.
.nav-list {
display: flex;
gap: 0.25rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}Keep the links as links rather than replacing them with clickable div elements. Later, padding and focus styles can make each link comfortable to use without changing the semantic structure.
Exercise
Which element should contain the navigation links, and why?
Show answer
A ul should contain li elements, with each li containing an a element, because the links form a semantic list of navigation choices.