Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Controlling Flex Items' Behavior | Aligning and Distributing Elements
CSS Flexbox Foundations

bookControlling Flex Items' Behavior

When you need precise control over how your flex items behave inside a container, three properties are essential: flex-grow, flex-shrink, and flex-basis. Each property handles a different aspect of an item's size and flexibility.

  • flex-grow determines how much a flex item will expand to fill extra space;
  • flex-shrink decides how much it will shrink when there is not enough space;
  • flex-basis sets the initial main size of the item before growing or shrinking happens.

These properties interact to create powerful, responsive layouts.

index.html

index.html

copy

In the example above, both items start with a flex-basis of 80px, but they have different flex-grow values. The container is 400px wide, so after the initial sizes and margins are accounted for, there is extra space to distribute. The first item has flex-grow: 2, while the second has flex-grow: 1. This means the available space is split into three parts: the first item gets two parts, and the second gets one. As a result, the first item becomes twice as wide as the second, showing how flex-grow lets you control the proportion of space each item receives when the container has extra room.

index.html

index.html

copy

This second example demonstrates how flex-shrink works. Both items are set to width: 200px, but the container is only 300px wide, so there is not enough space. The first item has flex-shrink: 2, and the second has flex-shrink: 1. This means the first item will shrink twice as much as the second to fit into the container. As a result, the first item becomes noticeably smaller than the second, showing how flex-shrink controls how items reduce their size when space is tight.

index.html

index.html

copy

In this final example, you can see the effect of flex-basis. The first item starts at 120px wide, and the second at 60px, regardless of their content. This property sets the starting size for each item before any growing or shrinking occurs, giving you control over the initial layout. By adjusting flex-basis, you can decide how much space each item should take up by default, making it a key part of building flexible and predictable flexbox layouts.

question mark

Which statement best describes the role of the flex-grow, flex-shrink, and flex-basis properties in flexbox layouts?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 7.69

bookControlling Flex Items' Behavior

Desliza para mostrar el menú

When you need precise control over how your flex items behave inside a container, three properties are essential: flex-grow, flex-shrink, and flex-basis. Each property handles a different aspect of an item's size and flexibility.

  • flex-grow determines how much a flex item will expand to fill extra space;
  • flex-shrink decides how much it will shrink when there is not enough space;
  • flex-basis sets the initial main size of the item before growing or shrinking happens.

These properties interact to create powerful, responsive layouts.

index.html

index.html

copy

In the example above, both items start with a flex-basis of 80px, but they have different flex-grow values. The container is 400px wide, so after the initial sizes and margins are accounted for, there is extra space to distribute. The first item has flex-grow: 2, while the second has flex-grow: 1. This means the available space is split into three parts: the first item gets two parts, and the second gets one. As a result, the first item becomes twice as wide as the second, showing how flex-grow lets you control the proportion of space each item receives when the container has extra room.

index.html

index.html

copy

This second example demonstrates how flex-shrink works. Both items are set to width: 200px, but the container is only 300px wide, so there is not enough space. The first item has flex-shrink: 2, and the second has flex-shrink: 1. This means the first item will shrink twice as much as the second to fit into the container. As a result, the first item becomes noticeably smaller than the second, showing how flex-shrink controls how items reduce their size when space is tight.

index.html

index.html

copy

In this final example, you can see the effect of flex-basis. The first item starts at 120px wide, and the second at 60px, regardless of their content. This property sets the starting size for each item before any growing or shrinking occurs, giving you control over the initial layout. By adjusting flex-basis, you can decide how much space each item should take up by default, making it a key part of building flexible and predictable flexbox layouts.

question mark

Which statement best describes the role of the flex-grow, flex-shrink, and flex-basis properties in flexbox layouts?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
some-alt