æ ¼ç·ä½å±çåºæ¬æ¦å¿µ
CSS æ ¼ç·ä½å±ä»ç´¹äºäºç¶ç CSS æ ¼ç·ç³»çµ±ãæ ¼ç·å¯ä»¥ç¨ä¾ä½ç½®é é¢ç主è¦ååãæå°åç¨æ¶ä»é¢ãæ¬æä»ç´¹ CSS æ ¼ç· Level 1 è¦ç¯çå ¶ä¸ä¸é¨ä»½ãé份æ¦è§é¡¯ç¤ºçæäºåè½ï¼å°å¨æå¸çå ¶ä»é¨ä»½è©³ç´°è§£éã
ä»éº½æ¯æ ¼ç·ï¼
æ ¼ç·æ¯ä¸çµæ°´å¹³ç·ååç´ç·ç交åéåï¼intersecting setï¼ï¼ä¸åå®ç¾©çºè¡ï¼rowï¼ï¼å¦ä¸åå®ç¾©çºåï¼columnï¼ãä½ å¯ä»¥è®åå ç´ ä¾ç §è¡åçè¦åæ¾å°åæ ¼ç·ä¸ãCSS æ ¼ç·ä½å±å ·æä»¥ä¸ç¹é»ï¼
åºå®åéæ´»çè»é尺寸
ä½ å¯ä»¥çµ¦åæ ¼ç·æå®ä¸ååºå®çè»é大å°ï¼ä¾å¦åç´ ï¼pixelï¼ã鿍£å°±è½ææ ¼ç·è¨çºæå®çåç´ ï¼ä»¥è²¼è¿ä½ ææçæçãä¹å¯ä»¥åµå»ºä¸å使ç¨ç¾åæ¯ãææ¯æ°ç fr å®ä½ä¹æ ¼ç·ãfr å®ä½å°±æ¯çºäºæ ¼ç·å¸å±èçã
å®å ä½ç½®
ä½ å¯ä»¥å¨æ ¼ç·ä½¿ç¨è¡èãååãç®æ¨ååï¼è®åå®å æ¾å°ç²¾ç¢ºçä½ç½®ãæ ¼ç·ä¹ææ§å¶éæå¼ï¼explicitï¼å®å çå¸å±æ¼ç®æ³ã
Creation of additional tracks to hold content
ä½ å¯ä»¥æç §éæ±ï¼å®ç¾©æå¼æ ¼ç·ãä¹å¯ä»¥èçæ²ææå®çæ ¼ç·ãéå¯ä»¥å¢å é¡å¤çè¡ï¼rowï¼èåï¼columnï¼ã妿éè¦ãç¡å¯è½å°æ¾é²å®¹å¨å®¹å¾äºçåãä¹é¡çä¹è¾¦å¾å°ã
æ§å¶å°é½
æ ¼ç·ä¹å å«äºä¾åºå°é½çåè½ï¼ä»¥ä¾¿æ§å¶åæ ¼ç·å §çåå®å ãéææ´åæ ¼ç·è¦å¦ä½å°é½ã
Control of overlapping content
æ¸åå®ä½ä¹è½è¢«æ¾é² grid cellãææ¯ååçä¸é¨åç¸äºéçãæåå¯ä»¥éé z-index æ§å¶è©²å層ã
æ ¼ç·æ¯å強大çè¦ç¯ãå®å¨è諸å¦å½æ§çåä¹é¡ç CSS çµåæï¼ä¹æå©æ¼ç¨ CSS 建ç«åæå°æªæçæçãä¸åé½å»ºç«è¦å¾å»ºç«æ ¼ç·å®¹å¨ï¼grid containerï¼éå§ã
æ ¼ç·å®¹å¨
æåæå®£å display: grid æ display: inline-grid ä¾çµ¦ä¸åå
ç´ å»ºç«æ ¼ç·å®¹å¨ï¼grid containerï¼ã宣å以å¾ï¼è©²å
ç´ çææç´æ¥åå
ç´ æè®ææ ¼ç·å®ä½ï¼grid itemï¼
æ¬ä¾ä¸ï¼ææåç¨±ä½ wrapper class ç divï¼è£¡é¢æäºåå ç´ ã
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
æ¥èæè® .wrapper è®ææ ¼ç·å®¹å¨ï¼grid containerï¼ã
.wrapper {
display: grid;
}
å¨å®ä¸é¢çç´æ¥åå
ç´ ï¼ç¾å¨é½æ¯æ ¼ç·å®å
äºãå¾ç¶²è·¯ç覽å¨ä¾çï¼åå®å
è®ææ ¼ç·çåå¾ï¼ä¼¼ä¹æ²ä»éº¼ä¸åï¼å çºç®åæ ¼ç·åªæå»ºç«ä¸åæ ¼ç·åï¼ä¾æ¾ææçæ ¼ç·å®å
ãéæåï¼ä½ æç¼ç¾æ ¼ç·æª¢æ¸¬å¨ç¸ç¶å¥½ç¨ãå¦æå¨ Firefox 檢æ¥éå示ä¾çæ ¼ç·ï¼ä½ æç¼ç¾å¨ grid 弿éï¼æä¸åå°åæ¨ãé»é¸éåå°åæ¨ï¼ç覽å¨è¦çªçå
ç´ ï¼å°±æè¢«ä¸åå層è¦èã

