A Navbar With display: flex
A navbar is the classic flex job: logo on the left, links in the middle or right, a button at the end. display: flex puts the children in a row, justify-content: space-between pushes them to opposite ends and align-items: center lines them up vertically.
<nav class="navbar">
<div class="logo">MySite</div>
<div class="menu">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</div>
<a class="btn" href="#">Sign Up</a>
</nav>.navbar {
display: flex;
justify-content: space-between; /* logo left, button right */
align-items: center; /* vertical centering */
padding: 14px 20px;
}
.menu {
display: flex; /* links in a row */
gap: 18px;
}Two flex containers are nested: .navbar lays out three children, and .menu lays out the links inside one of them. Flex only affects direct children, so each level needs its own display: flex.
Centering a Hero With flex-direction: column
By default flex lays items in a row. flex-direction: column stacks them vertically instead, and then align-items: center centers them horizontally. This is the quickest way to build a centered hero.
Learn Web Development
Structured, self-paced lessons for absolute beginners. Start with HTML and build up to full websites.
.hero {
display: flex;
flex-direction: column; /* stack children vertically */
align-items: center; /* center horizontally */
justify-content: center; /* center vertically */
gap: 14px;
min-height: 240px;
text-align: center;
}
.actions {
display: flex; /* two buttons side by side */
gap: 10px;
}Responsive Cards With flex-wrap
Cards in a row that wrap onto new lines on small screens, without a single media query. flex-wrap: wrap lets items drop to the next line, and flex: 1 1 180px means "grow to share space, shrink if needed, but start at about 180px wide". Resize your browser to see them reflow.
.cards {
display: flex;
flex-wrap: wrap; /* allow cards to drop to the next line */
gap: 14px;
}
.card {
flex: 1 1 180px; /* grow | shrink | starting width */
display: flex;
flex-direction: column; /* stack content inside the card */
gap: 8px;
}
.card .btn {
margin-top: auto; /* pushes the button to the bottom */
align-self: flex-start; /* keeps it from stretching full width */
}
/* inline-flex: tags shrink to fit their text */
.tags { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { display: inline-flex; padding: 3px 9px; }The card row is a flex container, and each card is also a flex container (column direction). Using margin-top: auto on the button is a neat trick that pins it to the bottom, so buttons line up even when the text above has different lengths.
Sidebar + Content With flex: 0 0 170px
A fixed-width sidebar next to flexible content is one of the most common page layouts. The sidebar uses flex: 0 0 170px (don't grow, don't shrink, exactly 170px), and the content uses flex: 1 to take all the remaining space.
Welcome back
This is the main content area. It grows to fill all the space the sidebar doesn't use, so if you widen the window only this column gets wider.
On narrow screens the layout switches to a single column using flex-direction: column inside a media query.
.layout {
display: flex;
gap: 18px;
}
.sidebar {
flex: 0 0 170px; /* fixed width, never grows or shrinks */
display: flex;
flex-direction: column; /* links stacked */
gap: 8px;
}
.content {
flex: 1; /* take all remaining space */
min-width: 0; /* lets long content shrink properly */
}
/* Stack on small screens */
@media (max-width: 560px) {
.layout { flex-direction: column; }
.sidebar { flex: none; }
}Buttons With Icons Using inline-flex
When a button or link needs an icon and text lined up neatly, but must still sit inline next to other content, use display: inline-flex. It behaves like inline-block on the outside and like flex on the inside.
★ Favorite ↓ Download ✉ Contact ← all three sit inline with this text
.btn {
display: inline-flex; /* inline outside, flex inside */
align-items: center; /* icon and text vertically aligned */
gap: 6px; /* space between icon and text */
padding: 9px 16px;
}
.icon {
display: inline-flex;
align-items: center;
justify-content: center; /* center the symbol in the circle */
width: 18px;
height: 18px;
border-radius: 50%;
}A Multi-Column Footer
Footers are flex rows of columns. justify-content: space-between spreads the columns out, and flex-wrap: wrap lets them stack on small screens. The bottom copyright bar is a second, simpler flex row.
.footer {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
gap: 20px;
}
.footer .col {
display: flex;
flex-direction: column; /* heading above its list */
}
.footer ul {
display: flex;
flex-direction: column;
gap: 6px;
}
.copyright {
display: flex;
justify-content: space-between;
}Centering Anything in Three Lines
Before flexbox, centering a box both ways was famously painful. Now it takes three declarations.
.box {
display: flex;
justify-content: center; /* horizontal */
align-items: center; /* vertical */
height: 140px; /* the container needs a height */
}Show and Hide With display: none
Menus, dropdowns and accordions all rely on switching between display: none and display: block. This demo does it with pure CSS using a hidden checkbox. Click the button to toggle the panel.
.panel { display: none; } /* hidden, takes no space */
#toggle:checked ~ .panel {
display: block; /* visible when checked */
}You will usually switch the display value with a small piece of JavaScript instead, using a class like .is-open. You will build that in the JavaScript course.
Putting It All Together: A Complete Website
Here is a full one-page website, "BrewBox Coffee", that combines everything from today: flex, inline-flex, grid, display: none on mobile, a sticky header with z-index, and an absolutely positioned badge inside a relative card. The preview below is generated from the exact code underneath it, so what you see is what the code does.
flex: navbar, hero, cards, about section, newsletter, footer. inline-flex: buttons, eyebrow label and tags. grid: the three feature boxes. none: the menu links on small screens. position: sticky header (with z-index) and the absolute "Popular" badge on a relative card.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BrewBox Coffee</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Header / Navbar -->
<header class="header">
<nav class="navbar container">
<a class="logo" href="#">Brew<span>Box</span></a>
<ul class="menu">
<li><a href="#menu">Menu</a></li>
<li><a href="#about">About</a></li>
<li><a href="#order">Contact</a></li>
</ul>
<a class="btn" href="#order">Order Now</a>
</nav>
</header>
<!-- Hero -->
<section class="hero">
<div class="container hero-inner">
<div class="hero-text">
<span class="eyebrow">Freshly roasted daily</span>
<h1>Coffee that starts your day right</h1>
<p>Small-batch beans, honest prices and a warm cup waiting for you. Order online and pick up in minutes.</p>
<div class="actions">
<a class="btn" href="#menu">See the Menu</a>
<a class="btn btn-outline" href="#about">Our Story</a>
</div>
</div>
<div class="hero-card">
<span>Today's special</span>
<strong>☕ Latte</strong>
<span>Only ₹149</span>
</div>
</div>
</section>
<!-- Features (grid) -->
<section class="features container">
<div class="feature">
<h3>🌱 Fresh Beans</h3>
<p>Roasted in small batches every morning for the best flavor.</p>
</div>
<div class="feature">
<h3>⚡ Fast Pickup</h3>
<p>Order ahead and skip the line. Ready in under five minutes.</p>
</div>
<div class="feature">
<h3>💛 Fair Prices</h3>
<p>Great coffee shouldn't be expensive. Simple, honest pricing.</p>
</div>
</section>
<!-- Menu cards (flex-wrap) -->
<section class="section container" id="menu">
<h2>Our Menu</h2>
<p class="lead">Three favorites to get you started.</p>
<div class="cards">
<article class="card">
<span class="badge">Popular</span>
<div class="card-img">☕</div>
<h3>Classic Latte</h3>
<p>Smooth espresso with steamed milk and a light foam top.</p>
<div class="card-bottom">
<span class="price">₹149</span>
<a class="btn" href="#order">Add</a>
</div>
</article>
<article class="card">
<div class="card-img">🧊</div>
<h3>Cold Brew</h3>
<p>Steeped for 18 hours. Bold, smooth and refreshing over ice.</p>
<div class="card-bottom">
<span class="price">₹169</span>
<a class="btn" href="#order">Add</a>
</div>
</article>
<article class="card">
<div class="card-img">🥐</div>
<h3>Butter Croissant</h3>
<p>Flaky, golden and baked fresh every morning in-store.</p>
<div class="card-bottom">
<span class="price">₹99</span>
<a class="btn" href="#order">Add</a>
</div>
</article>
</div>
</section>
<!-- About (two columns) -->
<section class="section container about" id="about">
<div class="about-box">
<span style="font-size:3rem">🫘</span>
<span>Since 2016</span>
</div>
<div class="about-text">
<h2>Our Story</h2>
<p>BrewBox began as a tiny cart with one grinder and a big idea: make a really good cup of coffee easy to find. Today we still source directly from farms and roast every batch ourselves.</p>
<div class="tags">
<span class="tag">Single origin</span>
<span class="tag">Fair trade</span>
<span class="tag">Small batch</span>
</div>
</div>
</section>
<!-- Newsletter -->
<section class="newsletter" id="order">
<div class="container news-inner">
<div>
<h2>Get 10% off your first order</h2>
<p>Join our list for new blends and weekly offers.</p>
</div>
<form class="news-form">
<input type="email" placeholder="Your email address">
<button class="btn" type="button">Subscribe</button>
</form>
</div>
</section>
<!-- Footer -->
<footer class="footer">
<div class="container">
<div class="footer-top">
<div class="footer-col">
<h4>BrewBox Coffee</h4>
<span>Good coffee, good people.</span>
</div>
<div class="footer-col">
<h4>Visit</h4>
<span>12 Market Road</span>
<span>Open daily, 7am – 10pm</span>
</div>
<div class="footer-col">
<h4>Follow</h4>
<span>Instagram</span>
<span>Facebook</span>
</div>
</div>
<div class="footer-bottom">
<span>© 2026 BrewBox Coffee</span>
<span>Built with HTML & CSS</span>
</div>
</div>
</footer>
</body>
</html>/* ---------- Reset & variables ---------- */
:root {
--brand: #e8590c;
--brand-dark: #b8430a;
--soft: #fff1e6;
--ink: #1c1f2e;
--muted: #5d6278;
--line: #f2e2d5;
--bg: #fffaf6;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Inter', Arial, sans-serif;
color: var(--ink);
background: var(--bg);
line-height: 1.6;
}
a { text-decoration: none; color: inherit; }
ul { list-style: none; }
.container {
width: min(1100px, 100% - 40px);
margin-inline: auto;
}
/* ---------- Buttons (inline-flex) ---------- */
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
background: var(--brand);
color: #fff;
padding: 11px 22px;
border-radius: 8px;
font-weight: 600;
border: none;
cursor: pointer;
font-size: 0.95rem;
}
.btn:hover { background: var(--brand-dark); }
.btn-outline {
background: transparent;
color: var(--brand);
border: 2px solid var(--brand);
}
.btn-outline:hover { background: var(--soft); }
/* ---------- Header: sticky + z-index ---------- */
.header {
position: sticky;
top: 0;
z-index: 100; /* stays above everything while scrolling */
background: #fff;
border-bottom: 1px solid var(--line);
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
height: 68px;
}
.logo { font-weight: 700; font-size: 1.3rem; }
.logo span { color: var(--brand); }
.menu {
display: flex;
gap: 28px;
font-weight: 500;
color: var(--muted);
}
.menu a:hover { color: var(--brand); }
/* ---------- Hero ---------- */
.hero {
background: linear-gradient(135deg, #fff1e6, #ffe0c7);
padding: 72px 0;
}
.hero-inner {
display: flex;
align-items: center;
gap: 40px;
}
.hero-text {
flex: 1;
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 16px;
}
.eyebrow {
display: inline-flex;
background: #fff;
color: var(--brand-dark);
font-size: 0.8rem;
font-weight: 600;
padding: 5px 14px;
border-radius: 100px;
}
.hero h1 { font-size: clamp(2rem, 5vw, 3.2rem); line-height: 1.15; }
.hero p { color: var(--muted); max-width: 480px; }
.actions { display: flex; gap: 12px; flex-wrap: wrap; }
.hero-card {
flex: 0 0 280px;
height: 260px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 6px;
background: var(--brand);
color: #fff;
border-radius: 20px;
text-align: center;
}
.hero-card strong { font-size: 2.2rem; }
/* ---------- Features (grid) ---------- */
.features {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
padding: 56px 0 24px;
}
.feature {
background: #fff;
border: 1px solid var(--line);
border-radius: 14px;
padding: 24px;
}
.feature h3 { margin-bottom: 6px; }
.feature p { color: var(--muted); font-size: 0.92rem; }
/* ---------- Sections ---------- */
.section { padding: 48px 0; }
.section h2 { font-size: 1.8rem; margin-bottom: 6px; }
.lead { color: var(--muted); margin-bottom: 28px; }
/* ---------- Menu cards (flex-wrap + absolute badge) ---------- */
.cards { display: flex; flex-wrap: wrap; gap: 20px; }
.card {
position: relative; /* anchor for the absolute badge */
flex: 1 1 240px;
display: flex;
flex-direction: column;
gap: 10px;
background: #fff;
border: 1px solid var(--line);
border-radius: 14px;
padding: 22px;
}
.badge {
position: absolute;
top: 14px;
right: 14px;
z-index: 1;
background: var(--brand);
color: #fff;
font-size: 0.72rem;
font-weight: 700;
padding: 4px 10px;
border-radius: 100px;
}
.card-img {
height: 120px;
border-radius: 10px;
background: var(--soft);
display: flex;
align-items: center;
justify-content: center;
font-size: 2.6rem;
}
.card p { color: var(--muted); font-size: 0.9rem; }
.card-bottom {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: auto; /* pins the price row to the card bottom */
}
.price { font-weight: 700; font-size: 1.2rem; }
/* ---------- About (two columns) ---------- */
.about { display: flex; align-items: center; gap: 40px; }
.about-box {
flex: 0 0 340px;
height: 260px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
background: var(--ink);
color: #cfd3e6;
border-radius: 20px;
}
.about-text { flex: 1; display: flex; flex-direction: column; gap: 12px; }
.about-text p { color: var(--muted); }
.tags { display: flex; gap: 8px; flex-wrap: wrap; }
.tag {
display: inline-flex;
background: var(--soft);
color: var(--brand-dark);
font-size: 0.8rem;
font-weight: 600;
padding: 4px 12px;
border-radius: 100px;
}
/* ---------- Newsletter ---------- */
.newsletter { background: var(--ink); color: #fff; padding: 48px 0; }
.news-inner {
display: flex;
justify-content: space-between;
align-items: center;
gap: 24px;
flex-wrap: wrap;
}
.news-inner p { color: #aab0c6; }
.news-form { display: flex; gap: 10px; flex: 1 1 320px; max-width: 460px; }
.news-form input {
flex: 1;
padding: 12px 14px;
border: none;
border-radius: 8px;
font-size: 0.95rem;
}
/* ---------- Footer ---------- */
.footer { background: #12141c; color: #aab0c6; padding: 40px 0 20px; font-size: 0.9rem; }
.footer-top {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
gap: 30px;
padding-bottom: 28px;
}
.footer h4 { color: #fff; margin-bottom: 4px; }
.footer-col { display: flex; flex-direction: column; gap: 6px; }
.footer-bottom {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
gap: 10px;
padding-top: 16px;
border-top: 1px solid #272a3a;
}
/* ---------- Responsive ---------- */
@media (max-width: 760px) {
.menu { display: none; } /* display: none on small screens */
.hero-inner, .about { flex-direction: column; }
.hero-card, .about-box { flex: none; width: 100%; }
.features { grid-template-columns: 1fr; }
}Download both files (or copy the code into your editor), save them in the same folder as index.html and style.css, then open index.html in your browser. Try changing the colors in :root and watch the whole site update.
Assignment: Build the Full Page
Create layout.html and layout.css and combine all the pieces above into one simple website.
- Build a navbar with
display: flexandjustify-content: space-between. - Add a centered hero section using
flex-direction: column. - Add three cards in a row with
flex-wrap: wrapandflex: 1 1 200px. - Add a sidebar and content area using
flex: 0 0 200pxandflex: 1. - Make two buttons with icons using
display: inline-flex. - Finish with a three-column footer.
- Bonus: resize the browser and use one media query to stack the sidebar layout on small screens.