CSS3 Flex - Stretch Width and Height
Categories:
CSS3 Flexbox: Mastering Width and Height Stretching

Learn how to effectively use CSS3 Flexbox properties to control the width and height of flex items, ensuring they stretch or shrink as needed to fill available space or maintain proportions.
CSS3 Flexbox is a powerful layout module designed for one-dimensional layouts, meaning it can arrange items in a row or a column. One of its most useful features is the ability to control how flex items grow and shrink to fill the available space within their container. This article will delve into the core properties that allow you to stretch the width and height of flex items, providing practical examples and best practices.
Understanding Flex Item Sizing Properties
The primary properties for controlling the size of flex items are flex-grow
, flex-shrink
, and flex-basis
. These three properties are often combined into the flex
shorthand property. Understanding each component is crucial for mastering flex item stretching.
graph TD A[Flex Item Sizing] --> B{Flex Shorthand} B --> C[flex-grow] B --> D[flex-shrink] B --> E[flex-basis] C --> C1["Defines item's ability to grow"] D --> D1["Defines item's ability to shrink"] E --> E1["Defines item's initial size"] C1 & D1 & E1 --> F[Controls Width/Height Stretching]
Relationship between Flexbox sizing properties
flex-grow
: A unitless number that dictates how much a flex item will grow relative to the rest of the flex items in the container. If all items haveflex-grow: 1
, they will share the extra space equally.flex-shrink
: Also a unitless number, this property determines how much a flex item will shrink relative to the rest of the flex items when there isn't enough space in the container.flex-basis
: Defines the default size of an element before any free space is distributed. It can be a length (e.g.,200px
) or a keyword (e.g.,auto
,content
). When set toauto
, the browser looks at the item's width or height property.
Stretching Width with flex-grow
When your flex container has flex-direction: row
(the default), flex-grow
primarily affects the width of the flex items. By assigning a flex-grow
value greater than 0, items will expand to fill any available horizontal space. The proportion of space they take is determined by their flex-grow
value relative to the sum of all flex-grow
values of sibling items.
.container {
display: flex;
width: 100%;
border: 1px solid #ccc;
}
.item {
padding: 20px;
background-color: lightblue;
border: 1px solid blue;
}
.item-grow-1 {
flex-grow: 1; /* Takes 1/3 of available space */
}
.item-grow-2 {
flex-grow: 2; /* Takes 2/3 of available space */
}
.item-no-grow {
flex-grow: 0; /* Will not grow */
width: 100px;
}
CSS demonstrating flex-grow
for horizontal stretching.
<div class="container">
<div class="item item-grow-1">Item 1 (Grow 1)</div>
<div class="item item-grow-2">Item 2 (Grow 2)</div>
<div class="item item-no-grow">Item 3 (No Grow, 100px)</div>
</div>
HTML structure for the flex-grow
example.
flex-grow
only distributes available space. If your items already fill the container due to their flex-basis
or explicit width
, flex-grow
will have no effect.Stretching Height with align-items
and flex-direction: column
When working with flex-direction: column
, flex-grow
will affect the height of items. However, to make items stretch vertically to fill the cross-axis (which is horizontal in a column layout), you'll typically use the align-items
property on the flex container. The default value for align-items
is stretch
, which causes flex items to stretch to fill the container along the cross-axis.
.container-column {
display: flex;
flex-direction: column;
height: 300px; /* Define a height for the container */
width: 300px;
border: 1px solid #ccc;
align-items: stretch; /* Default, but good to be explicit */
}
.item-column {
padding: 10px;
background-color: lightcoral;
border: 1px solid red;
}
.item-column-grow-1 {
flex-grow: 1; /* Takes 1/2 of available vertical space */
}
.item-column-fixed {
flex-grow: 0;
height: 50px; /* Fixed height */
}
CSS for vertical stretching in a column layout.
<div class="container-column">
<div class="item-column item-column-grow-1">Item A (Grow 1)</div>
<div class="item-column item-column-fixed">Item B (Fixed Height)</div>
<div class="item-column item-column-grow-1">Item C (Grow 1)</div>
</div>
HTML structure for the column layout example.
flex-direction: row
container, ensure the container itself has a defined height, and align-items
is set to stretch
(its default value). This will make items fill the container's height.The flex
Shorthand Property
The flex
shorthand property combines flex-grow
, flex-shrink
, and flex-basis
in that order. It's the most common way to define flex item sizing behavior. Common values include:
flex: 1;
(equivalent toflex: 1 1 0%;
): The item will grow and shrink as needed, with aflex-basis
of0%
. This is often used for items that should take up equal space.flex: auto;
(equivalent toflex: 1 1 auto;
): The item will grow and shrink, but itsflex-basis
isauto
, meaning it respects its content size or explicitwidth
/height
before growing/shrinking.flex: none;
(equivalent toflex: 0 0 auto;
): The item will neither grow nor shrink, maintaining its content size or explicitwidth
/height
.
.container-shorthand {
display: flex;
width: 100%;
height: 150px;
border: 1px solid #ccc;
align-items: stretch; /* To make items stretch vertically */
}
.item-shorthand {
padding: 15px;
background-color: lightgreen;
border: 1px solid green;
}
.item-flex-1 {
flex: 1; /* Grow, shrink, basis 0% */
}
.item-flex-auto {
flex: auto; /* Grow, shrink, basis auto */
width: 150px; /* Will be respected initially */
}
.item-flex-none {
flex: none; /* No grow, no shrink, basis auto */
width: 100px;
}
Using the flex
shorthand property for item sizing.
<div class="container-shorthand">
<div class="item-shorthand item-flex-1">Flex: 1</div>
<div class="item-shorthand item-flex-auto">Flex: auto</div>
<div class="item-shorthand item-flex-none">Flex: none</div>
</div>
HTML for the flex
shorthand example.
flex-basis
with explicit width
or height
properties. flex-basis
takes precedence over width
(for flex-direction: row
) or height
(for flex-direction: column
) when determining the initial size of a flex item, unless flex-basis
is auto
.