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

Justify Self


The justify-self CSS property is a sub-property of the CSS Box Alignment module. It is used to set the alignment of an individual grid item along the inline (main) axis within a grid container.


Example 1: justify-self: start

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: 100%;
}

.item-1 {
  justify-self: start; /* Aligns the item to the start of its grid area */
  background-color: lightblue;
}

Explanation

This code aligns the grid item with the class .item-1 to the very beginning of its designated grid cell along the row or inline axis. The start value positions the item at the leftmost edge of its area.


Example 2: justify-self: end

.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}

.item-2 {
  justify-self: end; /* Aligns the item to the end of its grid area */
  background-color: lightcoral;
}

Explanation

Here, justify-self: end; pushes the grid item (.item-2) to the far right edge of its grid area. This is the opposite of the start alignment, placing it at the end of the inline axis.


Example 3: justify-self: center

.grid-container {
  display: grid;
  grid-template-columns: auto auto;
}

.item-3 {
  justify-self: center; /* Horizontally centers the item within its grid area */
  background-color: lightgreen;
}

Explanation

The center value perfectly centers the grid item horizontally within its cell. The item (.item-3) will have equal space on its left and right sides inside its grid track.


Example 4: justify-self: stretch

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.item-4 {
  justify-self: stretch; /* Stretches the item to fill the grid area's width */
  background-color: lightgoldenrodyellow;
}

Explanation

stretch is the default value for justify-self. It makes the grid item expand to completely fill the entire width of its grid cell along the inline axis.


Example 5: justify-self with auto

.grid-container {
  display: grid;
  justify-items: center; /* Center all items by default */
  grid-template-columns: 1fr 1fr;
}

.item-5 {
  justify-self: auto; /* Inherits the justify-items value from the container */
  background-color: lightpink;
}

Explanation

The auto value inherits the justify-items property from the grid container. In this case, since the container has justify-items: center, .item-5 will also be centered.


Example 6: Overriding container alignment

.grid-container {
  display: grid;
  justify-items: end; /* Default alignment for all items is 'end' */
  grid-template-columns: repeat(2, 1fr);
}

.item-6 {
  justify-self: start; /* Overrides the container's alignment */
  background-color: lightseagreen;
}

Explanation

This demonstrates how justify-self on an item can override the justify-items property of its parent grid container. While other items would align to the end, .item-6 is aligned to the start.


Example 7: justify-self in a complex grid

.grid-container {
  display: grid;
  grid-template-columns: 100px 1fr 100px;
}

.item-7 {
  grid-column: 2 / 3;
  justify-self: center; /* Centers the item in the flexible middle column */
  background-color: lightskyblue;
}

Explanation

Even in a more complex grid layout with fixed and flexible columns, justify-self: center; precisely centers the content of .item-7 within the available space of its specified column.