<p>When you apply a CSS property to an element, there’s lots of things going on under the hood. For example, let’s say we have some HTML like this: </p> <p> Child<br /> Child<br /> Child </p> <p>And then we write some CSS… </p> <p>.parent {<br /> display: flex;<br /> } </p> <p>These are technically not the only styles we’re applying when we write that one line of CSS above. In fact, a whole bunch of properties will be applied to the .child elements here, as if we wrote these styles ourselves: </p> <p>.child {<br /> flex: 0 1 auto; /* Default flex value */<br /> } </p> <p>That’s weird! Why do these elements have these extra styles applied to them even though we didn’t write that code? Well, that’s because some properties have defaults that are then intended to be overridden by us. And if we don’t happen to know these styles are being applied when we’re writing CSS, then our layouts can get pretty darn confusing and tough to manage. </p> <p>That flex property above is what’s known as a shorthand CSS property. And really what this is doing is setting three separate CSS properties at the same time. So what we wrote above is the same as writing this: </p> <p>.child {<br /> flex-grow: 0;<br /> flex-shrink: 1;<br /> flex-basis: auto;<br /> } </p> <p>So, a shorthand property bundles up a bunch of different CSS properties to make it easier to write multiple properties at once, precisely like the background property where we can write something like this: </p> <p>body {<br /> background: url(sweettexture.jpg) top center no-repeat fixed padding-box content-box red;<br /> } </p> <p>I try to avoid shorthand properties because they can get pretty confusing and I often tend to write the long hand versions just because my brain fails to parse long lines of property values. But it’s recommended to use the shorthand when it comes to flexbox, which is…weird… that is, until you understand that the flex property is doing a lot of work and each of its sub-properties interact with the others. </p> <p>Also, the default styles are a good thing because we don’t need to know what these flexbox properties are doing 90% of the time. For example, when I use flexbox, I tend to write something like this: </p> <p>.parent {<br /> display: flex;<br /> justify-content: space-between;<br /> } </p> <p>I don’t even need to care about the child elements or what styles have been applied to them, and that’s great! In this case, we’re aligning the child items side-by-side and then spacing them equally between each other. Two lines of CSS gives you a lot of power here and that’s the neatest thing about flexbox and these inherited styles — you don’t have to understand all the complexity under the hood if you just want to do the same thing 90% of the time. It’s remarkably smart because all of that complexity is hidden out of view. </p> <p>But what if we want to understand how flexbox — including the flex-grow, flex-shrink, and flex-basis properties — actually work? And what cool things can we do with them? </p> <p>Just go to the CSS-Tricks Almanac. Done! </p> <p>Just kidding. Let’s start with a quick overview that’s a little bit simplified, and return to the default flex properties that are applied to child elements: </p> <p>.child {<br /> flex: 0 1 auto;<br /> } </p> <p>These default styles are telling that child element how to stretch and expand. But whenever I see it being used or overridden, I find it helpful to think of these shorthand properties like this: </p> <p>/* This is just how I think about the rule above in my head */ </p> <p>.child {<br /> flex: [flex-grow] [flex-shrink] [flex-basis];<br /> } </p> <p>/* or&#8230; */ </p> <p>.child {<br /> flex: [max] [min] [ideal size];<br /> } </p> <p>That first value is flex-grow and it’s set to 0 because, by default, we don’t want our elements to expand at all (most of the time). Instead, we want every element to be dependent on the size of the content within it. Here’s an example: </p> <p>.parent {<br /> display: flex;<br /> } </p> <p>CodePen Embed Fallback </p> <p>I’ve added the contenteditable property to each .child element above so you can click into it and type even more content. See how it responds? That’s the default behavior of a flexbox item: flex-grow is set to 0 because we want the element to grow based on the content inside it. </p> <p>But! If we were to change the default of the flex-grow property from 0 to 1, like this… </p> <p>.child {<br /> flex: 1 1 auto;<br /> } </p> <p>Then all the elements will grow to take up an equal portion of the .parent element: </p> <p>CodePen Embed Fallback </p> <p>This is exactly the same as writing… </p> <p>.child {<br /> flex-grow: 1;<br /> } </p> <p>…and ignoring the other values because those have been set by default anyway. I think this confused me for such a long time when I started working with flexible layouts. I would see code that would add just flex-grow and wonder where the other styles are coming from. It was like an infuriating murder mystery that I just couldn’t figure out. </p> <p>Now, if we wanted to make just one of these elements grow more than the others we’d just need to do the following: </p> <p>.child-three {<br /> flex: 3 1 auto;<br /> } </p> <p>/* or we could just write&#8230; */ </p> <p>.child-three {<br /> flex-grow: 3;<br /> } </p> <p>CodePen Embed Fallback </p> <p>Is this weird code to look at even a decade after flexbox landed in browsers? It certainly is for me. I need extra brain power to say, “Ah, max, min, ideal size,” when I’m reading the shorthand, but it does get easier over time. Anyway, in the example above, the first two child elements will take up proportionally the same amount of space but that third element will try to grow up to three times the space as the others. </p> <p>Now this is where things get weird because this is all dependent on the content of the child elements. Even if we set flex-grow to 3, like we did in the example above and then add more content, the layout will do something odd and peculiar like this: </p> <p>CodePen Embed Fallback </p> <p>That second column is now taking up too much darn space! We’ll come back to this later, but for now, it’s just important to remember that the content of a flex item has an impact on how flex-grow, flex-shrink, and flex-basis work together. </p> <p>OK so now for flex-shrink. Remember that’s the second value in the shorthand: </p> <p>.child {<br /> flex: 0 1 auto; /* flex-shrink = 1 */<br /> } </p> <p>flex-shrink tells the browser what the minimum size of an element should be. The default value is 1, which is saying, “Take up the same amount of space at all times.” However! If we were to set that value to 0 like this: </p> <p>.child {<br /> flex: 0 0 auto;<br /> } </p> <p>…then we’re telling this element not to shrink at all now. Stay the same size, you blasted element! is essentially what this CSS says, and that’s precisely what it’ll do. We’ll come back to this property in a bit once we look at the final value in this shorthand. </p> <p>flex-basis is the last value that’s added by default in the flex shorthand, and it’s how we tell an element to stick to an ideal size. By default, it’s set to auto which means, “Use my height or width.” So, when we set a parent element to display: flex… </p> <p>.parent {<br /> display: flex;<br /> } </p> <p>.child {<br /> flex: 0 1 auto;<br /> } </p> <p>We’ll get this by default in the browser: </p> <p>CodePen Embed Fallback </p> <p>Notice how all the elements are the width of their content by default? That’s because auto is saying that the ideal size of our element is defined by its content. To make all the elements take up the full space of the parent we can set the child elements to width: 100%, or we can set the flex-basis to 100%, or we can set flex-grow to 1. </p> <p>Does that make sense? It’s weird, huh! It does when you think about it. Each of these shorthand values impact the other and that’s why it is recommended to write this shorthand in the first place rather than setting these values independently of one another. </p> <p>OK, moving on. When we write something like this… </p> <p>.child-three {<br /> flex: 0 1 1000px;<br /> } </p> <p>What we’re telling the browser here is to set the flex-basis to 1000px or, “please, please, please just try and take up 1000px of space.” If that’s not possible, then the element will take up that much space proportionally to the other elements. </p> <p>CodePen Embed Fallback </p> <p>You might notice that on smaller screens this third element is not actually a 1000px! That’s because it’s really a suggestion. We still have flex-shrink applied which is telling the element to shrink to the same size as the other elements. </p> <p>Also, adding more content to the other children will still have an impact here: </p> <p>CodePen Embed Fallback </p> <p>Now, if we wanted to prevent this element from shrinking at all we could write something like this: </p> <p>.child-three {<br /> flex: 0 0 1000px;<br /> } </p> <p>Remember, flex-shrink is the second value here and by setting it to 0 we’re saying, “Don’t shrink ever, you jerk.” And so it won’t. The element will even break out of the parent element because it’ll never get shorter than 1000px wide: </p> <p>CodePen Embed Fallback </p> <p>Now all of this changes if we set flex-wrap to the parent element: </p> <p>.parent {<br /> display: flex;<br /> flex-wrap: wrap;<br /> } </p> <p>.child-three {<br /> flex: 0 0 1000px;<br /> } </p> <p>We’ll see something like this: </p> <p>CodePen Embed Fallback </p> <p>This is because, by default, flex items will try to fit into one line but flex-wrap: wrap will ignore that entirely. Now, if those flex items can’t fit in the same space, they’ll break onto a new line. </p> <p>Anyway, this is just some of the ways in which flex properties bump into each other and why it’s so gosh darn valuable to understand how these properties work under the hood. Each of these properties can affect the other, and if you don’t understand how one property works, then you sort of don’t understand how any of it works at all — which certainly confused me before I started digging into this! </p> <p>But to summarize: </p> <p>Try to use the flex shorthand Remember min, max, and ideal size when doing soRemember that the content of an element can impact how these values work together, too. </p> <p>The post Understanding flex-grow, flex-shrink, and flex-basis appeared first on CSS-Tricks.<br /> You can support CSS-Tricks by being an MVP Supporter.</p>


When you apply a CSS property to an element, there’s lots of things going on under the hood. For example, let’s say we have some HTML like this:

<div class=”parent”> <div class=”child”>Child</div> <div class=”child”>Child</div> <div class=”child”>Child</div> </div>

And then we