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 Self


The align-self property allows you to override the align-items property for a single flex item. It controls the alignment of an individual item along the cross axis of the flex container.

Example 1: align-self: center

.flex-container {
  display: flex;
  height: 200px;
  align-items: flex-start; /* All items are aligned to the top by default */
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  background-color: moccasin;
  border: 1px solid #000;
}

.center-me {
  align-self: center; /* This item is centered on the cross-axis */
}

Explanation

While the container has align-items: flex-start, the .center-me item is vertically centered because align-self: center overrides the container's alignment property.


Example 2: align-self: flex-end

.flex-container {
  display: flex;
  height: 200px;
  align-items: center; /* All items are centered by default */
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  background-color: navajowhite;
  border: 1px solid #000;
}

.end-me {
  align-self: flex-end; /* This item is aligned to the bottom */
}

Explanation

In this case, the .end-me item is aligned to the end of the cross axis (the bottom), even though the other items are centered according to the align-items property of the container.


Example 3: align-self: flex-start

.flex-container {
  display: flex;
  height: 200px;
  align-items: center;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  background-color: peachpuff;
  border: 1px solid #000;
}

.start-me {
  align-self: flex-start; /* This item is aligned to the top */
}

Explanation

The .start-me item is aligned to the start of the cross axis (the top), overriding the align-items: center set on the flex container.


Example 4: align-self: stretch

.flex-container {
  display: flex;
  height: 200px;
  align-items: flex-start; /* Items don't stretch by default */
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  background-color: papayawhip;
  border: 1px solid #000;
}

.stretch-me {
  align-self: stretch; /* This item stretches to fill the container's height */
}

Explanation

align-self: stretch causes the .stretch-me item to stretch to fill the height of the flex container, ignoring the align-items: flex-start property. This is the default align-items behavior if no height is set on the item.


Example 5: align-self: baseline

.flex-container {
  display: flex;
  height: 200px;
  align-items: flex-start;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  background-color: mistyrose;
  border: 1px solid #000;
}

.baseline-me {
  font-size: 2em;
  align-self: baseline; /* Aligns based on the text baseline */
}

Explanation

The .baseline-me item is aligned with the other items based on their text's baseline. This is useful for aligning items that have different font sizes.


Example 6: auto value

.flex-container {
  display: flex;
  height: 200px;
  align-items: center;
  border: 1px solid #000;
}

.flex-item {
  width: 100px;
  background-color: oldlace;
  border: 1px solid #000;
  align-self: auto; /* Inherits the align-items value from the container */
}

Explanation

The auto value for align-self is the default, and it simply inherits the align-items value from the parent flex container. In this case, the item will be centered.


Example 7: Using with different flex-direction

.flex-container {
  display: flex;
  height: 200px;
  flex-direction: column; /* Main axis is now vertical */
  align-items: center; /* Aligns items horizontally */
  border: 1px solid #000;
}

.flex-item {
  height: 50px;
  background-color: linen;
  border: 1px solid #000;
}

.align-me {
  align-self: flex-end; /* Aligns the item to the right edge */
}

Explanation

When flex-direction is column, the cross axis becomes horizontal. align-self: flex-end will therefore align the .align-me item to the right edge of the container.