Skip to content

Commit b39cfdd

Browse files
committed
Fully overhaul centred nav article
1 parent aa79d6b commit b39cfdd

File tree

3 files changed

+91
-199
lines changed

3 files changed

+91
-199
lines changed
Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
comments: true
33
date: 2011-01-29 18:53:10
4-
last_modified_at: 2023-09-29
4+
last_modified_at: 2025-06-04
55
layout: post
66
slug: create-a-centred-horizontal-navigation
77
title: Create a centred horizontal navigation
@@ -12,88 +12,73 @@ tag:
1212
- CSS
1313
---
1414

15-
<p class="c-highlight"><strong>N.B.</strong> This article is kind of old now.
16-
While the technique definitely still works, I’d recommend looking into <a
17-
href="https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox">Flexbox</a>
18-
for this nowadays. Happy coding!</p>
19-
2015
{% include promo.html %}
2116

22-
Centring block level elements is easy, just define a width and set `margin:
23-
0 auto;`, but what if you don’t know that fixed width? You could use
24-
`text-align: center;` but that won’t work on 100%-width block-level elements
25-
either… that’ll only work on text-level elements.
17+
This article was originally written in 2011 and used `text-align`ment and
18+
`display: inline;` to manipulate lists as text-level, inline elements. However,
19+
in 2025, I completely rewrote it to utilise Flexbox: the much more suitable
20+
approach for the times.
2621

27-
Defining explicit widths and heights should always be avoided wherever possible,
28-
as doing so will make the document a lot less future-proof, flexible and
29-
extensible… Suppose you have four items in your navigation menu--you can work
30-
out the width of these and use `margin: 0 auto;` to centre them. Adding a fifth
31-
will increase the width, meaning you’d need to alter the CSS, too. This is far
32-
from ideal, and more so with a CMS to power the site (a client can add pages,
33-
but perhaps can’t edit CSS).
22+
It massively simplified the amount of CSS needed to build a horizontally centred
23+
nav, so while this post may now seem a little underwhelming, it does serve as
24+
a great example of just how powerful CSS has gotten in the last decade.
3425

