Emmanuel Akinfulubi

HTML and CSSPractical II

Push the last nav item right with flexbox in CSS

20 A navbar with one item pushed to the end

easy

Put a brand and three links on the left of a bar and a sign-in link hard against the right, without wrapping anything in an extra container.

Stuck? Three hints, smallest first
  1. 1. margin-left: auto on the item you want pushed. The auto margin eats all the free space.
  2. 2. This beats space-between when the split is not simply first and last.
  3. 3. Use a real <nav> with a list, and keep a visible focus style on the links.

index.html

<nav class="bar" aria-label="Main">
  <a class="brand" href="#home">Jibowu Park</a>
  <ul class="links">
    <li><a href="#routes">Routes</a></li>
    <li><a href="#fares">Fares</a></li>
    <li><a href="#help">Help</a></li>
    <li class="end"><a href="#signin">Sign in</a></li>
  </ul>
</nav>

style.css

.bar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 14px;
  background: #17171a;
  border-radius: 8px;
}

.links {
  display: flex;
  align-items: center;
  gap: 14px;
  flex: 1;              /* so the list fills the bar and has space to give away */
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Everything before this stays put; this one is pushed to the far end. */
.end {
  margin-left: auto;
}

.bar a {
  color: #ffffff;
  text-decoration: none;
  font-size: 0.88rem;
}

.brand { font-weight: 700; }

.bar a:hover { text-decoration: underline; }

.bar a:focus-visible {
  outline: 2px solid #ffd75e;
  outline-offset: 3px;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 20

test('the bar is a flex row', /\.bar\s*\{[^}]*display:\s*flex/.test(css), true);
test('the last item is pushed by an auto margin', /\.end\s*\{[^}]*margin-left:\s*auto/.test(css), true);
test('space-between was not needed', /justify-content/.test(css), false);
test('the list itself takes the spare width', /\.links\s*\{[^}]*flex:\s*1/.test(css), true);
test('a real nav landmark, and it is named', /<nav class="bar" aria-label="Main">/.test(html), true);
test('the links are a list', hasTag(html, 'ul') && countTag(html, 'li') === 4, true);
test('five links including the brand', countTag(html, 'a'), 5);
test('focus is visible against the dark bar', /:focus-visible/.test(css) && /#ffd75e/.test(css), true);

Output


      

margin-left: auto is the tool for “push this one and everything after it to the end”, and unlike space-between it does not care where the item sits in the list — you can move the split by moving one declaration. Two details in the markup: aria-label on the <nav> matters once a page has more than one navigation landmark, otherwise a screen reader announces two identical “navigation” regions; and the yellow focus ring is chosen because the default dark ring would be invisible on a near-black bar.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.