Home Web Front-end CSS Tutorial Flexbox – The Modern Way to Align and Distribute Space

Flexbox – The Modern Way to Align and Distribute Space

Sep 20, 2024 am 06:41 AM

Flexbox – The Modern Way to Align and Distribute Space

Lecture 14: Flexbox – The Modern Way to Align and Distribute Space

Hey there! Ready to dive into one of the coolest and most powerful tools in CSS? Today, we’re going to explore Flexbox. If you’ve ever struggled with aligning items or distributing space in a neat and responsive way, Flexbox is your new best friend.

1. What is Flexbox?

Flexbox (Flexible Box Layout) is a one-dimensional layout system that allows you to control the alignment, spacing, and distribution of elements inside a container—even when the size of those elements is unknown or dynamic.

Think of Flexbox as a toolbox to create layouts that can stretch, shrink, or align depending on the available space.

2. The Magic Begins with display: flex

To start using Flexbox, you only need to set display: flex on a container. Once you do that, all the direct children of that container become flex items, and they’ll immediately start behaving differently.

<div class="flex-container">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
</div>
Copy after login
Copy after login
.flex-container {
    display: flex;
}
Copy after login

Now, all the items inside .flex-container are flex items and can be easily manipulated.

3. Flex Direction – Which Way Should We Go?

By default, Flexbox arranges items in a row (horizontally), but what if you want them in a column (vertically)? Flexbox gives you total control with the flex-direction property.

  • row: Align items in a horizontal row (this is the default).
  • column: Stack items in a vertical column.
  • row-reverse: Same as row, but the order of items is reversed.
  • column-reverse: Same as column, but items are stacked in reverse order.
.flex-container {
    display: flex;
    flex-direction: column;
}
Copy after login

Now, the items will stack vertically!

4. Justifying Content – Spreading Things Out

Let’s say you have three items, and you want to spread them out evenly in your container. This is where justify-content comes in handy.

  • flex-start: Items align to the start of the container (default).
  • center: Items are centered.
  • space-between: Items are evenly spaced, with the first item at the start and the last item at the end.
  • space-around: Items are spaced with equal padding around each item.
.flex-container {
    display: flex;
    justify-content: space-between;
}
Copy after login

Now, the items will be evenly spaced within the container.

5. Aligning Items – Vertical Magic

While justify-content controls horizontal alignment, align-items takes care of vertical alignment (or along the cross-axis). Here are your options:

  • stretch: Items stretch to fill the container (default).
  • flex-start: Items align to the top.
  • flex-end: Items align to the bottom.
  • center: Items are vertically centered.
.flex-container {
    display: flex;
    align-items: center;
}
Copy after login

Now, all the items will be vertically centered within the container.

6. Flex-Grow, Flex-Shrink, and Flex-Basis – Fine-Tuning the Flex Items

Sometimes, you want certain items to grow, shrink, or have a fixed starting size. The flex-grow, flex-shrink, and flex-basis properties let you control that behavior:

  • flex-grow: Controls how much an item should grow relative to the other items.
  • flex-shrink: Controls how much an item should shrink relative to the other items.
  • flex-basis: Sets the initial size of the item before it grows or shrinks.

Example:

.item {
    flex-grow: 1;
    flex-shrink: 0;
    flex-basis: 100px;
}
Copy after login

This ensures that the item starts at 100px, but it can grow to fill extra space if needed, without shrinking.

7. Flexbox Example in Action

Let’s put all this together with an example!

<div class="flex-container">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
</div>
Copy after login
Copy after login
.flex-container {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    height: 300px;
    background-color: #f0f0f0;
}

.item {
    background-color: #4CAF50;
    padding: 20px;
    color: white;
    flex-grow: 1;
}
Copy after login

In this example:

  • Items are arranged in a row.
  • They are evenly spaced out with justify-content: space-around.
  • All items are vertically centered in the container with align-items: center.
  • Each item grows to fill the available space equally, thanks to flex-grow: 1.

8. Why Flexbox Rocks

Flexbox takes away much of the complexity of layout design that we used to struggle with in CSS. No more floats, no more worrying about clearing, and much easier responsive design!

Ambilan Utama:

  • Gunakan paparan: lentur untuk menukar bekas menjadi bekas lentur.
  • Gunakan arah lentur untuk menetapkan arah aliran (baris atau lajur).
  • Gunakan justify-content dan align-item untuk mengawal jarak dan penjajaran.
  • Tala halus item flex anda dengan flex-grow, flex-shrink dan flex-basis.

ikuti saya di LinkedIn-

Ridoy Hasan

The above is the detailed content of Flexbox – The Modern Way to Align and Distribute Space. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles