CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Align Items


This property aligns flex items along the cross axis of the current line of the flex container. The cross axis is perpendicular to the main axis.

Example 1: Stretching to Fill (Default)

HTML

<div class="flex-container align-stretch">
  <div class="item">One</div>
  <div class="item" style="font-size: 24px;">Two</div>
  <div class="item">Three</div>
</div>

CSS

.flex-container {
  display: flex;
  height: 150px; /* Container needs height for alignment to be visible. */
  border: 2px solid #000;
}

.align-stretch {
  align-items: stretch; /* Stretches items to fill the container's height (default). */
}

Explanation

align-items: stretch is the default value. It causes flex items to stretch to fill the height (or width, in a column layout) of the flex container along the cross axis.


Example 2: Aligning to Flex Start

HTML

<div class="flex-container align-flex-start">
  <div class="item">One</div>
  <div class="item" style="font-size: 24px;">Two</div>
  <div class="item">Three</div>
</div>

CSS

.flex-container {
  display: flex;
  height: 150px;
  border: 2px solid #000;
}

.align-flex-start {
  align-items: flex-start; /* Aligns items to the start of the cross axis. */
}

Explanation

align-items: flex-start aligns all items to the start of the cross axis (the top edge in a standard row layout).


Example 3: Aligning to Flex End

HTML

<div class="flex-container align-flex-end">
  <div class="item">One</div>
  <div class="item" style="font-size: 24px;">Two</div>
  <div class="item">Three</div>
</div>

CSS

.flex-container {
  display: flex;
  height: 150px;
  border: 2px solid #000;
}

.align-flex-end {
  align-items: flex-end; /* Aligns items to the end of the cross axis. */
}

Explanation

align-items: flex-end aligns all items to the end of the cross axis (the bottom edge in a standard row layout).


Example 4: Aligning to the Center

HTML

<div class="flex-container align-center">
  <div class="item">One</div>
  <div class="item" style="font-size: 24px;">Two</div>
  <div class="item">Three</div>
</div>

CSS

.flex-container {
  display: flex;
  height: 150px;
  border: 2px solid #000;
}

.align-center {
  align-items: center; /* Centers items along the cross axis. */
}

Explanation

align-items: center is a very common value used to vertically center a group of items within a container.


Example 5: Aligning by Baseline

HTML

<div class="flex-container align-baseline">
  <div class="item" style="font-size: 12px;">Small</div>
  <div class="item" style="font-size: 24px;">Medium</div>
  <div class="item" style="font-size: 36px;">Large</div>
</div>

CSS

.flex-container {
  display: flex;
  height: 150px;
  border: 2px solid #000;
}

.align-baseline {
  align-items: baseline; /* Aligns items based on their text's baseline. */
}

Explanation

align-items: baseline aligns items so that their text baselines are aligned. This is useful for ensuring text across different-sized elements lines up properly.


Example 6: Align Items in a Column Layout

HTML

<div class="flex-container column-align">
  <div class="item">Item 1</div>
  <div class="item">Longer Item 2</div>
  <div class="item">Item 3</div>
</div>

CSS

.flex-container {
  display: flex;
  flex-direction: column; /* Main axis is vertical. */
  height: 200px;
  border: 2px solid #d9534f;
}

.column-align {
  align-items: flex-end; /* Aligns items to the right side of the container. */
}

Explanation

When flex-direction is column, the cross axis becomes horizontal. Therefore, align-items: flex-end aligns the items to the right edge of the container.


Example 7: Overriding align-items with align-self

HTML

<div class="flex-container override-align">
  <div class="item">Start</div>
  <div class="item self-center">Center</div>
  <div class="item self-end">End</div>
</div>

CSS

.flex-container {
  display: flex;
  height: 150px;
  border: 2px solid #337ab7;
}

.override-align {
  align-items: flex-start; /* All items default to aligning at the top. */
}

.self-center { align-self: center; } /* This item overrides the parent and centers itself. */
.self-end { align-self: flex-end; } /* This item aligns itself to the bottom. */

Explanation

While align-items sets the default alignment for all items, the align-self property can be applied to an individual flex item to override its alignment.