CSS Grid中的justify-self,justify-items和justify-content之间的区别

kei*_*e96 36 css css3 css-grid

我真的很困惑.在查找在线资源和文档时,这些属性的大多数文档似乎都引用了Flex-box,而不是grid.我不知道Flex-box中等效属性的文档对网格的适用性如何.

所以,我尝试引用https://css-tricks.com/snippets/css/complete-guide-grid/,它定义如下:

justify-items - 沿着行轴对齐网格项内的内容

justify-content - 此属性沿着行轴对齐网格

justify-self - 沿着行轴对齐网格项内的内容

但我仍然不明白它们之间的区别是什么.所以,我有3个问题要澄清.

  1. justify-items在Flex的框属性相同 justify-items的网格属性?或者它们有什么不同?(换句话说,我可以重用Grid的Flex-box文档)
  2. (证明 - )内容,自我和项目有什么作用?
  3. 如何(证明 - )内容,自我和项目有何不同?

任何澄清将不胜感激.

编辑:由于每个人都不断给我Flex-box资源,我问的是css-grid,而不是flex-box.

L B*_*ahr 44

回答你的问题:

1

正如reiallenramos所提到的那样,"自我辩解和对齐物品属性并没有在flexbox中实现.这是由于flexbox的一维特性,沿着轴可能有多个项目,因此无法证明单个item.要在flexbox中沿主要内联轴对齐项目,请使用justify-content属性." - MDN

2-3

W3拍摄的这个屏幕非常出色地展示了它们的作用以及它们之间的差异. 在此输入图像描述

好知道:

有关更多信息和示例,我建议您查看:

并获得一些灵感:

  • W3的图像不清晰。 (5认同)

Mic*_*l_B 27

主要差异之间justify-content,justify-itemsjustify-self在CSS电网:

  • justify-content属性控制网格列的对齐方式.它设置在网格容器上.它不适用于或控制网格项的对齐.
  • justify-items属性控制网格项的对齐方式.它设置在网格容器上.
  • justify-self属性覆盖justify-items单个项.默认情况下,它在网格项上设置并继承值justify-items.

这是一个2x3网格.

  • 2列,每列100px
  • 3排,每排50px高

容器是:

  • 500px宽
  • 250px高

在此输入图像描述

.container {
    display: grid;
    grid-template-columns: 100px 100px;
    grid-template-rows: 50px 50px 50px;
    width: 500px;
    height: 250px;
    grid-template-areas: " one two"
                         " three four"
                         " five six ";
}

.box:nth-child(1) {  grid-area: one;   }
.box:nth-child(2) {  grid-area: two;   }
.box:nth-child(3) {  grid-area: three; }
.box:nth-child(4) {  grid-area: four;  }
.box:nth-child(5) {  grid-area: five;  }
.box:nth-child(6) {  grid-area: six;   }

