Flexbox
CSS Flexible Box Layout
In short
Flexbox is a CSS layout model that arranges items in a single row or column and controls how they grow, shrink, align, and share the available space.
What is Flexbox in CSS?
Flexbox, short for CSS Flexible Box Layout, is a way to lay out elements along one direction at a time, either a row or a column. You turn an element into a flex container with display: flex, and its direct children become flex items that line up next to each other and can stretch or shrink to fill the space.
Flexbox works along two axes. The main axis follows flex-direction, which is a row by default, and justify-content controls how items are spaced along it; the cross axis runs perpendicular to it, and align-items controls alignment in that direction. The gap property adds space between items, flex-wrap lets items move onto new lines, and the flex shorthand on an item, such as flex: 1, sets how much it grows or shrinks compared with its siblings.
Imagine books on a shelf with adjustable bookends: you can push them all to the left, center them, spread them out evenly, or let some books expand to fill the gap. Flexbox is the everyday tool for navigation bars, toolbars, button groups, rows of cards, and the famously tricky task of centering something both horizontally and vertically, and all modern browsers support it.
Flexbox is often compared with CSS Grid. Flexbox is one-dimensional and sizes items from their content outward, which suits components where items flow in a line, while Grid is two-dimensional and controls rows and columns at the same time, which suits page layouts and galleries. Most modern sites use both, often Grid for the overall page structure and Flexbox inside individual components.
Key takeaways
display: flexmakes an element a flex container and its children flex items.- Flexbox lays out items in one dimension: a row or a column.
justify-contentaligns items on the main axis;align-itemsaligns them on the cross axis.gap,flex-wrap, andflexcontrol spacing, wrapping, and growth.- Use Flexbox for components and CSS Grid for two-dimensional layouts.
Example
/* A navigation bar: logo on the left, links on the right */
.navbar {
display: flex;
justify-content: space-between; /* main axis (horizontal) */
align-items: center; /* cross axis (vertical) */
gap: 1rem;
}
/* Center anything horizontally and vertically */
.centered {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}Readers ask
What is the difference between Flexbox and CSS Grid?
Flexbox arranges items in a single row or column and is ideal for components like navigation bars. CSS Grid controls rows and columns at the same time, which makes it better for page layouts and galleries, and the two are often used together.
How do I center a div with Flexbox?
Give the parent display: flex, justify-content: center, and align-items: center. The child is then centered horizontally and vertically, as long as the parent is taller than the child.
What does flex: 1 mean?
flex: 1 is shorthand that lets an item grow to fill free space, allows it to shrink, and sets its starting size to zero. As a result, all items with flex: 1 share the available space equally.
Often compared
See also
- CSSWeb Development, p. 10CSS is the style sheet language that controls how HTML content looks on screen, including colors, fonts, spacing, layout, and animations.
- Responsive DesignWeb Development, p. 38Responsive design is an approach to building web pages whose layout, images, and text adapt automatically to any screen size, from phones to large monitors.
- HTMLWeb Development, p. 18HTML is the markup language that defines the structure and content of web pages, such as headings, paragraphs, links, images, and forms.
- Web BrowserWeb Development, p. 57A web browser is an application that fetches web pages from servers and turns their HTML, CSS, and JavaScript into the interactive pages you see on screen.
Spotted a mistake or something missing on this page?Suggest an edit