å¨ç解並è CSS æ ¼ç·å ±äºæï¼éåå·¥å ·è½å¹«ä½ è¦è¦ºåçè§£ï¼æ ¼ç·å°åºæéº¼ååçã
妿è¦éå§æå徿´åæ ¼ç·ï¼æåééè¦å¤å¯«å column trackã
æ ¼ç·è»éï¼Grid Trackï¼
æå卿 ¼ç·è£¡éé grid-template-columns è grid-template-rows 屬æ§å®ç¾©äºè¡èåãå®åä¹å®ç¾©äºæ ¼ç·è»éãæ ¼ç·è»éæ¯å¨æ ¼ç·çå
©åæ¬ä½ä¹éç空éãä¸åå°±æçå°è»éçé«äº®ï¼å°±å¨æ ¼ç·è£¡é¢ç第ä¸è¡ã

I can add to our earlier example by adding the grid-template-columns property, then defining the size of the column tracks.
I have now created a grid with three 200-pixel-wide column tracks. The child items will be laid out on this grid one in each grid cell.
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
.wrapper {
display: grid;
grid-template-columns: 200px 200px 200px;
}
fr å®ä½
æ ¼ç·è»éå¯ä»¥ä½¿ç¨ä»»ä½å®ä½å®ç¾©ï¼ä¸éæ ¼ç·å¼å
¥äºé¡å¤çå®ä½ï¼ä»¥å©æ¼å»ºç«æå½æ§çæ ¼ç·è»éãæ°çå®ä½ fr ä»£è¡¨æ ¼ç·å®¹å¨å
§ï¼å¯ç¨ç©ºéçåå¡ï¼fractionï¼ãæ¥ä¸ä¾çæ ¼ç·å®ç¾©ï¼æå»ºç«ä¸ååçãä¸è½ä¾ç
§å¯ç¨ç©ºé縮æ¾çé·åº¦è»éã
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
æ¥èä¸ä¾å°åµå»ºæä¸å 2fr çè»éï¼æ¥èéæå
©å 1fr çè»éãå¯ç¨ç©ºéæå æ¤è¢«åçºåå¡ï¼å
¶ä¸å
©å¡çµ¦ç¬¬ä¸åè»éãå©ä¸å
©å¡çµ¦å
©åè»éåä¸åã
.wrapper {
display: grid;
grid-template-columns: 2fr 1fr 1fr;
}
æå¾ï¼æåææåå¡èçµå°å¤§å°åçµåã第ä¸åè»éæ 500 åç´ ï¼éååºå®çå¯¬åº¦ï¼æå æ¤å¾å¯ç¨ç©ºéå å²ä¸å¡åºå»ãæ¥ä¸ä¾ç空éæè¢«ååçºä¸å¡ï¼ä¸¦ææ¯ä¾ææ´¾çµ¦å©ä¸ç彿§è»éã
.wrapper {
display: grid;
grid-template-columns: 500px 1fr 2fr;
}
Track listings with repeat() notation
å«æè¨±å¤è»éçæ ¼ç·è½ç¨ repeat() æ¨è¨ï¼ä»¥ä½¿è»éééè¿´åè¡¨åæ¸æ¬¡ã以ä¸é¢çºä¾ï¼
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
éå¯ä»¥å¯«æï¼
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
éè¤æ¨è¨æ¯è»é表åä¸é¨åãä¸ä¾ä¸ï¼æå建ç«äºæ 20 åç´ çè»éï¼æ¥èéè¤ 1fr è»éå
æ¬¡ï¼æå¾ä»¥ 20 åç´ çè»éåçµã
.wrapper {
display: grid;
grid-template-columns: 20px repeat(6, 1fr) 20px;
}
éè¤æ¨è¨ä½¿ç¨è»é表åï¼å æ¤å¯ä»¥ç¨å®ä¾å»ºç«éè¤ç模å¼ãä¸åä¾åçæ ¼ç·ï¼æå
å«ååè»éï¼ä¹å°±æ¯ 1fr å¾é¢æ 2fr çè»éï¼ä¸¦éè¤äºæ¬¡ã
.wrapper {
display: grid;
grid-template-columns: repeat(5, 1fr 2fr);
}
æå¼èæå¼æ ¼ç·
å¨å»ºç«ä¸ä¾æ ¼ç·çæåï¼æåç¨ grid-template-columns å±¬æ§æå®äºåè»éï¼ä½æ ¼ç·èªå·±ä¹å»ºç«äºä¸è¡ãç¸è¼æ¼ä½¿ç¨ grid-template-columns æ grid-template-rows 屬æ§çæå¼æ ¼ç·ï¼explicit gridï¼ï¼éå¹¾è¡å°±å±¬æ¼æå¼æ ¼ç·ï¼implicit gridï¼ã
ä½ ä¹è½å¨æå¼æ ¼ç·å
§éé grid-auto-rows è grid-auto-columns 屬æ§ï¼çµ¦è»éå®ç¾©ä¸å¥å¤§å°ã
ä¸ä¾å°ä½¿ç¨ grid-auto-rows 以確ä¿ç±æå¼æ ¼ç·å»ºç«çè»éï¼é«åº¦é½ææ¯ 200 åç´ ã
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 200px;
}
è»é縮æ¾è minmax()
å¨è¨å®é¡¯å¼æ ¼ç·æå¸æèªå給è»éä¸åæå°å°ºå¯¸ï¼ä½ä¹è¦ç¢ºä¿å®åæ´å±ä»¥é©æä»»ä½æ·»å çå §å®¹ââ忝叿æè¡ä¸è½å°æ¼ 100 åç´ ä»¥é¿å è·çï¼ä½å¦æå §å®¹é«åº¦è¶ é 300 åç´ ï¼è©²è¡å°±è¦æå°é£éº¼é«ã
éå°éåæ
å¢ï¼æ ¼ç·æä¾äº minmax() å½å¼ãæ¬ä¾ä¸ï¼æéå° grid-auto-rows æå®äº minmax() çæ¸å¼ã宿æå®é«åº¦æå°è¦ 100 åç´ ï¼æå¤§åæ¯ autoãauto æå³èå¤§å°ææª¢æ¥å
§å®¹å¤§å°ï¼ä¸¦é©é
éä¸è¡ cell å
§æé«é
ç®çé«åº¦ã
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(100px, auto);
}
<div class="wrapper">
<div>One</div>
<div>
Two
<p>I have some more content in.</p>
<p>This makes me taller than 100 pixels.</p>
</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
Grid Lines
It should be noted that when we define a grid we define the grid tracks, not the lines. Grid then gives us numbered lines to use when positioning items. In our three column, two row grid we have four column lines.

