Build a Complete Responsive Web Page Using CSS Flexbox
Quick Answer: Build a complete flexbox page in layers. Start with the viewport meta tag. Make the body a column flex container with min-height: 100vh, and give the main element flex: 1, so the footer stays at the bottom. Make the header a wrapping row with justify-content: space-between. Make every section, such as the hero and the feature cards, a wrapping flex row with a flex-basis and a gap. Center a call-to-action band with justify-content and align-items. Keep the content readable with a container that has max-width and auto side margins, and finish with a footer of wrapping columns. Write the small-screen layout first, and add a media query only when a section really needs it.
S1. Why Do We Need a Complete Page Layout With Flexbox?
Imagine you have built a navigation bar, a card grid and a dashboard, each in its own file. Now a client asks for one real page: a header, an introduction with a picture, three feature cards, a call to action and a footer. It must look right on a phone, a tablet and a wide monitor.
A complete page is where small layouts meet. Each part has its own job, but all the parts must agree on width, spacing and order. The footer must stay at the bottom even when the page is short. The text must not stretch across a very wide screen.
Would you write a new set of fixed widths for every section and hope they all fit together? Every section would break in its own way.
Flexbox solves this. The same few ideas work at every level of the page: direction, wrapping, free space and alignment. Let's first see what a page without them looks like.
S2. The Problem: A Page Built With Inline-Block Columns and a Loose Footer
Inline-block page = a page whose columns use display: inline-block with percentage widths, and whose footer simply follows the content.
Imagine a page that is 420px wide and 240px tall on screen. It has a header, a hero section with a text column and an image column that are 50% wide each, and a footer. The page content is short.
Example 1: An inline-block hero and a footer that follows the content
Assumption: In the output diagrams, one character is about 20px wide and one text row is about 40px tall. H is the header, T the hero text, I the hero image and F the footer. The space between two inline elements is about 4px wide.
<!-- HTML -->
<header class="site-header">Header</header>
<main>
<div class="hero-text">Text</div>
<div class="hero-media">Image</div>
</main>
<footer class="site-footer">Footer</footer>
/* CSS */
.hero-text, .hero-media {
display: inline-block;
width: 50%;
vertical-align: top;
}
Expected output
|HHHHHHHHHHHHHHHHHHHHH| |TTTTTTTTTT | |IIIIIIIIII | |FFFFFFFFFFFFFFFFFFFFF| | | | | We wanted the text and the image side by side. Each column is 50% of 420px, which is 210px. The line break between the two tags becomes a space of about 4px, so the line needs 424px. That is more than 420px, so the image wraps below the text. The footer follows the content at row 4, and rows 5 and 6 of the screen stay empty.
The page renders without any error, and every rule is valid. The columns and the footer simply do not behave as planned.
What goes wrong?
- Hidden whitespace: Inline-block columns are separated by the white space in the HTML, so two 50% columns do not fit on one line.
- Percent tricks: Developers reduce the widths to 49% or remove the whitespace from the HTML, and the fix breaks when someone formats the file again.
- A loose footer: On a short page the footer floats in the middle of the screen.
- Fixed page widths: A page with a fixed width, such as 960px, scrolls sideways on a phone and leaves empty space on a large monitor.
- One-off fixes: The header, the hero, the cards and the footer each use a different layout trick, so nothing can be reused.
Now think about a real page with six sections. Each section would bring its own trick, and the tricks would start to fight each other.
Remember: The problem is not one broken rule. It is that every part of the page is laid out with a different method. A whole page needs one method that works for the page, for the sections and for the parts inside them.
Animation 1: How mixed layout tricks break a page
Different tricks meet a small screen
The red highlights show where the page stops following the plan.
The animation highlights the three causes that make the page unreliable.
S3. The Solution: One Layout Method for the Page, the Sections and the Parts
Flexbox gives us a simpler design, and it lets us use the same few ideas at every level of the page.
page shell = the structure of the whole page: the header, the main content and the footer, arranged in a column that is at least as tall as the screen.
A complete page needs only three ideas.
- The
bodyis a column flex container, and themainelement grows to take the free height. - Every section is a wrapping flex row, so its parts sit side by side when there is room and stack when there is not.
- The parts inside a section can be flex containers too, with their own direction and alignment.
Now the footer stays at the bottom, and the hero columns sit side by side.
Example 2: The page shell with flexbox
Assumption: The screen is 420px wide and 240px tall (6 rows). The header and the footer are 40px tall each, and the hero columns (T and I) are 40px tall.
<!-- HTML -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<header class="site-header">Header</header>
<main class="site-main">
<section class="hero">
<div class="hero-text">Text</div>
<div class="hero-media">Image</div>
</section>
</main>
<footer class="site-footer">Footer</footer>
</body>
/* CSS */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
.site-main { flex: 1; }
.hero {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.hero-text, .hero-media { flex: 1 1 180px; }
Expected output
|HHHHHHHHHHHHHHHHHHHHH| |TTTTTTTTTT IIIIIIIIII| | | | | | | |FFFFFFFFFFFFFFFFFFFFF| Used height = header 40 + footer 40 + hero 40 = 120px, and the screen is 240px tall. The main area has flex: 1, so it takes all the free height, and the footer stays at the bottom. The hero is 40px tall, so the rest of the main area (3 rows) is empty and ready for more sections. Hero: 180 + 20 + 180 = 380px fits in 420px. Each column grows by 20px, to 200px (10 characters).
Notice the difference. We never counted the whitespace, and the footer needs no special rule. The viewport meta tag in the head tells phones to use the real screen width, so the layout can respond to it.
Animation 2: How the flex page is built
Page, sections, parts
Each level uses the same ideas with a different direction.
The animation highlights the three levels of the page and the flexbox feature each one uses.
Compare both approaches
| Mixed tricks | Flexbox page |
|---|---|
| Whitespace between inline-blocks changes the widths. | Flex items ignore the whitespace between them. |
| The footer follows the content. | The footer is pushed down by a growing main. |
| A fixed page width scrolls on phones. | A container uses max-width and fits any screen. |
| Each section needs its own trick. | Every section is a wrapping flex row. |
| Centering needs guessed margins. | justify-content and align-items center it. |
The idea to remember
- The page is a column = header, main and footer stack, and the main area grows to fill the height.
- Sections are wrapping rows = they use
flex-wrap, a basis and a gap, so they adapt without many media queries. - Parts are flex containers too = a header, a card or a footer column can have its own direction.
- Axes swap with the direction = in a column,
justify-contentworks vertically andalign-itemsworks horizontally. - Small screen first = write the stacked layout first, then add a
min-widthquery only when needed.
S4. Step-by-Step Code Breakdown
1. Learn the layers and the properties
Every part of the page has a direction and a main job. The table shows the layout of each part.
| Part | Direction | Main job |
|---|---|---|
Page (body) |
Column | Keeps the footer at the bottom with a growing main |
| Header | Row, wrapping | Puts the logo and the links at opposite ends, and wraps on small screens |
| Hero | Row, wrapping | Puts the text and the image side by side, and stacks them when there is no room |
| Feature cards | Row, wrapping | Shows the cards in a row, and wraps them as the screen narrows |
| Call to action | Column, centered | Centers its text and button in both directions |
| Footer columns | Row, wrapping | Spreads link groups across the width, and wraps them on small screens |
The properties repeat from part to part. The table shows where each one is used on this page.
| Property | Set on | What it does on the page |
|---|---|---|
flex-direction: column and min-height: 100vh |
The body |
Stacks the header, the main area and the footer, at least as tall as the screen |
flex: 1 |
The main element |
Takes the free height, so the footer is pushed to the bottom |
flex-wrap, gap and a flex basis |
Sections and their items | Lets the parts sit side by side or stack, with equal spacing |
justify-content: space-between |
The header and the footer columns | Spreads the groups to the edges of the row |
justify-content: center and align-items: center |
The call to action | Centers the content on both axes |
align-self: flex-start |
A button in a column | Stops the button from stretching across the column |
max-width and margin: 0 auto |
The page container | Limits the line length and centers the content on wide screens |
2. Build the page shell
Example 2 already showed this step. The body is a column flex container with min-height: 100vh, and the main element has flex: 1. The default 8px margin of the body is removed, because it would add space around the whole page. Do not forget the viewport meta tag, because without it many phones show the page at a wide virtual width and shrink it.
3. Build a header that wraps
The header is a row with the logo on one side and the links on the other. Instead of hiding the links behind a button, this header simply lets the links wrap under the logo when there is not enough room. It is the simplest responsive header, and it works well for a few links.
Example 3: A header that wraps on small screens
Assumption: The logo (L) and each link (H for Home, A for About and C for Contact) are about 100px wide and 40px tall. The row gap of 10px is not drawn.
<!-- HTML -->
<header class="site-header">
<a class="logo" href="#">Logo</a>
<nav aria-label="Main">
<ul class="nav-links">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
/* CSS */
.site-header {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 10px 20px;
}
.nav-links {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin: 0;
padding: 0;
list-style: none;
}
.logo, .nav-links a {
display: block;
padding: 10px 20px;
}
Expected output
Screen 620px wide (31 characters): |LLLLL HHHHH AAAAA CCCCC| Screen 420px wide (21 characters): |LLLLL | |HHHHH AAAAA CCCCC | The links need 3 x 100 + 2 x 20 = 340px, and the logo needs 100px, so the header needs 440px. At 620px, the free space is 180px (9 characters), and space-between puts it between the logo and the links. At 420px, the header would need 440px plus a 20px gap, which is 460px, so the links wrap to a second row.
4. Build the hero section
The hero is a row with a text column and a media column, as in Example 2. The text column is also a column flex container. That gives a common surprise: inside a column, an item stretches across the full width, so a button becomes as wide as the text. align-self: flex-start keeps the button at its natural size.
Example 4: A hero with a button that keeps its size
Assumption: The screen is 420px wide, so each hero column is 200px wide, as in Example 2. The title (T), the paragraph (P) and the button (B) are 40px tall each. The button is about 100px wide. I is the media column.
<!-- HTML -->
<section class="hero">
<div class="hero-text">
<h1>Welcome</h1>
<p>A short introduction.</p>
<a class="btn" href="#">Start</a>
</div>
<div class="hero-media">Image</div>
</section>
/* CSS */
.hero {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.hero-text {
flex: 1 1 180px;
display: flex;
flex-direction: column;
}
.hero-media { flex: 1 1 180px; }
.hero-text h1, .hero-text p { margin: 0; }
.btn {
align-self: flex-start;
padding: 10px 20px;
}
Expected output
With align-self: flex-start on the button: |TTTTTTTTTT IIIIIIIIII| |PPPPPPPPPP IIIIIIIIII| |BBBBB IIIIIIIIII| Without it, the button stretches across the whole text column: |TTTTTTTTTT IIIIIIIIII| |PPPPPPPPPP IIIIIIIIII| |BBBBBBBBBB IIIIIIIIII| The text column is 3 rows tall, and the media column stretches to the same height. In a column flex container, the cross axis is horizontal, so align-items: stretch makes the button 200px wide. align-self: flex-start puts the button at the start of the cross axis and keeps it at its own width.
5. Add feature cards and a centered call to action
The feature cards use the same wrapping row as the card layout, with flex: 1 1 180px. The call to action is a column flex container. Because the direction is a column, justify-content: center centers the content vertically, and align-items: center centers it horizontally.
Example 5: Three feature cards and a centered call to action
Assumption: The container is 640px wide (32 characters), and each feature card (F) is 40px tall. The call to action is 160px tall, with a title (C) about 240px wide and a button (B) about 120px wide, each 40px tall.
<!-- HTML -->
<section class="features">
<article class="feature">Feature one</article>
<article class="feature">Feature two</article>
<article class="feature">Feature three</article>
</section>
<section class="cta">
<h2>Ready to start?</h2>
<a class="btn" href="#">Get started</a>
</section>
/* CSS */
.features {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.feature { flex: 1 1 180px; }
.cta {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 160px;
text-align: center;
}
.cta h2 { margin: 0; }
Expected output
Feature cards in a 640px container (32 characters): |FFFFFFFFFF FFFFFFFFFF FFFFFFFFFF| Feature cards in a 420px container (21 characters): |FFFFFFFFFF FFFFFFFFFF| |FFFFFFFFFFFFFFFFFFFFF| Call to action in a 640px container: | | | CCCCCCCCCCCC | | BBBBBB | | | Wide: 3 x 180 + 2 x 20 = 580px, so the free space is 60px, and each card grows by 20px to 200px. Narrow: only two cards fit, and the third card grows across the full row. That is a choice, and you can stop it with flex: 0 0 calc(...) as in the card article. Call to action: the content is 80px tall, so the free height is 80px. justify-content: center puts 40px above and 40px below. align-items: center centers each item horizontally: (640 - 240) / 2 = 200px on each side of the title.
6. Add the footer and a readable container
The footer is a wrapping row of link groups, like the header. Last, a container keeps the content readable on wide screens. It has a max-width and auto side margins, so it fills a phone and stays centered on a big monitor.
Example 6: Footer columns and a centered container
Assumption: The footer content area is 640px wide on large screens and 300px wide on small screens. The brand block (B) is about 160px wide, and the two link groups (L and R) are about 100px wide each. Each block is 40px tall.
<!-- HTML -->
<footer class="site-footer">
<div class="container footer-columns">
<div class="footer-brand">Your Company</div>
<ul><li><a href="#">About</a></li></ul>
<ul><li><a href="#">Help</a></li></ul>
</div>
</footer>
/* CSS */
.container {
box-sizing: border-box;
width: 100%;
max-width: 960px;
margin: 0 auto;
padding: 0 20px;
}
.footer-columns {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 20px;
}
Expected output
Footer in a 640px content area (32 characters): |BBBBBBBB LLLLL RRRRR| Footer in a 300px content area (15 characters): |BBBBBBBB LLLLL| |RRRRR | Wide: the blocks need 160 + 100 + 100 = 360px, so the free space is 280px, and each gap is 140px (7 characters). Narrow: all three would need 400px with the gaps, so the last block wraps to a second row. Container: on a 1400px screen it is 960px wide, and (1400 - 960) / 2 = 220px of margin sits on each side. On a 360px phone, width: 100% makes it fill the screen, and the 20px padding keeps the text off the edges.
S5. Architecture and Real-World Trade-Offs
On a real project, a page is a set of layers that you build, test and change one at a time. When the page shell, the sections and the parts each have a clear flex layout, a new section is just another wrapping row.
How the design fits a project
Imagine a team building a marketing site with many pages. Every page shares the same shell, and each section can be moved, added or removed without touching the others.
Three layers of one page
Each layer has its own flexbox job and does not depend on the others.
The animation traces each layer of the page to the flexbox feature that supports it.
Benefits for larger websites
- Consistency: One layout method means that developers read and change every section the same way.
- Fewer media queries: Wrapping rows and flex bases adapt on their own, so media queries are needed only for real changes.
- Safe growth: A new section is another wrapping row, and the footer stays at the bottom.
- Readable pages: A container with a maximum width keeps lines short on large monitors.
- Accessible structure:
header,nav,mainandfooterare landmarks that assistive technology can jump to.
Important trade-offs
- Flexbox arranges one direction at a time. CSS Grid is often a better fit for the page as a whole when you need named areas or columns that line up across rows, and the two can be used together.
- A complete page needs more than layout, such as image sizes, fonts, loading speed and clear content. Layout alone does not make a page good.
- The
orderproperty can change the visual order of items, but the keyboard and screen readers still follow the HTML order. Prefer to write the HTML in the order you want people to read it. - Wrapping sections are flexible, but the last row can look uneven, as with the third feature card in Example 5. Decide whether that is acceptable.
100vhmay include the browser's address bar on some mobile browsers, and thedvhunit is available in modern browsers. Test on real devices.- Too many nested flex containers become hard to read. Add a container only when it has a layout job.
- Test the page at several widths, at 200% zoom, with long text and with the keyboard, in more than one browser.
Important: Flexbox does not design the page for you. It gives you direction, wrapping, free space and alignment. What goes in each section, and where the page should change, are decisions you make from the content.
S6. Common Mistakes and Best Practices
Mistake 1: Forgetting the viewport meta tag
Wrong: Write a flexible layout and leave out <meta name="viewport" ...> from the head.
Why it fails: Many phones then lay the page out at a wide virtual width, often around 980px, and shrink it to fit. Your media queries and wrapping rows respond to that width, not to the real screen.
Better: Add <meta name="viewport" content="width=device-width, initial-scale=1"> to every page.
Rule: Every responsive page starts with the viewport tag.
Mistake 2: Building columns with inline-block and percentages
Wrong: Give two columns display: inline-block and width: 50%.
Why it fails: The white space between the tags adds a few pixels, so the line is wider than 100% and the second column wraps.
Better: Put both columns in a flex container with gap, and use a flex basis.
Rule: Columns that sit side by side belong in a flex container.
Mistake 3: Giving the page a fixed width
Wrong: Write .container { width: 960px; }.
Why it fails: On a phone the page scrolls sideways, and on a very wide screen the page does not use the space well.
Better: Use width: 100% with max-width: 960px and margin: 0 auto.
Rule: Use max-width for the container, not width.
Mistake 4: Letting the footer follow the content
Wrong: Leave the body as a normal block and put the footer after the content.
Why it fails: On a short page, the footer sits in the middle of the screen with empty space below it.
Better: Make the body a column flex container with min-height: 100vh, and give the main element flex: 1.
Rule: Let the main area grow, and the footer follows it down.
Mistake 5: Forgetting that items stretch in a column
Wrong: Put a button in a column flex container and expect it to keep its natural width.
Why it fails: In a column, the cross axis is horizontal, and the default stretch makes the button as wide as the container.
Better: Add align-self: flex-start to the button, or set align-items: flex-start on the container.
Rule: In a column, align-items works horizontally.
S7. How This Comes Up in Interviews
Question 1: How would you structure a complete responsive page with flexbox?
What the interviewer wants: They want to hear the layers and what each one does, not a list of properties.
Strong answer:
"I start with the viewport meta tag. The body is a column flex container with min-height: 100vh, and the main element has flex: 1, so the footer stays at the bottom. Each section is a wrapping flex row with a gap and a flex basis, so it adapts without many media queries. A container with a max-width centers the content on wide screens, and I use a media query only where a section needs a real change."
Question 2: How do you keep the footer at the bottom of a short page?
Strong answer:
"I make the body a column flex container with min-height: 100vh, and give the main content flex: 1. The main area takes all the free height, so the footer is pushed to the bottom. When the content is long, the page just grows."
Question 3: What changes for justify-content and align-items when flex-direction is column?
Strong answer:
"The axes swap. justify-content works along the main axis, which is vertical in a column. align-items works along the cross axis, which is horizontal. So in a column, justify-content: center centers vertically, and align-items: center centers horizontally. A button in a column also stretches by default, so I use align-self: flex-start to keep its width."
Question 4: How do you center an element both horizontally and vertically with flexbox?
Strong answer:
"I make the parent a flex container and set justify-content: center and align-items: center. The parent needs some height, such as a min-height, or there is no free space to center in. If the parent is a column, the two properties still work, but each one controls the other axis."
What should different experience levels explain?
- Junior developer: Explain how to center content with flexbox, and how the header, the hero and the footer use rows.
- Senior developer: Explain the column page shell with a growing
main, the swapped axes in a column, whyalign-selfis needed, and why the viewport tag matters. - Technical architect: Explain how the shell, the sections and the parts stay independent, when to combine Grid and Flexbox, and how performance, accessibility and testing affect a whole page.
S8. Hands-On Practice
Beginner: Build the shell and the header
Create the page shell from Example 2 and the header from Example 3. Add the viewport meta tag, and make the browser window narrower and wider.
Expected result: The footer stays at the bottom of a short page. The links sit beside the logo on a wide screen, and wrap under it on a narrow screen.
Intermediate: Add the hero and the sections
Add the hero from Example 4, then the feature cards and the call to action from Example 5. Remove align-self: flex-start and put it back.
Hint: Remember that the cross axis of a column is horizontal.
Expected result: The hero text and image sit side by side on a wide screen and stack on a narrow one. Without the align-self rule, the button stretches. The call to action is centered in both directions.
Advanced: Build and test the complete page
Combine all the examples into one page with the container and the footer from Example 6. Test it at 360px, 640px and 1400px, then zoom to 200% and use only the keyboard to reach every link.
Expected result: The page works at all three widths with no sideways scroll. The container stays 960px wide and centered on the big screen. The footer stays at the bottom, and every link shows a visible focus outline.
S9. Next Steps and Community
Previous article: Build a Professional Dashboard with Sidebar and Main Content.
Next article: Top 50 CSS Flexbox Interview Questions and Practical Coding Challenges.
Continue the series with interview preparation. You have now built a navigation bar, a card layout, a dashboard and a complete page, so you are ready to practice the questions and the coding challenges that employers ask about flexbox.
Try the examples in your own HTML and CSS project. Add a section, narrow the window and watch how the page adapts. Share your finished page with the Upgradix community, and help other developers understand Flexbox through practical examples.
Stack the page. Wrap the sections. Keep the footer down.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.