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

Flex Container

Flexbox container properties control how the children flow: which axis, how they wrap, how they're spaced and aligned.

Container properties

PropertyValuesControls
displayflex / inline-flexTurn the element into a flex container.
flex-directionrow (default), column, row-reverse, column-reverseMain axis direction.
flex-wrapnowrap (default), wrap, wrap-reverseWhether items wrap onto new lines.
flex-flowshorthand for direction + wraprow wrap
justify-contentflex-start, center, space-between, space-around, space-evenlyDistribution along the main axis.
align-itemsstretch, flex-start, flex-end, center, baselineCross-axis alignment per row.
align-contentSame values as justify-contentCross-axis distribution when items wrap onto multiple lines.
gapany lengthSpace between items (replaces margins).

The toolbar pattern

CSS
.toolbar {
  display: flex;
  align-items: center;       /* vertically centre everything */
  gap: 12px;                 /* even spacing between items */
}
.toolbar .spacer { flex: 1; }   /* eats the leftover room, pushes the rest right */
Tip: align-content only does anything when items wrap onto more than one line. On a single-row flex container, it's align-items you want.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>

<h1>Flex Container</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

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

Exercise

Space the toolbar items evenly across the main axis.

.toolbar { display: flex; -content: space-between; }

Test yourself

Q1. Default `flex-direction` value is…
Q2. Which container property aligns items along the main axis?
Q3. When does `align-content` have an effect?

Discussion

Loading…