Sub topic 2
Remove default list styling
This chapter covers the second step of the navigation bar: stripping the browser's default list styles so the links can be laid out and styled as a bar. It assumes you can write basic HTML elements and attributes, use CSS selectors and properties, and read the box model (margin, padding, border).
Why the defaults get in the way
The previous chapter wrapped the links in a nav landmark that contains a ul. Browsers give a ul a built-in look through their user-agent stylesheet, which is the set of default rules every browser applies before your CSS. For a ul the defaults are roughly:
list-style-type: disc, which draws a bullet before eachli.padding-leftof about 40px, which indents the list.- Top and bottom margins of about 1em on the
ul.
These suit a bulleted list in an article. In a navigation bar they add bullets, a left gap and extra vertical space that you would otherwise have to work around.
Here is the markup from the previous chapter again:
<nav aria-label="Main">
<ul class="nav-list" id="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>Without any CSS, this renders as an indented, bulleted vertical list.
Reset the list
Three declarations remove the defaults. Put them on a class instead of the bare ul selector, so other lists on the page, such as bulleted lists in articles, keep their normal look.
.nav-list {
margin: 0;
padding: 0;
list-style: none;
}What each line does:
| Declaration | Effect |
|---|---|
| `margin: 0` | Removes the space above and below the list. |
| `padding: 0` | Removes the left indent that browsers use to make room for bullets. |
| `list-style: none` | Removes the bullets. |
Each li still has display: list-item, but with list-style: none no marker is drawn. The li elements are still list items in the document, which is what you want.
Add the background
The finished bar uses a dark background. You can set it on the same rule now, so the next chapters show white link text against something visible:
.nav-list {
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}The ul is a block element, so the background spans the full width of its container. That already looks like a bar, but the links are still stacked vertically. Laying them out in a row is the next chapter's job.
Check the result
After the reset, the list should look like this:
- No bullets appear next to the links.
- The links start flush with the left edge of the list, with no 40px indent.
- No gap appears above the first link or below the last one.
If a gap remains, open your browser's developer tools and inspect the ul. The Computed tab shows which rule set margin and padding. A common cause is a more specific selector, such as nav ul, elsewhere in your stylesheet that overrides the single-class rule.
Common errors
- Resetting only
list-style. The bullets disappear but the 40px indent stays, so the bar looks shifted right. Resetpaddingas well. - Resetting
ulglobally. A rule likeul { list-style: none; }also removes bullets from every article list on the site. Scope the reset to.nav-list. - Targeting
lifor the bullets. The marker belongs to the list item, solist-styleworks on either theulor theli. Setting it on theulis simpler because the value is inherited by everyli. - Forgetting that
marginandpaddingare on theul. Setting them on thelielements does nothing to the outer gap.
What changes in the document
Only the presentation changes. The HTML keeps its meaning, so screen readers and search engines still see a list of links inside a navigation landmark.
Exercise
A teammate wrote .nav-list { list-style: none; } and reports that the bullets are gone but the links still sit about 40px from the left edge. Which declaration is missing, and why does it fix the problem?
Show answer
padding: 0 is missing. Browsers indent a ul with left padding to make room for the bullets. Removing the bullets does not remove the padding, so it has to be reset separately. Adding margin: 0 as well removes the space above and below the list.
Exercise
Why is it better to write the reset on .nav-list than on ul?
Show answer
A rule on ul applies to every unordered list on the page, including bulleted lists in article content, and would remove their bullets and indent. A class limits the reset to the navigation list.