CSS Layout
- An Interactive Guide to Flexbox ↗
- Flexbox Froggy ↗
- CSS Flexbox Layout Guide ↗
- Flexbox Layout Patterns ↗
Flexbox consists of two main components:
- Flex Container: The parent element that holds flex items. You activate Flexbox by setting
display: flexon this container. - Flex Items: The direct children of the flex container, which Flexbox arranges either horizontally or vertically.
- Main Axis: The primary axis along which the flex items are laid out. By default, it’s horizontal (left to right), but it can be changed to vertical using
flex-direction. - Cross Axis: The axis perpendicular to the main axis. If the main axis is horizontal, the cross axis will be vertical, and vice versa.
To enable Flexbox on a container, use the display property:
.container { display: flex; /* or inline-flex */}flex: The container behaves as a block-level element.inline-flex: The container behaves as an inline element.
Defines the direction in which the flex items are placed inside the container. This property is key for controlling the layout’s flow (row, column, or reversed).
.container { flex-direction: row; /* Default. Left to right */ /* Other options: */ flex-direction: row-reverse; /* Right to left */ flex-direction: column; /* Top to bottom */ flex-direction: column-reverse; /* Bottom to top */}-
flex-direction: Determines the direction of the main axis.row(default): Items are laid out horizontally.column: Items are laid out vertically.row-reverse/column-reverse: Reverses the direction of the layout.
-
flex-wrap: Controls whether the flex items should wrap onto multiple lines if there’s not enough space.nowrap(default): Items will stay in a single line.wrap: Items will wrap onto a new line.wrap-reverse: Items will wrap in the opposite direction.
-
justify-content: Aligns flex items along the main axis.flex-start: Items are packed to the start of the container.flex-end: Items are packed to the end.center: Items are centered in the container.space-between: Items are spaced evenly, with the first item at the start and the last at the end.space-around: Items are spaced evenly, with space around each item.
-
align-items: Aligns flex items along the cross axis (perpendicular to the main axis).flex-start: Items are aligned at the start of the container.flex-end: Items are aligned at the end.center: Items are centered.stretch(default): Items stretch to fill the container.baseline: Items align along their baseline.
-
align-content: Aligns lines of flex items (if wrapping occurs) along the cross axis.- Similar values to
align-items, but it affects multiple lines of items when wrapping is enabled.
- Similar values to
Defines the ability of a flex item to grow relative to other flex items inside the container.
.item { flex-grow: 1; /* The item will take up available space */ flex-grow: 0; /* The item will not grow */}Controls the ability of a flex item to shrink when the container is too small.
.item { flex-shrink: 1; /* The item will shrink */ flex-shrink: 0; /* The item will not shrink */}Defines the initial size of a flex item before it starts growing or shrinking. It can be set as a specific value (e.g., px, %, em, etc.) or auto (based on the item’s content).
.item { flex-basis: 200px; /* Start with 200px */ flex-basis: auto; /* Use the item's natural size */}The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis.
.item { flex: 1; /* Equivalent to flex-grow: 1, flex-shrink: 1, flex-basis: 0% */ flex: 0 1 200px; /* flex-grow, flex-shrink, flex-basis */}Allows individual flex items to override the align-items property of the container.
.item { align-self: auto; /* Inherits from container */ align-self: flex-start; /* Aligns item at the start */ align-self: flex-end; /* Aligns item at the end */ align-self: center; /* Aligns item in the center */ align-self: baseline; /* Aligns item based on baseline */ align-self: stretch; /* Stretches item */}<div class="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div></div>.container { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap;}
.item { flex: 1; margin: 10px; text-align: center; padding: 20px; background-color: lightblue;}In this example:
- The container uses
display: flexto activate the flexbox layout. justify-content: space-betweenensures the items are spaced evenly along the main axis (horizontal by default).align-items: centercenters the items vertically within the container.flex-wrap: wrapmakes the items wrap onto a new line if needed.