Home Text Tools Math & Numbers Developer Tools Date & Time Image Tools Blog
CSS Flexbox Generator

Build a Navigation Bar with Flexbox: Logo Left, Buttons Right

Lay out a navbar with flexbox: evenly spaced links, a logo on the left and call-to-action buttons on the right, with mobile wrapping tips.

A typical header has a logo on the left, a row of links in the middle and a button or two on the right. Flexbox handles this layout with a handful of properties, and a visual playground helps you see what each one does before you paste the code into your project.

CSS Flexbox Generator FREE online toolRuns in your browser. No sign-up, nothing to install. Open the tool

The basic container

Put the logo, the links and the buttons inside one container and set display: flex. By default the children line up in a row, from left to right, and their height stretches to match the tallest one.

Use align-items: center to line everything up vertically, which is nearly always what you want for a navbar. Without it, shorter items stick to the top.

Keep the markup simple as well. A header element holding a logo link, a nav element with a list of links, and a div with the buttons gives you three flex children, which is easy to reason about. Fewer wrapper elements mean fewer places for alignment to go wrong, and screen readers get a cleaner structure.

Build it

  1. Open the CSS Flexbox Generator and set the direction to row.
  2. Set align-items to center.
  3. Set justify-content to space-between so the first item sits on the left and the last on the right.
  4. Add a gap, such as 16px, between items.
  5. Copy the CSS and apply it to your header container.
  6. Add margin-left: auto to the first button if you want the links to stay next to the logo and only the buttons pushed right.

Choosing the spacing

The justify-content property controls horizontal distribution. With space-between and three children, the middle child lands in the center only if the first and last are the same width. If the logo is narrow and the buttons are wide, the middle group looks off-center.

A more precise method is to give the logo and the button group equal flex-basis values, or place the link group between them with margin: 0 auto. Both approaches keep the links visually centered.

The gap property is usually better than margins on each item, because it adds space only between items and never at the ends. It is supported by flexbox in all current browsers, and a single value is easier to tweak than a set of margins on every link.

  • space-between: first item left, last item right, equal gaps.
  • space-around: equal space around each item, edges get half the gap.
  • space-evenly: identical gaps everywhere, including at the edges.
  • flex-start with gap: items grouped at the left.

Small screens

On narrow phones a full row of links will not fit. Setting flex-wrap: wrap lets items drop onto a second line instead of squashing or overflowing. For a real mobile menu, most sites hide the links behind a toggle button and show them in a column using a media query.

A useful habit is to test at around 320 to 375 pixels wide. If you can still tap the buttons without zooming, the layout is in good shape. Make touch targets roughly 44 pixels tall.

Common mistakes

A frequent one is forgetting that justify-content works along the main axis. If you switch to a column direction, justify-content controls vertical spacing, and align-items controls horizontal. Another is using fixed widths on flex children, which fights the layout. Prefer flex: 1 or flex-basis for items that should share space.

Also watch for items that refuse to shrink. Long text inside a flex child can force the row wider than the screen, and the fix is often min-width: 0 on that child. If a logo image stretches, set its height rather than relying on the container. Check the result in the browser's developer tools, which can highlight flex containers.

Try the free CSS Flexbox Generator
Runs in your browser. No sign-up, nothing to install.

Comments (0)

No comments yet. Be the first to share a thought.