Lines are numbered according to the writing mode of the document. In a left-to-right language, line 1 is on the left-hand side of the grid. In a right-to-left language, it is on the right-hand side of the grid. Lines can also be named, and we will look at how to do this in a later guide in this series.
Positioning items against lines
We will be exploring line based placement in full detail in a later article, the following example demonstrates doing this in a simple way. When placing an item we target the line â rather than the track.
In the following example I am placing the first two items on our three column track grid, using the grid-column-start, grid-column-end, grid-row-start and grid-row-end properties. Working from left to right, the first item is placed against column line 1, and spans to column line 4, which in our case is the far right line on the grid. It begins at row line 1 and ends at row line 3, therefore spanning two row tracks.
The second item starts on grid column line 1, and spans one track. This is the default so I do not need to specify the end line. It also spans two row tracks from row line 3 to row line 5. The other items will place themselves into empty spaces on the grid.
<div class="wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
<div class="box5">Five</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
}
.box1 {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
}
.box2 {
grid-column-start: 1;
grid-row-start: 3;
grid-row-end: 5;
}
Don't forget that you can use the Grid Inspector in Firefox Developer Tools to see how the items are positioned against the lines of the grid.
Grid Cells
A grid cell is the smallest unit on a grid, conceptually it is like a table cell. As we saw in our earlier examples, once a grid is defined on a parent the child items will lay themselves out once in each cell of the defined grid. In the below image I have highlighted the first cell of the grid.

