You already built a navbar with the logo on the left and the menu on the right in the CSS lessons: display: flex plus justify-content: space-between on the parent. In Tailwind the recipe is exactly the same. The difference is that you write the Flexbox as classes in the HTML.
Each class holds one line of CSS: flex means display: flex, justify-between means justify-content: space-between. It's like spice sachets: instead of measuring salt, sugar and pepper one by one, you open one sachet that already holds the right mix. A Tailwind class is the sachet, and what's inside is CSS you already know.
Navbar: Logo on the left, Menu on the right
This is the same navbar as in the Lab and the exercise:
<div id="nav" class="flex items-center justify-between h-24 p-4">
<span>Logo</span>
<span>Menu</span>
</div>
| Class | CSS it generates | Meaning |
|---|---|---|
flex |
display: flex; |
Flexbox is on, Logo and Menu become flex items |
items-center |
align-items: center; |
the items sit in the middle of the navbar's height |
justify-between |
justify-content: space-between; |
the first item sits at the left edge, the last at the right edge |
h-24 |
height: 6rem; |
96px tall |
p-4 |
padding: 1rem; |
16px of inner space on every side |
- Every class goes on
#nav, the parent. The two<span>s inside are arranged without any class of their own. h-24is only there so the effect ofitems-centershows. With no extra height, the navbar is as tall as its content, so the top and the middle are almost the same place.- Remember Tailwind's number scale: 1 step = 0.25rem = 4px. So
p-4= 16px andh-24= 96px.
The result: Logo at the left edge, Menu at the right edge, and both in the middle of the navbar's height. Try it in the Lab: pick justify-start, and Menu moves next to Logo.
Without flex, the rest does nothing
justify-* and items-* set properties of a flex container, which is an element with display: flex. If #nav has no flex, #nav is just a plain <div> (display: block), and the browser ignores justify-content and align-items there. So <div class="justify-between"> without flex does nothing: Logo and Menu stay together on the left, like plain text.
So always start with flex, then add justify-*, items-* and gap-*.
Direction: flex-row and flex-col
flex-row (flex-direction: row) is the default value: items line up from left to right, so you don't need to write it. flex-col (flex-direction: column) lines the items up from top to bottom, one below the other.
<div id="nav" class="flex flex-col justify-between h-24 p-4">
<span>Logo</span>
<span>Menu</span>
</div>
In flex-col, the main axis stands upright. justify-between now puts Logo at the top and Menu at the bottom, and items-* controls left and right. The practical rule: justify-* always follows the direction the items line up in (the main axis), and items-* always runs across it (the cross axis).
Main axis (justify-) |
CSS | Cross axis (items-) |
CSS |
|---|---|---|---|
justify-start |
flex-start |
items-stretch (default) |
stretch |
justify-center |
center |
items-start |
flex-start |
justify-end |
flex-end |
items-center |
center |
justify-between |
space-between |
items-end |
flex-end |
justify-around, justify-evenly |
space-around, space-evenly |
items-baseline |
baseline |
Space between items: gap
gap-4 generates gap: 1rem, so there are 16px between every pair of items, with no space before the first item or after the last. gap-2 = 8px, gap-8 = 32px. With justify-between the gap rarely shows, because the leftover space is already spread into the gaps between the items. The gap shows with justify-start or justify-center.
📌 Set one item at a time
The classes above go on the parent. There are classes for a single item too:
flex-1(flex: 1): that item fills all the leftover space. For example<span class="flex-1">Menu</span>makes Menu stretch to the right edge.shrink-0(flex-shrink: 0): the item may not shrink even when space is tight, good for a logo. Old tutorials writeflex-shrink-0. v4 removed that name, so writeshrink-0.flex-wrap(on the parent): items that don't fit move to a new line instead of shrinking.
⚠️ The navbar won't line up?
- Forgetting
flex.justify-between items-centerwithoutflexdoes nothing. The exercise checksdisplay: flexon#nav, soflexhas to be there. - Classes on a child, not the parent.
<span class="flex justify-between">Logo</span>doesn't put Logo and Menu in a row. Put the classes on#nav. - Made-up class names.
justify-space-between,items-middle,flex-columnandalign-centerdon't exist. Tailwind shows no error. The class just generates no CSS. The right ones arejustify-between,items-center,flex-col. - Mixing up
justifyanditems. Do you want the items in the middle of a horizontal navbar's height? That'sitems-center, notjustify-center.
TL;DR: Each Tailwind Flexbox class is one line of CSS you already know: flex = display: flex, items-center = align-items: center, justify-between = justify-content: space-between, flex-col = flex-direction: column, gap-4 = 16px of space between items. Put them all on the parent, and always start with flex, because without flex, justify-* and items-* are ignored. justify-* follows the direction the items line up in, items-* runs across it. The classic navbar: flex items-center justify-between p-4.
⚡ A navbar in one step
Write:
<div id="nav" class="flex items-center justify-between h-24 p-4">
<span>Logo</span>
<span>Menu</span>
</div>
Meaning: flex = display: flex. items-center puts the items (Logo and Menu) in the middle of the navbar's height, and justify-between puts Logo at the left edge and Menu at the right edge. Every class goes on the parent, #nav.
⚡ Without flex, the rest does nothing
Guess first: does <div class="justify-between"> without flex separate Logo and Menu?
No. Without flex, the div is an ordinary display: block element, so justify-* is ignored. Logo and Menu stay together on the left.
Meaning: Always start with flex, then justify-*, items-* and gap-* (the space between items).
The exercise checks for display: flex on #nav.
⚡ Direction and axes
Write:
<div id="nav" class="flex flex-col justify-between h-24 p-4">
<span>Logo</span>
<span>Menu</span>
</div>
Meaning: flex-col lines the items up from top to bottom, one below the other, so justify-between now puts Logo at the top and Menu at the bottom. justify-* always follows the direction the items line up in, and items-* runs across it.