Tutorials

CSS Tutorial

All Tutorials Learning Tools Login

CSS Tutorial · Chapter 6 of 7

CSS Flexbox

Arrange items in rows or columns.

display: flex turns a container into a flex layout.

Use gap, justify-content, and align-items to control spacing and alignment.

Flexbox sample Try it Yourself »
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Flexbox</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <nav class="menu">
        <a href="#">Home</a>
        <a href="#">Lessons</a>
        <a href="#">Projects</a>
    </nav>
    <main class="cards">
        <article>HTML</article>
        <article>CSS</article>
        <article>JavaScript</article>
    </main>
</body>
</html>
style.css
body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f8fafc;
}

.menu {
    display: flex;
    gap: 16px;
    justify-content: center;
    padding: 16px;
    background: #0f766e;
}

.menu a {
    color: white;
    text-decoration: none;
    font-weight: 700;
}

.cards {
    display: flex;
    gap: 16px;
    justify-content: center;
    flex-wrap: wrap;
    padding: 24px;
}

.cards article {
    min-width: 140px;
    padding: 24px;
    border-radius: 12px;
    background: white;
    text-align: center;
}

Classroom Tips

  • Apply display: flex on the parent.
  • Try justify-content: space-between next.