35-
However, there is a way to have a centred horizontal navigation without knowing
36-
an explicit width, and without adding CSS. It’s also remarkably easy.
26+
```html
27+
<ul class=c-nav>
3728

38-
The markup:
29+
<li class=c-nav__item>
30+
<a href=# class=c-nav__link>Home</a>
31+
</li>
32+
33+
<li class=c-nav__item>
34+
<a href=# class=c-nav__link>About</a>
35+
</li>
36+
37+
<li class=c-nav__item>
38+
<a href=# class=c-nav__link>Work</a>
39+
</li>
40+
41+
<li class=c-nav__item>
42+
<a href=# class=c-nav__link>Clients</a>
43+
</li>
44+
45+
<li class=c-nav__item>
46+
<a href=# class=c-nav__link>Content</a>
47+
</li>
3948

40-
```html
41-
<ul class="nav">
42-
<li><a href="/">Home</a></li>
43-
<li><a href="/about/">About</a></li>
44-
<li><a href="/services/">Work</a></li>
45-
<li><a href="/clients/">Clients</a></li>
46-
<li><a href="/contact/">Contact</a></li>
4749
</ul>
4850
```
4951

50-
Pretty standard, an unordered list of menu items. The CSS is where it’s at.
51-
I have highlighted the bits that do the majority of the work:
52+
Pretty standard, an unordered list of menu items. The CSS is where it’s at:
5253

5354
```css
54-
.nav {
55-
border: 1px solid #ccc;
56-
border-width: 1px 0;
57-
list-style: none;
58-
margin: 0;
59-
padding: 0;
60-
text-align: center; /* « The magic. */
55+
.c-nav {
56+
border: 1px solid #ccc;
57+
border-width: 1px 0;
58+
list-style: none;
59+
margin: 0;
60+
padding: 0;
61+
62+
display: flex;
63+
justify-content: center;
64+
gap: 10px;
6165
}
6266

63-
.nav li {
64-
display: inline; /* « More magic. */
65-
}
67+
.c-nav__item { }
6668

67-
.nav a {
68-
display: inline-block; /* « Last bit of magic. */
69-
padding: 10px;
70-
}
69+
.c-nav__link {
70+
display: block;
71+
}
7172
```
7273

73-
What I’ve done here is simply create a navigation list and given it a border top
74-
and bottom (purely to highlight its centred text). Instead of floating the
75-
_block-level_ `<li>`s left I’ve given them `display: inline;`, that is to say
76-
they no longer occupy 100% the available width and they now stack up nicely
77-
against each other.
74+
The workhorses here are simply `display: flex;` and `justify-content: center;`.
75+
This creates a Flex context and forces items to pack from the centre outward.
7876

79-
Next we use (the much underused) `display: inline-block;` to make sure the links
80-
themselves don’t break onto new lines but still obey any padding values
81-
accordingly. Here I have given them a larger hit area by adding `padding: 10px;`
82-
83-
You could have, if you wanted, applied inline-block to the `<li>`s. however
84-
IE6-7 will only allow `inline-block` to work on elements that are inherently
85-
inline elements. `display: inline-block;` will not work on block-level elements.
77+
`gap` optionally spaces the items by `10px`, which creates an un-clickable
78+
‘dead’ zone between each link. Whether you want this or not is entirely up to
79+
you, so feel free to modify or exclude to suit your needs.
8680

8781
## [Demo](/demos/centred-nav/)
8882

89-
[Here’s a quick demo](/demos/centred-nav/). Try using Firebug or similar to add
90-
other list items on the fly, and watch as they seamlessly centre in the list. I
91-
have tested this in IE7-8 to find it works perfectly. I haven’t checked IE6 but
92-
I imagine it’ll be fine.
93-
94-
## Update
95-
96-
You asked and I heard; I have made [a CSS powered dropdown version of this](/demos/centred-nav/dropdown.html)
97-
for you. The line `top: 100%;` will make the dropdown work in IE7, but kinda
98-
ruins the experience a little in all other browsers. Whether you leave it in or
99-
not is up to you. Again, view source for the how-to…
83+
[Here’s a quick demo](/demos/centred-nav/)! It works in all major current
84+
browsers.

demos/centred-nav/dropdown.html

Lines changed: 0 additions & 112 deletions
This file was deleted.

demos/centred-nav/index.html

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@
1515
<style>
1616

1717
body { width:960px; padding:100px 0; margin:0 auto; font-family:Calibri, sans-serif; }
18-
#nav{
19-
border:1px solid #ccc;
20-
border-width:1px 0;
21-
list-style:none;
22-
margin:0;
23-
padding:0;
24-
text-align:center;
18+
19+
.c-nav {
20+
border: 1px solid #ccc;
21+
border-width: 1px 0;
22+
list-style: none;
23+
margin: 0;
24+
padding: 0;
25+
26+
display: flex;
27+
justify-content: center;
28+
gap: 10px;
2529
}
26-
#nav li{
27-
display:inline;
30+
31+
.c-nav__item {
2832
}
29-
#nav a{
30-
display:inline-block;
31-
padding:10px;
33+
34+
.c-nav__link {
35+
display: block;
36+
padding: 10px;
3237
}
38+
3339
a{
34-
color:#c00;
40+
color:#f43059;
3541
text-decoration:none;
3642
font-weight:bold;
3743
}
@@ -48,18 +54,31 @@
4854

4955
<h1>Create a vertically centred horizontal navigation</h1>
5056

51-
<ul id="nav">
57+
<ul class=c-nav>
58+
59+
<li class=c-nav__item>
60+
<a href=# class=c-nav__link>Home</a>
61+
</li>
62+
63+
<li class=c-nav__item>
64+
<a href=# class=c-nav__link>About</a>
65+
</li>
66+
67+
<li class=c-nav__item>
68+
<a href=# class=c-nav__link>Work</a>
69+
</li>
70+
71+
<li class=c-nav__item>
72+
<a href=# class=c-nav__link>Clients</a>
73+
</li>
5274

53-
<li><a href="#0">Home</a></li>
54-
<li><a href="#0">About</a></li>
55-
<li><a href="#0">Portfolio</a></li>
56-
<li><a href="#0">Archives</a></li>
57-
<li><a href="#0">Resources</a></li>
58-
<li><a href="#0">Contact</a></li>
75+
<li class=c-nav__item>
76+
<a href=# class=c-nav__link>Content</a>
77+
</li>
5978

6079
</ul>
6180

62-
<p><a href="https://csswizardry.com/2011/01/create-a-centred-horizontal-navigation/">Back to article</a> | <a href="dropdown.html">View dropdown version</a></p>
81+
<p><a href="/2011/01/create-a-centred-horizontal-navigation/">Back to article</a></p>
6382

6483
</body>
6584
</html>

0 commit comments

Comments
 (0)