Skip to content

FlexBox

This component provides a comprehensive set of utility classes for creating flexible layouts using CSS Flexbox in Metro UI.

The flex component defines a wide range of CSS classes that implement all aspects of the Flexbox layout model. These utilities make it easy to create responsive, flexible layouts without writing custom CSS.

These classes set the display property to create flex containers:

ClassDescription
.d-flexCreates a block-level flex container
.d-inline-flexCreates an inline-level flex container

Controls the direction of flex items within a container:

ClassDescription
.flex-rowItems are placed in a row (default)
.flex-row-reverseItems are placed in a row in reverse order
.flex-columnItems are placed in a column
.flex-column-reverseItems are placed in a column in reverse order

Controls whether flex items wrap onto multiple lines:

ClassDescription
.flex-nowrapItems will not wrap (default)
.flex-wrapItems will wrap onto multiple lines if needed
.flex-wrap-reverseItems will wrap onto multiple lines in reverse order

Controls the spacing between flex items:

ClassDescription
.gap-0 through .gap-12Sets gap between items from 0px to 48px (in increments of 4px)

Controls alignment of items along the main axis:

ClassDescription
.flex-justify-startItems are packed at the start of the container
.flex-justify-endItems are packed at the end of the container
.flex-justify-centerItems are centered along the line
.flex-justify-betweenItems are evenly distributed with the first item at the start and the last at the end
.flex-justify-aroundItems are evenly distributed with equal space around them
.flex-justify-evenlyItems are evenly distributed with equal space between them

Note: There are also longer-form classes like .flex-justify-content-start that do the same thing.

Controls alignment of items along the cross axis:

ClassDescription
.flex-align-startItems are aligned at the start of the cross axis
.flex-align-endItems are aligned at the end of the cross axis
.flex-align-centerItems are centered along the cross axis
.flex-align-baselineItems are aligned by their baselines
.flex-align-stretchItems are stretched to fill the container (default)

Note: There are also longer-form classes like .flex-align-items-start that do the same thing.

Controls alignment of lines within a multi-line flex container:

ClassDescription
.flex-align-content-startLines are packed at the start of the container
.flex-align-content-endLines are packed at the end of the container
.flex-align-content-centerLines are centered in the container
.flex-align-content-betweenLines are evenly distributed with the first at the start and the last at the end
.flex-align-content-aroundLines are evenly distributed with equal space around them
.flex-align-content-evenlyLines are evenly distributed with equal space between them
.flex-align-content-stretchLines stretch to fill the container (default)

Controls the order in which flex items appear:

ClassDescription
.order-0 through .order-24Sets the order of a flex item from 0 to 24

Overrides the container’s align-items property for specific items:

ClassDescription
.flex-align-self-autoUses the parent container’s align-items value
.flex-align-self-startItem is aligned at the start of the cross axis
.flex-align-self-endItem is aligned at the end of the cross axis
.flex-align-self-centerItem is centered along the cross axis
.flex-align-self-baselineItem is aligned by its baseline
.flex-align-self-stretchItem is stretched to fill the container

Controls alignment of a specific item along the main axis:

ClassDescription
.flex-justify-self-autoUses the parent container’s justify-items value
.flex-justify-self-startItem is aligned at the start of the main axis
.flex-justify-self-endItem is aligned at the end of the main axis
.flex-justify-self-centerItem is centered along the main axis
.flex-justify-self-stretchItem is stretched to fill the container

Controls how flex items grow and shrink:

ClassDescription
.flex-grow > *All child items can grow (flex-grow: 1)
.flex-no-grow > *Child items cannot grow (flex-grow: 0)
.flex-shrink > *All child items can shrink (flex-shrink: 1)
.flex-no-shrink > *Child items cannot shrink (flex-shrink: 0)
.flex-grow-selfApplied to an item to make it grow
.flex-no-grow-selfApplied to an item to prevent it from growing
.flex-shrink-selfApplied to an item to make it shrink
.flex-no-shrink-selfApplied to an item to prevent it from shrinking
ClassDescription
.flex-equal-itemsMakes all child items have equal width (flex: 1)
ClassDescription
.flex-rightPushes an item to the right (margin-left: auto)
.flex-leftPushes an item to the left (margin-right: auto)
.flex-topPushes an item to the top (margin-bottom: auto)
.flex-bottomPushes an item to the bottom (margin-top: auto)
.flex-centerCenters items both horizontally and vertically

All of the above classes have responsive variants that apply at specific breakpoints. The responsive variants follow the naming pattern .{class}-{breakpoint}.

For example:

  • .d-flex-md - Creates a flex container on medium screens and above
  • .flex-column-lg - Applies column direction on large screens and above
  • .flex-justify-center-xl - Centers items on extra-large screens and above

Where {breakpoint} can be: xs, sm, md, lg, xl, xxl, xxxl

<div class="d-flex">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Row on small screens, column on medium screens and above -->
<div class="d-flex flex-row flex-column-md">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
<!-- Center items both horizontally and vertically -->
<div class="d-flex flex-center" style="height: 200px;">
<div>Centered Content</div>
</div>
<!-- Distribute items evenly with space between -->
<div class="d-flex flex-justify-between">
<div>Left</div>
<div>Middle</div>
<div>Right</div>
</div>
<div class="d-flex">
<div class="order-3">Appears third</div>
<div class="order-1">Appears first</div>
<div class="order-2">Appears second</div>
</div>
<div class="d-flex flex-equal-items">
<div>Equal</div>
<div>Equal</div>
<div>Equal</div>
</div>
<div class="d-flex">
<div>Left</div>
<div class="flex-right">Right</div>
</div>
<div class="d-flex flex-column flex-row-lg">
<div class="order-2 order-1-lg">First on large screens, second on small screens</div>
<div class="order-1 order-2-lg">Second on large screens, first on small screens</div>
</div>
  1. Start with the basic container class (.d-flex or .d-inline-flex) before adding other flex utilities.
  2. Use responsive variants to create layouts that adapt to different screen sizes.
  3. For complex layouts, combine flex utilities with other Metro UI classes like spacing and sizing utilities.
  4. Use .flex-center for the common pattern of centering content both horizontally and vertically.
  5. Use .flex-equal-items when you want all items to have equal width regardless of their content.
  6. Use the positioning shortcuts (.flex-right, .flex-left, etc.) to push items to different sides of the container.