Sub topic 2
Remove default list styling
This chapter covers the browser's built-in styles for lists and how to remove them so a plain <ul> can become a navigation bar. It assumes you know basic HTML elements and attributes, basic CSS selectors and properties, and the box model (margin, padding, border). You should also have the semantic markup from the previous chapter: a <nav> that contains a <ul> of <li> items, each holding an <a>.
Why the list looks the way it does
Every browser ships with a default stylesheet, called the user-agent stylesheet. It gives each element a baseline appearance before any of your CSS runs. For lists, that baseline is designed for reading text, not for building menus.
For a <ul>, the typical defaults are:
- A block margin above and below the list, usually
1em. - Inline-start padding of about
40px, which leaves room for the bullets. - A
list-style-typeofdisc, which draws a bullet before each item.
Each <li> has display: list-item. That is the display value that creates the bullet, which CSS calls a marker.
Here is the markup from the previous chapter, with no author CSS applied:
<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>It renders as a vertical bulleted list, indented from the left edge, with blue underlined links. A navigation bar needs none of that. The next two chapters rebuild the layout and the link boxes, so this chapter only removes what gets in the way.
The three declarations
Three declarations on the list remove the defaults:
.nav-list {
margin: 0;
padding: 0;
list-style: none;
}Each one handles a different default.
margin: 0
This removes the space above and below the list. Without it, a gap appears between the top of the page and the bar, and between the bar and the content below. If your bar has a background color, that gap shows the page background instead of the bar color, which looks like a bug.
padding: 0
This removes the indent that makes room for bullets. The shorthand sets all four sides. The browser's indent comes from the logical property padding-inline-start, which maps to the left side in left-to-right languages and the right side in right-to-left ones. padding: 0 also resets that logical side, because the shorthand covers every physical side, and the cascade resolves the physical and logical values together. You do not need to write the logical version.
list-style: none
This removes the markers. list-style is a shorthand for three properties:
| Property | What it controls | Default for a `ul` |
|---|---|---|
| `list-style-type` | The marker shape or numbering | `disc` |
| `list-style-position` | Whether the marker sits outside or inside the item box | `outside` |
| `list-style-image` | An image used as the marker | `none` |
Writing list-style: none sets the type and the image to none, and resets the position to outside. Setting only list-style-type: none also hides the bullet, but it leaves any list-style-image in place. The shorthand is the safer choice for a reset.
Why you reset the list and not the items
Place the reset on the <ul>, not on each <li>. Two things make that work.
First, margin and padding that cause the visible gap and indent belong to the <ul>. Setting them on the <li> elements would leave the list's own indent in place.
Second, list-style is an inherited property. Set on the <ul>, it passes down to every <li> inside, so you do not repeat it. The same inheritance applies to any nested list inside those items. That matters later in this tutorial, because a dropdown submenu is a nested <ul>. It inherits list-style: none from the outer list, so it needs no marker reset of its own. It does need its own margin and padding reset, because those properties are not inherited. The dropdown chapter handles that.
Target the list with a class
The examples here use a class selector, .nav-list. You could write nav ul instead, but a class is usually better for a few reasons:
- A class selector has higher specificity than a type selector such as
ul, so it wins over broad rules likeul { padding-left: 1.5rem; }that a content stylesheet might contain. - It keeps the reset from hitting other lists inside the
<nav>. Anav ulrule would also affect any nested list, and you may want a nested list to look different. - It matches the markup you already have, since the
<ul>carriesclass="nav-list".
The opposite approach is a global reset such as ul { list-style: none; } for the whole site. That is a common choice in CSS resets, but it has a cost. Lists in articles lose their bullets, and you must restore them with a more specific rule. For a tutorial site or any page with long-form text, scope the reset to the navigation.
Checking the result in the browser
You can confirm that the reset took effect by reading the computed style, which is the final value the browser used after resolving the cascade. Open the developer console on the page and run:
const list = document.querySelector('.nav-list');
const style = getComputedStyle(list);
console.log('margin-top:', style.marginTop);
console.log('padding-left:', style.paddingLeft);
console.log('list-style-type:', style.listStyleType);With the reset applied, the output should be 0px, 0px, and none. Before the reset it is roughly 16px, 40px, and disc. The element inspector in your browser's developer tools shows the same information. It also marks the crossed-out declarations that your rule overrode, which helps when a default seems to persist.
A reset you can run
The following page combines the markup and the reset. Save it as an .html file and open it in a browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>List reset</title>
<style>
.nav-list {
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}
.nav-list a {
color: #ffffff;
}
</style>
</head>
<body>
<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>
</body>
</html>The items are still stacked vertically, because lists are block-level. What changed is that the bullets are gone, the left indent is gone, and the dark background now reaches the edges of the list with no gap above or below. The links still carry their default underline. The link chapter removes the underline and adds padding. The next chapter turns the stack into a row.
Edge cases and gotchas
Screen readers and list semantics
Some browsers drop list semantics when list-style: none is applied. Safari with VoiceOver is the best-known case. It treats a list with no visible markers as a plain group of items, so it stops announcing "list, 4 items". The reasoning is that a list without markers is often a layout device, not a real list.
For a navigation bar, hearing the item count is useful, so many authors restore the semantics explicitly:
<ul class="nav-list" role="list">
<li><a href="/">Home</a></li>
<li><a href="/products">Products</a></li>
</ul>An explicit role="list" on a <ul> is redundant according to the HTML specification, and some linters flag it. In this case it is a deliberate workaround for a browser behavior. The behavior has changed between versions, so test with the browsers and screen readers your audience uses. The accessibility chapter returns to this.
Specificity conflicts
If bullets or indentation persist after you add the reset, another rule is winning. Common causes:
- A content stylesheet has a rule such as
.content ul { padding-left: 2rem; }. Its specificity is the same as.nav-list, two selector parts for.content ulagainst one class for.nav-list. Actually,.content ulhas a class plus a type, so it outranks.nav-list. Raise the specificity of your rule, for example withnav .nav-list, or scope the other rule so it does not reach the navigation. - A framework's reset or base styles load after yours. Check the order of your
<link>and<style>elements, since equal specificity is decided by source order. - A typo, such as
list-style: nonw. The browser discards an invalid declaration silently, and the default stays. The inspector shows invalid declarations with a warning icon.
Margin versus padding when the reset seems to do nothing
If the page shows a gap above the bar after the reset, check the <body> too. Browsers give <body> a default margin of 8px. That is separate from the list, and removing it requires body { margin: 0; }. Many pages add this line next to the list reset.
Ordered lists
The same three declarations work on <ol>. The numbering comes from list-style-type: decimal, and list-style: none removes it. For a breadcrumb trail, an <ol> is the right element because the order matters, and it needs the same reset.
Do not remove the list for visual reasons only
The reset changes how the list looks. It should not change the HTML. Keep the <ul> and <li> elements instead of replacing them with <div> elements. The list structure gives assistive technology a count and a grouping, and it gives you free, consistent styling hooks.
Summary of the reset
| Declaration | Removes | Inherited by children? |
|---|---|---|
| `margin: 0` | Space above and below the list | No |
| `padding: 0` | Left indent reserved for markers | No |
| `list-style: none` | Bullets, numbers, and marker images | Yes |
Exercise
A teammate adds list-style: none to the .nav-list rule and finds that the nested <ul class="submenu"> inside it has no bullets either, but still sits indented from the left. Why does one default disappear while the other stays?
Show answer
list-style is an inherited property, so the nested list picks up none from the outer list. padding is not inherited, and the browser's default indent applies to every <ul> separately. The submenu needs its own margin: 0 and padding: 0.
Exercise
After adding .nav-list { margin: 0; padding: 0; list-style: none; }, bullets still show on the list. The page also contains the rule nav ul { list-style: disc; }. Which rule wins, and how can you fix it without using !important?
Show answer
nav ul has two type selectors, which gives it a specificity of (0,0,2). .nav-list has one class, which gives (0,1,0). The class outranks the two type selectors, so .nav-list wins and the bullets should not show. If bullets do show, the cause is elsewhere, such as a later rule with higher specificity, or an invalid declaration. Use the inspector to find the crossed-out rule. To raise the specificity of the reset, write nav .nav-list { list-style: none; }, which gives (0,1,1).
Exercise
Why does a reset on the <ul> remove the gap above the bar, while the same margin: 0 on each <li> would not?
Show answer
The gap is the margin of the <ul> itself. Setting margin: 0 on the <li> elements changes the space around the items, not around the list container, so the list's own margin stays.