Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Links HTML Images HTML Project HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Block & Inline HTML Div HTML Classes HTML Id HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Computercode HTML Semantics HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Charsets HTML URL Encode HTML vs. XHTML

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML Plug-ins HTML YouTube

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary HTML Accessibility

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML Layout - Using CSS Flexbox


Share

HTML Layout - CSS Flexbox

Flexbox is a CSS layout method for arranging items in one direction, either in a row or in a column. You turn it on by setting display: flex on a container, and its children become flexible items.

The following example shows how you can layout a webpage using CSS flexbox:

Example

Cities

London

London is the capital city of England. It is the most populous city in the United Kingdom.

Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.

Footer

Try it Yourself »


CSS Flexbox Layout - Pros and Cons

Pros:

  • Made for layout. Unlike float, Flexbox was designed for placing boxes
  • Easy centering. Centering an item both horizontally and vertically takes just two lines: justify-content: center; and align-items: center;
  • Equal-height columns. Items in a row stretch to the same height by default
  • Flexible sizes. Items can grow and shrink to fill the space (you do not have to calculate exact widths)
  • Easy spacing. The gap property adds space between items without margin tricks
  • No clearing needed. You do not need clear or clearfix, and the container never collapses
  • Simple to make responsive. flex-wrap: wrap lets items move to a new line on small screens, and switching flex-direction from row to column stacks them
  • Change order easily. The order property can move items visually without changing the HTML
  • Supported in all modern browsers

Cons:

  • One direction at a time. Flexbox handles a row or a column. For layouts where rows and columns need to line up together, Grid is better
  • Wrapped items do not line up like a grid. When items wrap, each line is sized on its own, so items in different lines may not match
  • Many properties to learn. flex-grow, flex-shrink, flex-basis, justify-content, and align-items can be confusing at first
  • Sizing can surprise you. Items sometimes shrink or grow more than expected, especially with long text or images
  • The order property can confuse users. If the visual order differs from the HTML order, keyboard and screen reader users may get lost

Should you use it?

Yes. Flexbox is great for parts of a page, such as navigation bars, rows of cards, buttons, and centering content. For the overall page layout with rows and columns, many developers combine it with CSS Grid.

Learn more about flexbox in our CSS Flexbox chapter.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.