Flexbox (Flexible Box Layout) is a one-dimensional layout model that helps align and distribute space among items in a container. It works well for horizontal or vertical alignment.
.container {
display: flex;
}Flex items are laid out along the main axis (by default: horizontal), while the cross axis runs perpendicular.
.container {
flex-direction: row;
}
.container-column {
flex-direction: column;
}Use justify-content to align items on the main axis:
Visual examples of each:
Use align-items to align flex items on the cross axis:
You can control the behavior of individual items using:
flex-grow: ability to growflex-shrink: ability to shrinkflex-basis: initial sizeflex: shorthand for grow, shrink, basisalign-self: override cross-axis alignment.item {
flex: 1;
align-self: center;
}Create a flex container with three evenly spaced items:
<div style={{
display: "flex",
justifyContent: "space-evenly",
alignItems: "center",
height: "100px",
backgroundColor: "#f0f0f0"
}}>
<div style={{ backgroundColor: "#ff9999", padding: "10px" }}>Item 1</div>
<div style={{ backgroundColor: "#99ccff", padding: "10px" }}>Item 2</div>
<div style={{ backgroundColor: "#99ff99", padding: "10px" }}>Item 3</div>
</div>You can copy and paste this code in your IDE to run!
Ask the AI if you need help understanding or want to dive deeper in any topic