Grid areas
Items can span one or more cells both by row or by column, and this creates a grid area. Grid areas have to be rectangular â it isn't possible to create an L-shaped area for example. The highlighted grid area spans two row and two column tracks.

Gutters
Gutters or alleys between grid cells can be created using the grid-column-gap and grid-row-gap properties, or the shorthand grid-gap. In the below example I am creating a 10-pixel gap between columns and a 1em gap between rows.
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-column-gap: 10px;
grid-row-gap: 1em;
}
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
Any space used by gaps will be accounted for before space is assigned to flexible length fr tracks, and gaps act for sizing purposes like a regular grid track, however you cannot place anything into a gap. In terms of line-based positioning, the gap acts like a fat line.
Nesting grids
A grid item can become a grid container. In the following example I have the three-column grid created earlier, with our two positioned items. In this case the first item has some sub-items. As these items are not direct children of the grid they do not participate in grid layout and so display in normal document flow.
<div class="wrapper">
<div class="box box1">
<div class="nested">a</div>
<div class="nested">b</div>
<div class="nested">c</div>
</div>
<div class="box box2">Two</div>
<div class="box box3">Three</div>
<div class="box box4">Four</div>
<div class="box box5">Five</div>
</div>

If I set box1 to display: grid I can give it a track definition and it too will become a grid, the items then lay out on this new grid.
.box1 {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
In this case the nested grid has no relationship to the parent. As you can see in the example it has not inherited the grid-gap of the parent and the lines in the nested grid do not align to the lines in the parent grid.
Subgrid
In the level 1 grid specification there is a feature called subgrid which would let us create nested grids that use the track definition of the parent grid.
å註ï¼Subgrids are not yet implemented in any browsers, and the specification is subject to change.
In the current specification, we would edit the above nested grid example to use display: subgrid rather than display: grid, then remove the track definition. The nested grid will use the parent grid tracks to lay out items.
It should be noted that the nested grid is in both dimensionsârows and columns. There is no concept of the implicit grid working with subgrids. This means you need to ensure that the parent grid has enough row and column tracks for all the subitems.
.box1 {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
display: subgrid;
}
Layering items with z-index
Grid items can occupy the same cell. If we return to our example with items positioned by line number, we can change this to make two items overlap.
<div class="wrapper">
<div class="box box1">One</div>
<div class="box box2">Two</div>
<div class="box box3">Three</div>
<div class="box box4">Four</div>
<div class="box box5">Five</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
}
.box1 {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
}
.box2 {
grid-column-start: 1;
grid-row-start: 2;
grid-row-end: 4;
}
The item box2 is now overlapping box1, it displays on top as it comes later in the source order.
Controlling the order
We can control the order in which items stack up by using the z-index property - just as with positioned items. If we give box2 a lower z-index than box1 it will display below box1 in the stack.
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
}
.box1 {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
z-index: 2;
}
.box2 {
grid-column-start: 1;
grid-row-start: 2;
grid-row-end: 4;
z-index: 1;
}
ä¸ä¸æ¥
In this article we have had a very quick look through the Grid Layout Specification. Have a play with the code examples, and then move onto the next part of this guide where we will really start to dig into the detail of CSS Grid Layout.