Home Web Front-end CSS Tutorial Understanding flex-grow, flex-shrink, and flex-basis

Understanding flex-grow, flex-shrink, and flex-basis

Apr 02, 2025 am 04:19 AM

Understanding flex-grow, flex-shrink, and flex-basis

Deeply understand the interaction of CSS flex-grow , flex-shrink and flex-basis properties. When we apply CSS attributes, a lot of things happen behind the scenes. For example, suppose we have the following HTML:

<div>
  <div>Child</div>
  <div>Child</div>
  <div>Child</div>
</div>
Copy after login

Then we write some CSS:

 .parent {
  display: flex;
}
Copy after login
Copy after login

In fact, in addition to the above line of CSS code, we also implicitly apply a series of attributes to .child element, as if we wrote the following style ourselves:

 .child {
  flex: 0 1 auto; /* Default flex value*/
}
Copy after login

This is because some properties have default values ​​that are intended to be overridden by us. Without understanding the style of these implicit applications, the layout can become very confusing and difficult to manage.

flex attribute is a shorthand CSS attribute, which sets three independent CSS attributes at the same time. Therefore, the abbreviation above is equivalent to:

 .child {
  flex-grow: 0;
  flex-shrink: 1;
  flex-basis: auto;
}
Copy after login

Abbreviation attributes bundle multiple CSS attributes together, making it convenient to write multiple attributes at once, just like background attributes.

While abbreviation properties may be confusing, for flexbox, it is recommended to use abbreviation because there is a complex interaction between flex attribute and its sub-properties.

The default style is very useful because in most cases we don't need to know the details of these flexbox properties. For example, when using flexbox, we usually write like this:

 .parent {
  display: flex;
  justify-content: space-between;
}
Copy after login

We don't even need to care about child elements or the styles applied to them. But this is just the tip of the iceberg of flexbox.

What if we wanted to dig deep into how flexbox, including flex-grow , flex-shrink and flex-basis properties work, and how to use them to create more complex layouts?

Let's start with the simplified overview and go back to the default flex attribute applied to the child elements:

 .child {
  flex: 0 1 auto;
}
Copy after login

These default styles tell the child elements how to stretch and shrink. I usually understand these abbreviation properties as:

 /* How do I think about the above rules in my mind*/

.child {
  flex: [flex-grow] [flex-shrink] [flex-basis];
}

/* or... */

.child {
  flex: [maximum] [minimum] [ideal size];
}
Copy after login

The first value flex-grow defaults to 0, which means that the element does not expand (mostly). The element size depends on its content. For example:

 .parent {
  display: flex;
}
Copy after login
Copy after login

If we change the default value of flex-grow attribute from 0 to 1:

 .child {
  flex: 1 1 auto;
}
Copy after login

Then all elements will equally distribute the space of the .parent element, but only if their contents are the same length. This is equivalent to:

 .child {
  flex-grow: 1;
}
Copy after login

If we want one of the elements to grow more than the others, we can do this:

 .child-three {
  flex: 3 1 auto;
}

/* or... */

.child-three {
  flex-grow: 3;
}
Copy after login

flex-shrink is the second value in the abbreviation:

 .child {
  flex: 0 1 auto; /* flex-shrink = 1 */
}
Copy after login

flex-shrink tells the browser the minimum size of the element. The default value is 1, which means that the same space is always occupied. If set to 0:

 .child {
  flex: 0 0 auto;
}
Copy after login

The element will not shrink.

flex-basis is the last value added to flex abbreviation by default, which tells the element to maintain the ideal size. The default value is auto , which means "use my height or width".

When we set flex-basis to 1000px:

 .child-three {
  flex: 0 1 1000px;
}
Copy after login

We tell the browser to try to take up 1000px of space. If it is not possible, the element will take up space proportionally based on the other elements.

If we want to prevent the element from shrinking:

 .child-three {
  flex: 0 0 1000px;
}
Copy after login

Setting flex-wrap property will change the layout:

 .parent {
  display: flex;
  flex-wrap: wrap;
}
Copy after login

all in all:

  • Try to use the abbreviation of flex .
  • When using abbreviation, remember the maximum, minimum, and ideal size.
  • Element content also affects how these values ​​work.

The above is the detailed content of Understanding flex-grow, flex-shrink, and flex-basis. 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.

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:

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.

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?

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.

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

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there&#039;s a bit of a learning curve, but Grid is

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

See all articles