问候!我向您介绍Rachel Andrew 撰写的文章“理解CSS网格:创建网格容器”的翻译

尽管CSS Grid技术早在2017年就已受到某些浏览器的支持,但许多开发人员尚未有机会在其项目中使用它。CSS Grid技术带来了许多新功能和新价值。因此,它似乎很复杂。但是,其中使用的许多工具都是可互换的,这意味着您无需研究整个规范即可上手。理解CSS网格系列的目的是引导读者从初学者到专家。
这篇初始文章将讨论创建网格容器时会发生什么,以及可应用于父级以控制网格的各种属性。您将了解到,在某些情况下,仅应用于网格容器的属性就足够了。
在本文中,我们将考虑:
- grid-
display: grid
display: inline-grid
grid-template-columns
grid-template-rows
- ( / )
grid-auto-columns
grid-auto-rows
:
, CSS Grid. Rachel Andrew , .
,
Grid-
Grid, Flexbox, display
. , , grid-, display: grid
. , , , Grid. , grid-, .
, . - , . , , . .
, - , grid-, grid-. , , <span>
, , grid-.
<span>
. grid-:
Firefox Grid Inspector, , .

, display: inline-grid
; grid- . , , grid-. display
. grid- , , .
grid, , , . , .
: , display: block grid
, display: inline grid
. display "Digging Into The DIsplay Property: The Two Values Of Display".
- , . grid-template-columns
grid-template-rows
. , "track-list" ( ).
, . grid-template-columns
, grid-template-rows
– .
:
grid-template-columns: 100px 100px 200px
– . 100px, – 100px, – 200pxgrid-template-columns: min-content max-content fit-content(10em)
– . min-content
, – max-content
. max-content
, 10em, – 10em.grid-template-columns: 1fr 1fr 1fr
– , fr
. grid- .grid-template-columns: repeat(2, 10em 1fr)
– 10em 1fr 10em 1fr, repeat() .grid-template-columns: repeat(auto-fill, 200px)
– 200px, , , .grid-template-columns: repeat(auto-fill, minmax(200px, 1fr))
– 200px, , .grid-template-columns: [full-start] 1fr [content-start] 3fr [content-end] 1fr [full-end]
– . , 1 , – 3 . .
, . , , , .
. grid-, , . - , align-content
justify-content
start
. , , How To Align Things In CSS.
min-content
, max-content
fit-content()
.
min-content
, , , . , , , . , .
max-content
, , .
fit-content
. , . , max-content
, , , . , . , , .
CSS Grid How Big Is That Box? Understanding Sizing In CSS Layout.
, , , . , float flex , , 100, .
«fr»
CSS Grid , , fr
. cacl()
; , grid-.
, 1fr 1fr 1fr
, 3 . 2fr 1fr 1fr
, 4, 2 , .
, " " – . - , , , .
ItemThree
. - , .
fr
. , .
– , fit-content(300px)
, 1fr
. , - 300px, , , , fr
. - (, max-width: 100%
), 300px, . fr
fit-content
– .
repeat()
repeat()
.
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-columns: repeat(12, 1fr);
repeat()
, , . . , .
repeat()
. , 1fr, 200px 1fr.
grid-template-columns: 1fr repeat(3,200px) 1fr
, , auto-fill
auto-fit
. , , grid- , .
, , . 500px, 200px .
grid-, , . minmax()
. = 200px, = 1fr, 200px, , = 1fr, , .
: auto-fill
auto-fit
. , , . – .
auto-fill
, .
auto-fit
, .
Firefox Grid Inspector , , , 0px. – 3, .
. CSS Grid, . , . . , . , .
grid-template-columns: [main-start sidebar-start] 1fr [sidebar-end content-start] 4fr [content-end main-end]
, span
, , .
: , . "Naming Things in CSS Grid Layout, .
grid-template-columns
grid-template-rows
. , .
grid- , , , . . , display: grid
grid-. , , , .
grid-auto-rows
grid-auto-columns
. , , 200px , , :
grid-auto-rows: minmax(200px, auto)
, , 100px ( , ), .
grid-auto-rows: auto 100px
创建网格并允许浏览器自动将元素放置在网格上,就可以实现的结果而言,这提供了巨大的机会。到目前为止,我们还没有考虑元素在网格上的位置,但是在使用CSS Grid开发时,他们通常不使用它。取而代之的是,它们仅按通常的源顺序依赖布局:每个单元中一个元素。
如果您不熟悉CSS Grid,那么开始使用CSS Grid的一个好方法就是试用轨道的大小,并查看元素如何适合您创建的单元格。