Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære The Flex Shorthand Property | Aligning and Distributing Elements
CSS Flexbox Foundations

bookThe Flex Shorthand Property

The flex shorthand property is a powerful way to control how flex items grow, shrink, and set their initial size along the main axis. Instead of writing out flex-grow, flex-shrink, and flex-basis separately for each item, you can use the flex property to set all three at once. The syntax for the flex property is:

flex: <flex-grow> <flex-shrink> <flex-basis>;

The order of values is important:

  • First, specify the grow factor (flex-grow);
  • Next, the shrink factor (flex-shrink);
  • Last, the basis (flex-basis), which sets the initial main size.

Common patterns include:

  • flex: 1; is a shortcut for flex: 1 1 0%, making the item flexible and able to grow and shrink as needed, starting from zero width;
  • flex: 0 1 auto; the default value, allows shrinking but not growing, with the item's initial size based on its content.
index.html

index.html

copy

In the example above, the .item1 and .item2 elements each use the flex shorthand to combine their grow, shrink, and basis values. For .item1, flex: 2 1 100px; means the item will try to grow twice as fast as .item2 when there is extra space (because its grow factor is 2, compared to 1 for .item2). The shrink values (1 for .item1, 2 for .item2) control how quickly each item gives up space when the container gets smaller. The basis values (100px for .item1, 50px for .item2) set the starting widths before any growing or shrinking happens. By combining all three controls in a single line, the flex shorthand makes it easy to manage flexible layouts that respond smoothly to changes in container size.

question mark

What do the three values in the CSS flex shorthand property represent, and in what order are they written?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain more about how the grow and shrink factors interact in practice?

What happens if I omit one or more values in the `flex` shorthand?

Can you give more examples of common `flex` shorthand patterns?

Awesome!

Completion rate improved to 7.69

bookThe Flex Shorthand Property

Stryg for at vise menuen

The flex shorthand property is a powerful way to control how flex items grow, shrink, and set their initial size along the main axis. Instead of writing out flex-grow, flex-shrink, and flex-basis separately for each item, you can use the flex property to set all three at once. The syntax for the flex property is:

flex: <flex-grow> <flex-shrink> <flex-basis>;

The order of values is important:

  • First, specify the grow factor (flex-grow);
  • Next, the shrink factor (flex-shrink);
  • Last, the basis (flex-basis), which sets the initial main size.

Common patterns include:

  • flex: 1; is a shortcut for flex: 1 1 0%, making the item flexible and able to grow and shrink as needed, starting from zero width;
  • flex: 0 1 auto; the default value, allows shrinking but not growing, with the item's initial size based on its content.
index.html

index.html

copy

In the example above, the .item1 and .item2 elements each use the flex shorthand to combine their grow, shrink, and basis values. For .item1, flex: 2 1 100px; means the item will try to grow twice as fast as .item2 when there is extra space (because its grow factor is 2, compared to 1 for .item2). The shrink values (1 for .item1, 2 for .item2) control how quickly each item gives up space when the container gets smaller. The basis values (100px for .item1, 50px for .item2) set the starting widths before any growing or shrinking happens. By combining all three controls in a single line, the flex shorthand makes it easy to manage flexible layouts that respond smoothly to changes in container size.

question mark

What do the three values in the CSS flex shorthand property represent, and in what order are they written?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5
some-alt