nav
and a header
Fog of details ahead!
Do not 'learn off'.
Look-up when needed.
To center text or an image horizontally within a parent element, use
text-align: center;
p {
text-align: center;
}
To center a single element whose display
is block
horizontally within a parent element, set its left and right margin to auto
.
(Note that nothing will happen unless the element is narrower than the parent.)
p {
margin-left: auto;
margin-right: auto;
}
Common shorthand:
p {
margin: 0 auto;
}
The same technique is often used to center the whole web page, e.g.:
html {
background-color: white;
}
body {
width: 80%;
margin: 0 auto;
background-color: green;
}
To center several elements elements horizontally within a parent element, use a flexbox and set justify-content
.
.parent {
display: flex;
flex-direction:row;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
gap: 1em;
}
.child {
flex: 1;
}
nav
<nav>
<a href="">Wombats</a>
<a href="badgers.html">Badgers</a>
<a href="squirrels.html">Squirrels</a>
</nav>
nav {
background-color: gray;
padding: 1em;
}
nav a {
color: black;
background-color: white;
border: 1px solid black;
padding: 1em;
text-align: center;
text-decoration: none;
}
We see a horizontal menu. Why?
What is a one-line way to make it a stacked menu?
nav
flex-direction: row
to column
to get a stacked menu. But swap the values of justify-content
/align-items
. Why?
nav {
background-color: gray;
padding: 1em;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 1em;
}
nav a {
color: black;
background-color: white;
border: 1px solid black;
padding: 1em;
text-align: center;
text-decoration: none;
flex: 1;
}
nav
nav a[href=""]
{
color: rgb(200 200 200);
}
nav a:hover
{
background-color: rgb(200 200 200);
}
header
1
<header>
<img src="web.png" alt="Web Dev logo"
width="128" height="128" />
<h1>Web Development
</header>
header {
border: 1px solid black;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-items: center;
gap: 2em;
}
header h1 {
font-size: 2em;
}
header
2
<header>
<h1>Web Development
<nav>
<a href="">Wombats</a>
<a href="badgers.html">Badgers</a>
<a href="squirrels.html">Squirrels</a>
</nav>
</header>
header {
border: 1px solid black;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 2em;
}
header h1 {
font-size: 2em;
}
nav {
padding: 1em;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 1em;
}
nav a {
color: black;
background-color: white;
border: 1px solid black;
padding: 1em;
text-align: center;
text-decoration: none;
flex: 1;
}
nav a[href=""]
{
color: rgb(200 200 200);
}
nav a:hover
{
background-color: rgb(200 200 200);
}
header
3
<header>
<img src="web.png" alt="Web Dev logo"
width="128 height=128" />
<h1>Web Development
<nav>
<a href="">Wombats</a>
<a href="badgers.html">Badgers</a>
<a href="squirrels.html">Squirrels</a>
</nav>
</header>
header {
border: 1px solid black;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-items: center;
gap: 2em;
}
header h1 {
font-size: 2em;
}
nav {
margin-left: auto;
padding: 1em;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 1em;
}
nav a {
color: black;
background-color: white;
border: 1px solid black;
padding: 1em;
text-align: center;
text-decoration: none;
flex: 1;
}
nav a[href=""]
{
color: rgb(200 200 200);
}
nav a:hover
{
background-color: rgb(200 200 200);
}
<body>
<header>
…
</header>
<nav>
…
</nav>
<main>
…
</main>
<footer>
…
</footer>
<body>
CSS flow layout takes care of this!
<body>
<header>
…
</header>
<nav>
…
</nav>
<main>
…
</main>
<footer>
…
</footer>
<body>
This can be done using either flexbox or grid
<body>
<header>
…
</header>
<nav>
…
</nav>
<main>
…
</main>
<footer>
…
</footer>
<body>
body {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-items: center;
}
header {
width: 100%;
}
nav {
width: 25%;
}
main {
width: 75%;
}
footer {
width: 100%;
}
<body>
<header>
…
</header>
<nav>
…
</nav>
<main>
…
</main>
<footer>
…
</footer>
</body>
<body>
<header>
…
</header>
<nav>
…
</nav>
<main>
…
</main>
<footer>
…
</footer>
<body>
body {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-areas: "top top"
"middle-left middle-right"
"bottom bottom"
}
header {
grid-area: top;
}
nav {
grid-area: middle-left;
}
main {
grid-area: middle-right;
}
footer {
grid-area: bottom;
}