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 Items


The justify-items property is a sub-property of the CSS Grid Layout module. It is used to align the content of all grid items along the inline (main) axis within their respective grid areas.


Example 1: justify-items: start

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  justify-items: start; /* Aligns items to the start of their grid area */
  gap: 10px;
  background-color: #f7f7f7;
  padding: 10px;
}

.grid-item {
  background-color: #3498db;
  padding: 20px;
  color: white;
}

Explanation

This code aligns all grid items to the beginning of their respective grid cells along the horizontal axis. The items will be flush with the left edge of their grid area.


Example 2: justify-items: end

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  justify-items: end; /* Aligns items to the end of their grid area */
  gap: 10px;
  background-color: #f7f7f7;
  padding: 10px;
}

.grid-item {
  background-color: #e74c3c;
  padding: 20px;
  color: white;
}

Explanation

This example demonstrates the end value, which aligns all grid items to the far right of their grid cell along the inline axis.


Example 3: justify-items: center

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  justify-items: center; /* Centers items within their grid area */
  gap: 10px;
  background-color: #f7f7f7;
  padding: 10px;
}

.grid-item {
  background-color: #2ecc71;
  padding: 20px;
  color: white;
}

Explanation

The center value for justify-items horizontally centers the content within each grid item's designated area. This is a common choice for creating balanced layouts.


Example 4: justify-items: stretch

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  justify-items: stretch; /* Stretches items to fill their grid area */
  gap: 10px;
  background-color: #f7f7f7;
  padding: 10px;
}

.grid-item {
  background-color: #f1c40f;
  /* No width is set, so the item will stretch */
  color: white;
  padding: 20px;
}

Explanation

stretch is the default value for justify-items. It causes the grid items to expand horizontally, filling the entire width of their grid area.


Example 5: justify-items: self-start

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  justify-items: center; /* Default alignment for the container */
  gap: 10px;
  background-color: #f7f7f7;
  padding: 10px;
}

.grid-item {
  background-color: #9b59b6;
  padding: 20px;
  color: white;
}

.special-item {
  justify-self: self-start; /* Overrides the container's alignment for this specific item */
}

Explanation

While justify-items sets the alignment for all items, justify-self can override it for a single item. Here, self-start aligns a specific item to the start, contrasting with the centered group.


Example 6: justify-items: self-end

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  justify-items: start; /* Default alignment for the container */
  gap: 10px;
  background-color: #f7f7f7;
  padding: 10px;
}

.grid-item {
  background-color: #1abc9c;
  padding: 20px;
  color: white;
}

.another-item {
  justify-self: self-end; /* Aligns this specific item to the end */
}

Explanation

Similar to the previous example, self-end is used with the justify-self property to target an individual grid item. It positions the item at the far right of its grid cell, overriding the container's start alignment.


Example 7: justify-items with different content sizes

.grid-container {
  display: grid;
  grid-template-columns: auto auto;
  justify-items: center; /* Center aligning items of different widths */
  gap: 15px;
  background-color: #f0e68c;
  padding: 15px;
}

.grid-item {
  background-color: #d2691e;
  padding: 15px;
  color: white;
}

.wide-item {
  width: 200px; /* A wider item to show alignment */
}

Explanation

This code illustrates how justify-items: center works with items of varying widths. Each item is centered within its respective grid track, regardless of its own size.