What Are CSS place-items and place-content?

CSS layout modules like Flexbox and Grid provide powerful alignment tools, and place-items and place-content serve as shorthand properties designed to streamline how elements and tracks are positioned. Instead of declaring separate rules for vertical and horizontal axes, these shorthands allow developers to set both alignment dimensions in a single line of CSS. Understanding the distinction between aligning individual items inside cells versus distributing total layout content across a container is essential for writing clean, maintainable stylesheets.

Understanding Alignment Dimensions

CSS Box Alignment defines alignment along two primary axes:

  • Block Axis (Vertical in horizontal writing modes): Controlled by align-* properties (align-items, align-content, align-self).
  • Inline Axis (Horizontal in horizontal writing modes): Controlled by justify-* properties (justify-items, justify-content, justify-self).

Both shorthand properties accept either one or two values:

  • If two values are provided, the first sets the block alignment (align-*) and the second sets the inline alignment (justify-*).
  • If a single value is provided, it is applied to both axes equally.
/* Syntax Structure */
selector {
  property: <align-axis> <justify-axis>;
  /* or */
  property: <both-axes>;
}

The place-items Shorthand

The place-items property combines align-items and justify-items. It specifies how individual items are aligned inside their respective grid areas or flex lines.

/* Shorthand declaration */
.grid-container {
  display: grid;
  place-items: center;
}

/* Equivalent longhand declarations */
.grid-container {
  display: grid;
  align-items: center;
  justify-items: center;
}

Key Behaviors of place-items

  • Single-element Centering: In CSS Grid, setting display: grid; place-items: center; on a parent element is the most concise way to center a child element both vertically and horizontally.
  • Two-value Usage: Declaring place-items: start end; aligns items to the top (start of the block axis) and to the right (end of the inline axis in left-to-right text).
  • Flexbox Compatibility: While align-items is standard in Flexbox, justify-items has limited to no effect on flex containers because items are distributed along the main axis via justify-content. Consequently, place-items is predominantly used in CSS Grid layouts.

The place-content Shorthand

The place-content property combines align-content and justify-content. It determines the distribution and alignment of the entire layout structure (such as grid tracks or flex lines) within the container when the total size of the items is smaller than the container itself.

/* Shorthand declaration */
.container {
  display: flex;
  place-content: center space-between;
}

/* Equivalent longhand declarations */
.container {
  display: flex;
  align-content: center;
  justify-content: space-between;
}

Key Behaviors of place-content

  • Track and Line Distribution: If a grid or multi-line flex container has explicit dimensions (e.g., height: 600px; width: 100%;) and the content does not fill that space, place-content dictates where the group of rows and columns sits as a whole.
  • Spacing Keywords: Supports distribution values such as space-between, space-around, space-evenly, and stretch, as well as positional values like center, start, and end.
  • Two-value Usage: Declaring place-content: space-between center; distributes rows evenly along the vertical axis while grouping columns in the center along the horizontal axis.

place-items vs. place-content

The choice between place-items and place-content depends on what part of the layout needs positioning:

  • Use place-items when adjusting the position of children within their assigned grid cells or bounds.
  • Use place-content when adjusting the position of the entire grid or flex system within the parent container boundaries.

Using these shorthands eliminates repetitive CSS declarations, reduces file size, and provides a clear, declarative approach to managing multi-directional layouts across modern web applications.