Short answer
You create a responsive navbar with a nav landmark, a list of links, and CSS that changes direction when the viewport gets narrow. No component library is required. Make the list a flex row, then use a media query at a width you choose, such as 40rem, to stack the links. That width is an example, not a breakpoint the CSS specs define as standard.
Last verified against the sources listed below.
Key takeaways
- MDN defines nav as a section of navigation links, such as a menu, a table of contents, or an index.
- A flex container is one-dimensional. Setting display to flex makes the direct children flex items.
- Media queries apply styles from viewport width, media type, and other features. They do not watch a component box.
- Use 40rem only as an example width. MDN's own samples use other values, including 30em and 1250px.
- If the links never stack, the media query is not matching, or flex was set on the wrong element.
- WCAG 2.2 says user interface components and navigation must be operable. A nav is part of that job.
How to create a responsive navbar, in the version that does not need a library, is a short HTML pattern plus two CSS rules. The links sit in a row while the viewport is wide. They stack when you say so.
CSS already has the tools: flexbox for the row, and a media query for the viewport.
Responsive web design is the wider idea, and this page is only the bar. The longer guide is responsive web design.
What do you need?
You need an HTML page, a style sheet, and a browser. You do not need a framework, a CSS library, or a fixed pixel width from a spec.
MDN’s nav page says the element represents a section whose purpose is to provide navigation links, in the current document or to other documents. Menus, tables of contents, and indexes are the examples it names. The WHATWG HTML spec uses that same description.
WCAG 2.2 states the Operable principle in plain words: user interface components and navigation must be operable. A navbar is that kind of navigation. The markup below keeps the links as real links inside a list, which a keyboard can reach.
How do you build the bar?
Build the bar in five actions. Each step does one thing.
- Add a
navelement where the main menu belongs. - Inside it, add a
ulwith oneliand one link for each destination. - In CSS, set the list to
display: flexandflex-direction: row, and remove the list bullets. - Add a media query at an example width of
40remthat setsflex-direction: column. - Resize the browser across that width and confirm the links change direction.
The row rule looks like this:
nav ul {
display: flex;
flex-direction: row;
list-style: none;
margin: 0;
padding: 0;
}
MDN’s flexbox guide calls flexbox a one-dimensional layout, a row or a column. Setting display to flex makes the direct children flex items.
Put that property on the ul if the links should line up. If you put it only on the nav, the list is the only flex item.
The main axis follows flex-direction. Row runs along the line of text. Column runs down the page. The Flexbox Playground is a place to try that axis before you paste the rule into the style sheet.
How does the narrow rule work?
The narrow rule applies only while the viewport matches the query. It does not watch the navbar’s own box.
MDN’s media query guide says queries apply styles from a media type, such as screen or print, or from features such as viewport width.
A query is an optional type plus feature tests. The type defaults to all, except with the only operator. min-width and max-width, or <= and >=, set an inclusive bound.
Media Queries Level 5 describes the width feature as the viewport width. Its sample (400px < width < 1000px) is true when the viewport is between those values. It also warns that a max-width of 320px and a min-width of 321px can leave a gap where neither set of styles applies.
Use this query as an example, not as a standard:
@media (max-width: 40rem) {
nav ul { flex-direction: column; }
}
40rem is a choice for this demo. MDN’s samples use other numbers, including 1250px and a band from 30em to 50em. Change 40rem if your labels need more or less room. Nothing on those pages declares one navbar breakpoint for every site.
What if the layout breaks?
Match the symptom to the rule that is still winning. The table uses only behavior described in the pages above.
| What you see | Why it happens | What to change |
|---|---|---|
| Links stay in one row on a phone | The viewport is still wider than the query, or the query never loaded | Check the width you typed, then resize past it |
| The whole list is one flex item | display: flex is on nav, and the ul is the only child |
Move display: flex to the ul |
| Items overflow the row | flex-wrap is nowrap, the initial value, and items cannot shrink enough |
Stack with the media query, or set flex-wrap: wrap if you want wrapping instead |
| Narrow and wide styles both seem missing | The two width tests leave a gap, as in the Level 5 example | Make the ranges meet, or overlap on purpose |
| Two menus sound the same | The page has several nav elements and no names |
Add aria-labelledby, which MDN suggests for that case |
MDN also says nowrap is the initial flex-wrap, and that items shrink because flex-shrink starts at 1. Wrapping is a different design from a single stack at one width. Pick one and test it.
How do you check the result?
Open the page, widen it, and narrow it. The links should form one row above your chosen width and a column below it.
Tab through the links. They should be in the same order as the list. WCAG 2.2 puts navigation under Operable, so a menu that only appears for a pointer is a weaker result than links that remain in the page.
If you later need the menu to open and close, add a button that shows or hides the list. That is a second behavior. The responsive part is still the flex direction and the media query, and those two do not require a library.
Frequently asked questions
Do all links on a page belong in nav?
No. MDN says nav is for a major block of navigation links. A footer often has a list of links that do not need to sit in a nav element. A page can also have more than one nav, for example site navigation and navigation inside the page. When you do that, MDN points at aria-labelledby so each landmark has a name. Screen readers can use the nav element when deciding whether to skip navigation-only content.
Does a nav have to contain a list?
No. MDN says the semantics are about providing links, and a nav can contain other kinds of content, including links written in prose. A list is still the clearer structure for a row of destinations, because each link is a separate item you can lay out. The WHATWG nav section uses the same idea: the element marks navigation links, whether they stay on this document or go to other documents.
What is the initial flex direction?
MDN's flexbox guide says that when you set display to flex and do not give the items a size, the items move into a row and take only the space their content needs. flex-direction can be row, row-reverse, column, or column-reverse. Row and row-reverse run along the inline direction. Column and column-reverse run in the block direction. The media query in this guide sets column explicitly so the narrow layout does not depend on guessing the initial value.
Why do flex items shrink?
MDN says flex-wrap starts at nowrap. Items that are too wide then shrink, because the initial flex-shrink value is 1. If they cannot shrink enough, the row overflows. Setting flex-wrap to wrap makes each line its own flex line, and space is distributed on that line only. A navbar that should stack at one chosen width is usually clearer with a media query than with wrap alone, because wrap breaks whenever the items no longer fit.
Is 40rem a standard breakpoint?
No. Nothing on the MDN media query page or in Media Queries Level 5 names 40rem as a required navbar breakpoint. MDN shows sample conditions such as max-width 1250px and a range from 30em to 50em. Media Queries Level 5 uses sample viewport comparisons such as a width between 400px and 1000px. Pick a width where your own links stop fitting on one line, and write that width in the query. Change it when the labels change.
Sources
- Basic concepts of flexbox · MDN, 2026
- Using media queries · MDN, 2026
- The nav element · MDN, 2026
- The nav element · WHATWG, 2026
- Media Queries Level 5 · CSS Working Group, 2026
- Web Content Accessibility Guidelines (WCAG) 2.2 · W3C, 2026