CSS justify-items eigenschap

Definition and Usage

The justify-items attribute is set on the grid container to align child elements (grid items) in the inline direction.

For English pages, the inline direction is from left to right, and the block direction is downward.

For this attribute to have any alignment effect, the grid item must have available space around itself in the inline direction.

Tip:To align grid items in the block direction instead of the inline direction, use align-items attribute Attributes.

See also:

CSS Tutorial:CSS Grid

CSS Tutorial:CSS Positioning

CSS Reference Manual:align-items attribute

CSS Reference Manual:direction attribute

CSS Reference Manual:grid attribute

CSS Reference Manual:grid-template-columns attribute

CSS Reference Manual:justify-self attribute

CSS Reference Manual:position attribute

CSS Reference Manual:writing-mode attribute

Instance

Example 1

Align each grid item at the end of its grid cell along the inline direction:

#container {
  display: grid;
  justify-items: end;
}

亲自试一试

Example 2: Comparison of justify-items and justify-self

The alignment relative to the container is set to 'centered', and the grid item itself is set to 'right aligned'. The attribute value 'right' takes precedence:

#container {
  display: grid;
  justify-items: center;
}
.blue {
  justify-self: right;
}

亲自试一试

Example 3: Set justify-items on an absolutely positioned grid item

Set the alignment of the grid items with absolute positioning to 'right aligned':

#container {
  display: grid;
  position: relative;
  justify-items: right;
}
.blue {
  position: absolute;
}

亲自试一试

Example 4: writing-mode

When the writing-mode attribute value of the grid container element is set to vertical-rl, the inline direction is downward. The result is that the container's starting point moves from the left to the top, and the container's end moves from the right to the bottom:

#container {
  justify-items: end;
  writing-mode: vertical-rl;
}

亲自试一试

Voorbeeld 5: richting

当网格容器元素的 direction 属性值设置为“rtl”时,行内方向是从右到左。结果是容器的起点从左侧移到右侧,容器的末端从右侧移到左侧:

#container {
  justify-items: start;
  direction: rtl;
}

亲自试一试

CSS 语法

justify-items: legacy|normal|stretch|positional alignment|overflow-alignment|baseline alignment|initial|inherit;

属性值

描述
legacy

默认值。

仅在以'legacy'开头时,justify-self 值为'auto'的网格项继承网格容器的 justify-items 属性值。

它的存在是为了实现 HTML 的 <center> 元素和 align 属性的遗留对齐行为。

normal 取决于布局上下文,但类似于网格布局的 'stretch'。
stretch 如果未设置 inline-size(宽度),则拉伸以填充网格单元格。
start 在行内方向开头对齐项目。
left 将项目左对齐。
center 将项目对齐到中心。
end 在行内方向的末尾对齐项目。
right 将项目右对齐。
overflow-alignment
  • 'safe' 会将项目的对齐方式设置为 'start',如果内容溢出。
  • 'unsafe' 保持对齐值,无论项目内容是否溢出。
baseline-alignment 元素与父元素的基线对齐。
initial 将此属性设置为其默认值。参阅 initial
inherit 从其父元素继承此属性。参阅 inherit

技术细节

默认值: legacy
继承:
动画制作: 不支持。请参阅:动画相关属性
版本: CSS3
JavaScript 语法: object.style.justifyItems="center"

浏览器支持

表中的数字注明了首个完全支持该属性的浏览器版本。

Chrome Edge Firefox Safari Opera
Chrome IE / Edge Firefox Safari Opera
57.0 16.0 45.0 10.1 44.0