"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > What\'s the difference between \"align-items\" and \"align-content\" in CSS Grid Layout?

What\'s the difference between \"align-items\" and \"align-content\" in CSS Grid Layout?

Published on 2024-11-15
Browse:722

What\'s the difference between \

Understanding the Distinction between "align-items" and "align-content" in Grid Layout

The Grid Layout module introduces two sets of properties, "justify/align-items" and "justify/align-content," which play distinct roles in aligning grid items and the grid itself within a grid container.

Grid Terminology Clarification

  • Grid Container: The parent container that defines the overall grid space.
  • Grid: A structured grid of lines that divides the grid container into grid areas.
  • Grid Items: Boxes that contain in-flow content within the grid areas.

Align-items vs. Align-content

The "align-items" and "align-content" properties, as their names suggest, align grid items and the grid, respectively. Specifically:

  • justify-content and align-content align the grid tracks within the content box of the grid container.
  • justify-self and justify-items align grid items in the inline dimension (horizontal by default).
  • align-self and align-items align grid items in the block dimension (vertical by default).

Addressing the Author's Claim

The author's claim that the "-content" properties exist because "sometimes the total size of your grid might be less than the size of its grid container" highlights the following:

  • When the grid is smaller than the grid container, there is free space available.
  • The "justify-content" and "align-content" properties can utilize this space to align the grid centrally or otherwise within the container.
  • In contrast, if the grid size equals the container size, there is no free space, and these alignment properties have no effect.

Illustrations for Clarity

[Image of illustration from W3C showing a grid smaller than its container, with "justify-content" and "align-content" aligning the grid within the container.]

Excerpt from the Specification

For clarity, relevant excerpts from the CSS Grid Layout specification are provided:

  • "Grid items can be aligned in the inline dimension by using the justify-self property on the grid item or justify-items property on the grid container."
  • "Grid items can also be aligned in the block dimension by using the align-self property on the grid item or align-items property on the grid container."
  • "If the grid’s outer edges do not correspond to the grid container’s content edges, the grid tracks are aligned within the content box according to the justify-content and align-content properties on the grid container."
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3