Skip to Content
👆 We offer 1-on-1 classes as well check now

Flexbox

Flexbox (Flexible Box) is a powerful CSS layout module that makes it easy to design responsive and dynamic web pages. It allows you to create flexible containers that can easily adapt to different screen sizes and orientations.

What is Flexbox?

Flexbox is a one-dimensional layout system. This means it primarily deals with laying out items in a single row or column. It provides a simple and efficient way to align and distribute items within a container, regardless of their size or the available space. Think of it as a smarter way to arrange elements on your webpage!

Basic Example

Let’s start with a simple example. We’ll create a container (the flex container) and some items inside it (flex items).

<div class="container"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> </div>

Now, let’s add some CSS to make the container a flex container and see the magic happen:

.container { display: flex; /* This turns the container into a flex container */ background-color: lightgray; padding: 10px; } .item { background-color: #eee; padding: 10px; margin: 5px; text-align: center; }

By setting display: flex; on the .container, we’ve told the browser to use Flexbox to arrange its children (.item elements). By default, flex items are laid out horizontally in a row, with as much space as possible. This is the simplest form of Flexbox!

Practical Usage

Let’s build on this. Imagine we want to create a navigation bar with a logo on the left and navigation links on the right. Flexbox makes this super easy.

<nav class="navbar"> <div class="logo">My Website</div> <ul class="nav-links"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </nav>
.navbar { display: flex; justify-content: space-between; /* Distributes space between elements */ align-items: center; /* Vertically centers items */ background-color: #333; color: white; padding: 10px 20px; } .logo { font-size: 1.5em; } .nav-links { list-style: none; display: flex; /* Make the links a flex container for horizontal arrangement */ margin: 0; /* Removing the default margin */ padding: 0; } .nav-links li { margin-left: 20px; } .nav-links a { color: white; text-decoration: none; }

In this example, justify-content: space-between; pushes the logo to the left and the links to the right, creating a clean layout. align-items: center vertically aligns the logo and links.

Key Takeaways

  • display: flex; is the key: This CSS property turns an element into a flex container.
  • Flex items are flexible: They can grow and shrink to fit the container.
  • justify-content aligns items horizontally: Useful for spacing and positioning. Common values include flex-start, flex-end, center, space-between, and space-around.
  • align-items aligns items vertically: Useful for centering content. Common values include flex-start, flex-end, center, and stretch.
Last updated on