/* non-essential decorative styles */
body {
    display: flex;
    justify-content: center;
}
.container {
    background-color: #ddd;
    border: 1px solid #aaa;
}
.box {
    background-color: lightgreen;
    border: 1px solid gray;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2em;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="box"><span>1</span></div>
    <div class="box"><span>2</span></div>
    <div class="box"><span>3</span></div>
    <div class="box"><span>4</span></div>
    <div class="box"><span>5</span></div>
    <div class="box"><span>6</span></div>    
</div>
Run Code Online (Sandbox Code Playgroud)


justify-content

justify-content属性对齐容器中的.

在此输入图像描述

.container {
  justify-content: space-between;
}

.container {
    display: grid;
    grid-template-columns: 100px 100px;
    grid-template-rows: 50px 50px 50px;
    width: 500px;
    height: 250px;
    grid-template-areas: " one two"
                         " three four"
                         " five six ";
}

.box:nth-child(1) {  grid-area: one;   }
.box:nth-child(2) {  grid-area: two;   }
.box:nth-child(3) {  grid-area: three; }
.box:nth-child(4) {  grid-area: four;  }
.box:nth-child(5) {  grid-area: five;  }
.box:nth-child(6) {  grid-area: six;   }

/* non-essential decorative styles */
body {
    display: flex;
    justify-content: center;
}
.container {
    background-color: #ddd;
    border: 1px solid #aaa;
}
.box {
    background-color: lightgreen;
    border: 1px solid gray;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2em;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="box"><span>1</span></div>
    <div class="box"><span>2</span></div>
    <div class="box"><span>3</span></div>
    <div class="box"><span>4</span></div>
    <div class="box"><span>5</span></div>
    <div class="box"><span>6</span></div>    
</div>
Run Code Online (Sandbox Code Playgroud)

随着justify-content: space-between两列都寄托到边缘.网格项只会因为它们存在于这些列中而移动.否则他们不会受到影响.

请注意,此属性仅在容器中有可用空间时有效.如果任何列的大小都是fr,那么将消耗所有可用空间,并且justify-content不会产生任何影响.


justify-items

justify-items属性对齐其轨道中的网格项(而不是整个容器)

在此输入图像描述

.container {
  justify-items: center;
}

.container {
    display: grid;
    grid-template-columns: 100px 100px;
    grid-template-rows: 50px 50px 50px;
    width: 500px;
    height: 250px;
    grid-template-areas: " one two"
                         " three four"
                         " five six ";
}

.box:nth-child(1) {  grid-area: one;   }
.box:nth-child(2) {  grid-area: two;   }
.box:nth-child(3) {  grid-area: three; }
.box:nth-child(4) {  grid-area: four;  }
.box:nth-child(5) {  grid-area: five;  }
.box:nth-child(6) {  grid-area: six;   }

/* non-essential decorative styles */
body {
    display: flex;
    justify-content: center;
}
.container {
    background-color: #ddd;
    border: 1px solid #aaa;
}
.box {
    background-color: lightgreen;
    border: 1px solid gray;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2em;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="box"><span>1</span></div>
    <div class="box"><span>2</span></div>
    <div class="box"><span>3</span></div>
    <div class="box"><span>4</span></div>
    <div class="box"><span>5</span></div>
    <div class="box"><span>6</span></div>    
</div>
Run Code Online (Sandbox Code Playgroud)

随着justify-items: center电网项目的列内居中.


justify-self

justify-self属性覆盖justify-items单个项.

在此输入图像描述

.container        { justify-items: center;}
.box:nth-child(2) { justify-self: start; }
.box:nth-child(3) { justify-self: end; }
.box:nth-child(6) { justify-self: stretch; }


.container {
    display: grid;
    grid-template-columns: 100px 100px;
    grid-template-rows: 50px 50px 50px;
    width: 500px;
    height: 250px;
    grid-template-areas: " one two"
                         " three four"
                         " five six ";
}

.box:nth-child(1) {  grid-area: one;   }
.box:nth-child(2) {  grid-area: two;   }
.box:nth-child(3) {  grid-area: three; }
.box:nth-child(4) {  grid-area: four;  }
.box:nth-child(5) {  grid-area: five;  }
.box:nth-child(6) {  grid-area: six;   }

/* non-essential decorative styles */
body {
    display: flex;
    justify-content: center;
}
.container {
    background-color: #ddd;
    border: 1px solid #aaa;
}
.box {
    background-color: lightgreen;
    border: 1px solid gray;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2em;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="box"><span>1</span></div>
    <div class="box"><span>2</span></div>
    <div class="box"><span>3</span></div>
    <div class="box"><span>4</span></div>
    <div class="box"><span>5</span></div>
    <div class="box"><span>6</span></div>    
</div>
Run Code Online (Sandbox Code Playgroud)


align-content,align-itemsalign-self

这些属性与justify-*对应属性相同,但在垂直方向上.

更多信息:网格布局中的align-items与align-content有什么区别?


规格参考

10.3.行轴对齐:justify-selfjustify-items 属性

电网项目可以在维联,通过使用对齐 justify-self网格上的物品或财产justify-items网格容器上属性.

10.4.列轴对齐:align-selfalign-items 属性

通过使用网格容器align-self上的网格项或align-items属性上的属性,网格项也可以在块尺寸(垂直于内联尺寸)中对齐.

10.5.对齐网格:justify-contentalign-content 属性

如果网格的外边缘与网格容器的内容边缘不对应(例如,如果没有列是弹性大小的),则网格轨道将根据网格容器上的justify-contentalign-content属性在内容框中对齐 .

(重点补充)


CSS Box对齐模块

你写了:

justify-items在Flex的框属性相同justify-items的网格属性?

尽管Flex和Grid规范为关键字对齐属性提供了自己的定义,例如justify-itemsalign-content,W3C正在逐步淘汰各个盒子模型的对齐属性并实现他们的新Box对齐模块,该模块试图定义一组对齐在所有箱型号中使用的属性.

来自flexbox规范:

1.2.模块交互

CSS盒对准模块延伸,并且取代了取向性的(定义justify-content,align-items, align-self,align-content这里引入).

Grid规范中有类似的引用.

  • 这应该被接受的答案。如果你对所有的例子都做中心,而不是 1 中心,1 左,那就更好了 (2认同)
  • 我第一次真正看到这个概念得到很好的解释。非常感谢您的精彩解释! (2认同)