CHAPTER 68

CSS Flex Container

Learn the major properties that control the flex container.

01. What is CSS Flex Container?

The flex container controls how its direct children are arranged.
Important properties include flex-direction, justify-content, align-items, flex-wrap and gap.
These properties allow flexible and responsive layouts.

02. Syntax

.container { display: flex; flex-direction: row; justify-content: center; align-items: center; flex-wrap: wrap; gap: 20px; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>

.container {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
}

.item {
    width: 180px;
    padding: 25px;
    background: #2563eb;
    color: white;
}

</style>
</head>
<body>

<div class="container">

    <div class="item">One</div>
    <div class="item">Two</div>
    <div class="item">Three</div>

</div>

</body>
</html>

04. Important Points

✓ flex-direction controls row or column direction.
✓ justify-content aligns along the main axis.
✓ align-items aligns along the cross axis.
✓ flex-wrap allows items to move to additional lines.
✓ gap controls spacing.

05. Common Mistakes

⚠ Confusing main axis and cross axis.
⚠ Using nowrap when wrapping is required.
⚠ Forgetting the effect of flex-direction.

06. Real World Use

Flex containers are used for responsive navigation, card rows, toolbars and page sections.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Build a Responsive Flex Container

Create four cards that wrap automatically when the screen becomes smaller.