CSS Stretch

The CSS stretch property is primarily used in layouts where elements need to expand to fill available space. This concept is commonly applied in Flexbox and Grid layouts, where items can stretch to fit the container’s size. The property enhances the responsiveness of web pages and ensures that elements take up appropriate space depending on the container’s dimensions.

Key Points on CSS Stretch:

Keyword Description
normal This is the default value, which does not stretch any font.
semi-condensed It slightly condensed the text characters of the element. This value makes the text narrower than normal but not narrower than condensed.
condensed This value makes the text narrower than semi-condensed but not narrower than extra-condensed.
extra-condensed This value makes the text narrower than condensed but not narrower than ultra-condensed.
ultra-condensed This value makes the text extremely narrowed.
semi-expanded It slightly widened the text characters of the element. This value makes the text wider than normal but not wider than expanded.
expanded This value makes the text wider than semi-expanded but not wider than extra-expanded.
extra-expanded This value makes the text wider than expanded but not wider than ultra-expanded.
ultra-expanded This value makes the text extremely wider.

Syntax for CSS Stretch in Flexbox:

Syntax Example

/* Stretching flex items */
        .container {
            display: flex;
            align-items: stretch; /* Stretch items along the cross axis */
        }
        
        .item {
            /* Optional: Define specific height for better visibility */
            height: 100px;
        }

Examples of CSS Stretch:

The following examples demonstrate how to use the stretch property in Flexbox and Grid layouts:

Code Example: Stretch in Flexbox


            .container {
            display: flex;
            align-items: stretch;
                       }
        
            .item {
            background-color: lightcoral;
            height: 100px;
                  }

Output

This flex item stretches to fill the available space.

Code Example: Stretch in Grid


            .container {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            align-items: stretch;
                        }
        
            .item {
            background-color: lightblue;
                  }

Output

This grid item stretches to fill the available space.

Code Example: Stretch with Justify Items (Grid)


            .container {
            display: grid;
            justify-items: stretch;
            grid-template-columns: repeat(3, 1fr);
                       }
        
            .item {
            background-color: lavender;
            height: 100px;
                  }

Output

This grid item stretches horizontally.

Common Use Cases for CSS Stretch:

Considerations When Using Stretch:

The CSS stretch property is highly effective when creating fluid and responsive layouts. It ensures elements expand or contract as needed to fill available space in a container, making it a versatile tool in modern web design.