iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Flexbox Intro

Flexbox is a one-dimensional layout system. Set display: flex on a parent and its children line up along a single axis with smart sizing.

Main axis and cross axis

1 2 3 main axis (flex-direction: row) cross axis
Fig 1. Items flow along the main axis; the cross axis runs perpendicular.

Container properties

PropertyWhat it controlsCommon values
flex-directionMain-axis direction.row, column, row-reverse
justify-contentAlignment along the main axis.flex-start, center, space-between
align-itemsAlignment along the cross axis.stretch, center, flex-end
flex-wrapWhether items wrap to new lines.nowrap, wrap
gapSpace between items.10px, 1rem
Tip: Reach for flexbox when items go in a row or a column. Reach for CSS Grid when you have rows and columns to coordinate.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
.row { display: flex; gap: 10px; }
.row > div { background: #04AA6D; color: #fff; padding: 20px; flex: 1; text-align: center; }
</style>
</head>
<body>

<div class="row">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>

</body>
</html>
Try it Yourself »

Exercise

Turn the parent into a flex container and centre its children horizontally.

.row { display: ; justify-content: center; }

Test yourself

Q1. Which value of `display` turns an element into a flex container?
Q2. Which property aligns items along the main axis?
Q3. What is the default `flex-direction`?

Discussion

Loading…