-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathOverview.bs
3903 lines (3250 loc) · 168 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<h1>CSS Grid Layout Module Level 1</h1>
<pre class='metadata'>
Status: WD
Date: 2015-09-17
Work Status: Refining
Shortname: css-grid
Level: 1
Group: csswg
ED: http://dev.w3.org/csswg/css-grid/
TR: http://www.w3.org/TR/css-grid-1/
Previous version: http://www.w3.org/TR/2015/WD-css-grid-1-20150806/
Previous version: http://www.w3.org/TR/2015/WD-css-grid-1-20150317/
Previous version: http://www.w3.org/TR/2014/WD-css-grid-1-20140513/
Previous version: http://www.w3.org/TR/2014/WD-css-grid-1-20140123/
Previous version: http://www.w3.org/TR/2013/WD-css3-grid-layout-20130402/
Previous version: http://www.w3.org/TR/2012/WD-css3-grid-layout-20121106/
Editor: Tab Atkins Jr., Google, http://www.xanthir.com/contact/
Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact
Editor: Rossen Atanassov, Microsoft, ratan@microsoft.com
Former editor: Alex Mogilevsky, Microsoft Corporation, alexmog@microsoft.com
Former editor: Phil Cupp, Microsoft Corporation, pcupp@microsoft.com
Issue Tracking: Disposition of Comments http://dev.w3.org/csswg/css-grid-1/issues
Abstract: This CSS module defines a two-dimensional grid-based layout system, optimized for user interface design. In the grid layout model, the children of a grid container can be positioned into arbitrary slots in a flexible or fixed predefined layout grid.
Ignored Terms: containing block, <positive-integer>, <ident>, auto, grid-*-start, grid-*-end, flex factor, flex factors, block formatting context, grid-auto-position, min size property, max size property
Link Defaults: css21 (property) margin/min-height/max-height/min-width/max-width, css-align-3 (dfn) alignment container/alignment subject/box alignment properties, css-align-3 (value) stretch/baseline, css-position-3 (property) left, css-position-3 (property) position, css-writing-modes-3 (dfn) start/end
At Risk: the ''grid-template-rows/subgrid'' value of 'grid-template-columns' and 'grid-template-rows', and its component parts individually
</pre>
<style type="text/css">
.example {
clear:both
}
.pseudo-code {
font-family:monospace
}
.pseudo-code > ol {
list-style-type:decimal
}
.pseudo-code > ol > li > ol {
list-style-type:lower-latin
}
.pseudo-code > ol > li > ol > li > ol {
list-style-type:lower-roman
}
.pseudo-code ul {
list-style-type:disc
}
dd > p:nth-child(1) {
margin-top:0
}
dd:last-child {
margin-bottom: 0;
}
dl.phase {
padding-left: .5em;
border-left: .5em #e0e0e0 solid;
}
#grid-property-breakdown tr:nth-child(3) td {
width: 25%;
border-style: solid;
}
</style>
<h2 id='intro'>
Introduction and Overview</h2>
Grid layout contains features targeted at web application authors.
The grid can be used to achieve many different layouts.
It excels at dividing up space for major regions of an application,
or defining the relationship in terms of size, position, and layer
between parts of a control built from HTML primitives.
Like tables,
grid layout enables an author to align elements into columns and rows,
but unlike tables,
grid layout doesn't have content structure,
and thus enables a wide variety of layouts not possible with tables.
For example, the children of a grid container can position themselves
such that they overlap and layer similar to positioned elements.
In addition, the absence of content structure in grid layout helps to manage changes to layout
by using fluid and source order independent layout techniques.
By combining media queries with the CSS properties that control layout of the grid container and its children,
authors can adapt their layout to changes in device form factors, orientation, and available space,
without needing to alter the semantic nature of their content.
<h3 id='background'>
Background and Motivation</h3>
<figure class="sidefigure">
<img class="figure" alt="Image: Application layout example requiring horizontal and vertical alignment." src="images/basic-form.png" />
<figcaption>Application layout example requiring horizontal and vertical alignment.</figcaption>
</figure>
As websites evolved from simple documents into complex, interactive applications,
tools for document layout, e.g. floats,
were not necessarily well suited for application layout.
By using a combination of tables, JavaScript, or careful measurements on floated elements,
authors discovered workarounds to achieve desired layouts.
Layouts that adapted to the available space were often brittle
and resulted in counter-intuitive behavior as space became constrained.
As an alternative, authors of many web applications opted for a fixed layout
that cannot take advantage of changes in the available rendering space on a screen.
The capabilities of grid layout address these problems.
It provides a mechanism for authors to divide available space for layout into columns and rows
using a set of predictable sizing behaviors.
Authors can then precisely position and size the building block elements of their application
by into <a>grid areas</a> defined by these columns and rows.
Figure 1 illustrates a basic layout which can be achieved with grid layout.
<h3 id='adapting-to-available-space'>
Adapting Layouts to Available Space</h3>
<figure class="sidefigure">
<img alt="Image: Five grid items arranged according to content size and available space." src="images/game-smaller.png" />
<figcaption>Five grid items arranged according to content size and available space.</figcaption>
</figure>
<figure class="sidefigure">
<img alt="Image: Growth in the grid due to an increase in available space." src="images/game-larger.png" />
<figcaption>Growth in the grid due to an increase in available space.</figcaption>
</figure>
Grid layout can be used to intelligently reflow elements within a webpage.
Figure 2 represents a game with five major areas in the layout:
the game title, stats area, game board, score area, and control area.
The author's intent is to divide the space for the game such that:
<ul>
<li>
The stats area always appears immediately under the game title.
<li>
The game board appears to the right of the stats and title.
<li>
The top of the game title and the game board should always align.
<li>
The bottom of the game board and the stats area align when the game has reached its minimum height,
but otherwise the game board will stretch to take advantage of all the screen real-estate available to it.
<li>
The score area should align into the column created by the game and stats area,
while the controls are centered under the board.
</ul>
As an alternative to using script to control the absolute position, width, and height of all elements,
the author can use grid layout,
as shown in Figure 3.
The following example shows how an author might achieve all the sizing, placement, and alignment rules declaratively.
Note that there are multiple ways to specify the structure of the grid
and to position and size <a>grid items</a>,
each optimized for different scenarios.
This example illustrates one that an author may use to define the position and space for each <a>grid item</a>
using the 'grid-template-rows' and 'grid-template-columns' properties on the <a>grid container</a>,
and the 'grid-row' and 'grid-column' properties on each <a>grid item</a>.
<div class="example">
<pre class="lang-css">
#grid {
display: grid;
/* Two columns: the first sized to content, the second receives
* the remaining space, but is never smaller than the minimum
* size of the board or the game controls, which occupy this
* column. */
grid-template-columns: auto minmax(min-content, 1fr);
/* Three rows: the first and last sized to content, the middle
* row receives the remaining space, but is never smaller than
* the minimum height of the board or stats areas. */
grid-template-rows: auto minmax(min-content, 1fr) auto
}
/* Each part of the game is positioned between grid lines by
* referencing the starting grid line and then specifying, if more
* than one, the number of rows or columns spanned to determine
* the ending grid line, which establishes bounds for the part. */
#title { grid-column: 1; grid-row: 1 }
#score { grid-column: 1; grid-row: 3 }
#stats { grid-column: 1; grid-row: 2; align-self: start }
#board { grid-column: 2; grid-row: 1 / span 2; }
#controls { grid-column: 2; grid-row: 3; justify-self: center }
</pre>
<pre class="lang-markup">
<div id="grid">
<div id="title">Game Title</div>
<div id="score">Score</div>
<div id="stats">Stats</div>
<div id="board">Board</div>
<div id="controls">Controls</div>
</div>
</pre>
</div>
<h3 id='source-independence'>
Source-Order Independence</h3>
<figure class="sidefigure">
<img alt="Image: An arrangement suitable for portrait orientation." src="images/game-portrait.png" />
<figcaption>An arrangement suitable for “portrait” orientation.</figcaption>
</figure>
<figure class="sidefigure">
<img alt="Image: An arrangement suitable for landscape orientation." src="images/game-landscape.png" />
<figcaption>An arrangement suitable for “landscape“ orientation.</figcaption>
</figure>
Continuing the prior example,
the author also wants the game to adapt to the space available on traditional computer monitors, handheld devices, or tablet computers.
Also, the game should optimize the placement of the components when viewed either in portrait or landscape orientation (Figures 4 and 5).
By combining grid layout with media queries,
the author is able to use the same semantic markup,
but rearrange the layout of elements independent of their source order,
to achieve the desired layout in both orientations.
The following example leverages grid layout’s ability to name the space which will be occupied by a <a>grid item</a>.
This allows the author to avoid rewriting rules for <a>grid items</a>
as the grid’s definition changes.
<div class="example">
<pre class="lang-css">
@media (orientation: portrait) {
#grid {
display: grid;
/* The rows, columns and areas of the grid are defined visually
* using the grid-template-areas property. Each string is a row,
* and each word an area. The number of words in a string
* determines the number of columns. Note the number of words
* in each string must be identical. */
grid-template-areas: "title stats"
"score stats"
"board board"
"ctrls ctrls";
/* Columns and rows created with the template property can be
* assigned a sizing function with the grid-template-columns
* and grid-template-rows properties. */
grid-template-columns: auto minmax(min-content, 1fr);
grid-template-rows: auto auto minmax(min-content, 1fr) auto
}
}
@media (orientation: landscape) {
#grid {
display: grid;
/* Again the template property defines areas of the same name,
* but this time positioned differently to better suit a
* landscape orientation. */
grid-template-areas: "title board"
"stats board"
"score ctrls";
grid-template-columns: auto minmax(min-content, 1fr);
grid-template-rows: auto minmax(min-content, 1fr) auto
}
}
/* The grid-area property places a grid item into a named
* region (area) of the grid. */
#title { grid-area: title }
#score { grid-area: score }
#stats { grid-area: stats }
#board { grid-area: board }
#controls { grid-area: ctrls }
</pre>
<pre class="lang-markup">
<div id="grid">
<div id="title">Game Title</div>
<div id="score">Score</div>
<div id="stats">Stats</div>
<div id="board">Board</div>
<div id="controls">Controls</div>
</div>
</pre>
</div>
Note: The reordering capabilities of grid layout intentionally affect
<em>only the visual rendering</em>,
leaving speech order and navigation based on the source order.
This allows authors to manipulate the visual presentation
while leaving the source order intact and optimized for non-CSS UAs
and for linear models such as speech and sequential navigation.
Advisement: Grid item placement and reordering must not be used
as a substitute for correct source ordering,
as that can ruin the accessibility of the document.
<h3 id='grid-layering'>
Grid Layering of Elements</h3>
<figure class="sidefigure">
<img alt="Image: A control composed of layered HTML elements." src="images/control-layering-and-alignment.png" />
<figcaption>A control composed of layered HTML elements.</figcaption>
</figure>
In the example shown in Figure 6,
the author is creating a custom slider control.
The control has six parts.
The lower and upper labels align to the left and right edges of the control.
The track of the slider spans the area between the labels.
The lower and upper fill parts touch beneath the thumb,
and the thumb is a fixed width and height that can be moved along the track
by updating the two flex-sized columns.
Prior to the introduction of grid layout,
the author would have likely used absolute positioning to control the top and left coordinates,
along with the width and height of each HTML element that comprises the control.
By leveraging grid layout,
the author can instead limit script usage to handling mouse events on the thumb,
which snaps to various positions along the track
as the 'grid-template-columns' property of the <a>grid container</a> is updated.
<div class="example">
<pre class="lang-css">
#grid {
display: grid;
/* The grid-template-columns and rows properties also support
* naming grid lines which can then be used to position grid
* items. The line names are assigned on either side of a column
* or row sizing function where the line would logically exist. */
grid-template-columns:
[start] auto
[track-start] 0.5fr
[thumb-start] auto
[fill-split] auto
[thumb-end] 0.5fr
[track-end] auto
[end];
}
/* The grid-placement properties accept named lines. Below the
* lines are referred to by name. Beyond any
* semantic advantage, the names also allow the author to avoid
* renumbering the grid-column-start and grid-row-start properties of the
* grid items. This is similar to the concept demonstrated in the
* prior example with the grid-template-areas property during orientation
* changes, but grid lines can also work with layered grid items
* that have overlapping areas of different shapes like the thumb
* and track parts in this example. */
#lower-label { grid-column-start: start }
#track { grid-column: track-start / track-end; justify-self: center }
#upper-label { grid-column-end: end; }
/* Fill parts are drawn above the track so set z-index to 5. */
#lower-fill { grid-column: track-start / fill-split;
justify-self: end;
z-index: 5 }
#upper-fill { grid-column: fill-split / track-end;
justify-self: start;
z-index: 5 }
/* Thumb is the topmost part; assign it the highest z-index value. */
#thumb { grid-column: thumb-start / thumb-end; z-index: 10 }
</pre>
<pre class="lang-markup">
<div id="grid">
<div id="lower-label">Lower Label</div>
<div id="upper-label">Upper Label</div>
<div id="track">Track</div>
<div id="lower-fill">Lower Fill</div>
<div id="upper-fill">Upper Fill</div>
<div id="thumb">Thumb</div>
</div>
</pre>
</div>
<!--
██████ ███████ ██ ██ ██████ ████████ ████████ ████████ ██████
██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██████ ████████ ██ ██████
██ ██ ██ ██ ████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██
██████ ███████ ██ ██ ██████ ████████ ██ ██ ██████
-->
<h2 id='grid-concepts'>
Grid Layout Concepts and Terminology</h2>
In <dfn export>grid layout</dfn>,
the content of a <a>grid container</a> is laid out
by positioning and aligning it into a <a>grid</a>.
The <dfn export>grid</dfn> is an intersecting set of horizontal and vertical <a>grid lines</a>
that divides the <a>grid container</a>’s space into <a>grid areas</a>,
into which <a>grid items</a> (representing the <a>grid container</a>’s content) can be placed.
There are two sets of <a>grid lines</a>:
one set defining <dfn export lt="grid column | column">columns</dfn>
that run along the <a href="http://www.w3.org/TR/css3-writing-modes/#block-axis-">block axis</a> (the <dfn export>column axis</dfn>),
and an orthogonal set defining <dfn export lt="grid row | row">rows</dfn>
along the <a href="http://www.w3.org/TR/css3-writing-modes/#inline-axis-">inline axis</a> (the <dfn export>row axis</dfn>).
[[!CSS3-WRITING-MODES]]
<!--
<div class="figure">
<img class="figure" alt="Image: A diagram illustrating the relationship between the Grid Element and its Tracks, Lines, Areas and Items." src="images/grid-concepts.png" />
<p class="caption">A diagram illustrating the relationship between the <a>grid container</a> and its tracks, lines, areas and items.
</div>
-->
<figure>
<img class="figure" alt="Image: Grid Lines." src="images/grid-lines.png" />
<figcaption>Grid lines: Three in the block axis and four in the inline axis.</figcaption>
</figure>
<h3 id="grid-track-concept">
Grid Tracks and Cells</h3>
<dfn export>Grid track</dfn> is a generic term for a <a>grid column</a> or <a>grid row</a>—in
other words, it is the space between two adjacent <a>grid lines</a>.
Each <a>grid track</a> is assigned a sizing function,
which controls how wide or tall the column or row may grow,
and thus how far apart its bounding <a>grid lines</a> are.
A <dfn export>grid cell</dfn> is the similar term for the full grid—it
is the space between two adjacent row and two adjacent column <a>grid lines</a>.
It is the smallest unit of the grid that can be referenced when positioning <a>grid items</a>.
<div class="example">
In the following example there are two columns and three rows.
The first column is fixed at 150px.
The second column uses flexible sizing, which is a function of the unassigned space in the Grid,
and thus will vary as the width of the <a>grid container</a> changes.
If the used width of the <a>grid container</a> is 200px, then the second column 50px wide.
If the used width of the <a>grid container</a> is 100px, then the second column is 0px
and any content positioned in the column will overflow the <a>grid container</a>.
<pre>
<style type="text/css">
#grid {
display: grid;
grid-template-columns: 150px 1fr; /* two columns */
grid-template-rows: 50px 1fr 50px /* three rows */
}
</style>
</pre>
</div>
<h3 id="grid-line-concept">
Grid Lines</h3>
<dfn id='grid-line' export lt='grid line|grid row line|grid column line'>Grid lines</dfn> are the horizontal and vertical dividing lines of the <a>grid</a>.
A <a>grid line</a> exists on either side of a column or row.
They can be referred to by numerical index,
or by an author-specified name.
A <a>grid item</a> references the <a>grid lines</a> to determine its position within the <a>grid</a>
using the <a href="#placement">grid-placement properties</a>.
<div class="example">
The following two examples create three column <a>grid lines</a> and four row <a>grid lines</a>.
The first example demonstrates how an author would position a <a>grid item</a> using <a>grid line</a> numbers.
The second example uses explicitly named <a>grid lines</a>.
<pre>
<style type="text/css">
#grid {
display: grid;
grid-template-columns: 150px 1fr;
grid-template-rows: 50px 1fr 50px
}
#item1 { grid-column: 2;
grid-row-start: 1; grid-row-end: 4; }
</style>
</pre>
<pre>
<style type="text/css">
/* equivalent layout to the prior example, but using named lines */
#grid {
display: grid;
grid-template-columns: 150px [item1-start] 1fr [item1-end];
grid-template-rows: [item1-start] 50px 1fr 50px [item1-end];
}
#item1 {
grid-column: item1-start / item1-end;
grid-row: item1-start / item1-end
}
</style>
</pre>
</div>
<h3 id="grid-area-concept">
Grid Areas</h3>
A <dfn export>grid area</dfn> is the logical space used to lay out one or more <a>grid items</a>.
It is bound by four <a>grid lines</a>, one on each side of the <a>grid area</a>,
and participates in the sizing of the <a>grid tracks</a> it intersects.
A <a>grid area</a> can be named explicitly using the 'grid-template-areas' property of the <a>grid container</a>,
or referenced implicitly by its bounding <a>grid lines</a>.
A <a>grid item</a> is assigned to a <a>grid area</a>
using the <a href="#placement">grid-placement properties</a>.
<div class="example">
<pre>
<style type="text/css">
/* using the template syntax */
#grid {
display: grid;
grid-template-areas: ". a"
"b a"
". a";
grid-template-columns: 150px 1fr;
grid-template-rows: 50px 1fr 50px
}
#item1 { grid-area: a }
#item2 { grid-area: b }
#item3 { grid-area: b }
/* Align items 2 and 3 at different points in the Grid Area "b". */
/* By default, Grid Items are stretched to fit their Grid Area */
/* and these items would layer one over the other. */
#item2 { align-self: start }
#item3 { justify-self: end; align-self: end }
</style>
</pre>
</div>
A <a>grid item</a>’s <a>grid area</a> forms the containing block into which it is laid out.
Percentage lengths specified on a <a>grid item</a> resolve against this containing block.
Percentages specified for 'margin-top', 'padding-top', 'margin-bottom', and 'padding-bottom' on a <a>grid item</a>
resolve against the height of its containing block,
rather than the width (as for blocks).
Issue: This margin/padding behavior is disputed.
<a href="https://lists.w3.org/Archives/Public/www-style/2015Mar/0273.html">(see discussion)</a>
<a>Grid items</a> placed into the same <a>grid area</a> do not directly affect each other's layout.
Indirectly, a <a>grid item</a> can affect the position of a <a>grid line</a> in a column or row that uses a content-based relative size,
which in turn can affect the position or size of another <a>grid item</a>.
<!--
████████ ████ ██████ ████████ ██ ███ ██ ██ ██ ██████ ████████ ████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██████ ████████ ██ ██ ██ ██ ██ ████ ████████ ██ ██ ██
██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
████████ ████ ██████ ██ ████████ ██ ██ ██ ██ ██████ ██ ██ ████ ████████
-->
<h2 id="grid-model">
Grid Containers</h2>
<h3 id='grid-containers'>
Establishing Grid Containers: the ''display/grid'' and ''inline-grid'' 'display' values</h3>
<pre class="propdef">
Name: display
New values: grid | inline-grid
</pre>
<dl dfn-for="display" dfn-type=value>
<dt><dfn>grid</dfn>
<dd>
This value causes an element to generate a block-level <a>grid container</a> box.
<dt><dfn>inline-grid</dfn>
<dd>
This value causes an element to generate an inline-level <a>grid container</a> box.
</dl>
A <dfn export>grid container</dfn> establishes a new <dfn export>grid formatting context</dfn> for its contents.
This is the same as establishing a block formatting context,
except that grid layout is used instead of block layout:
floats do not intrude into the grid container,
and the grid container's margins do not collapse with the margins of its contents.
The contents of a <a>grid container</a> are laid out into a <a>grid</a>,
with <a>grid lines</a> forming the boundaries of each <a>grid items</a>’ containing block.
The 'overflow' property applies to <a>grid containers</a>.
Grid containers are not block containers,
and so some properties that were designed with the assumption of block layout
don't apply in the context of grid layout.
In particular:
<ul>
<li>
the 'column-*' properties in the Multi-column Layout module [[!CSS3COL]] have no effect on a grid container.
<li>
'float' and 'clear' have no effect on a <a>grid item</a>.
(However, the 'float' property still affects the computed value of 'display' on children of a grid container,
as this occurs <em>before</em> <a>grid items</a> are determined.)
<li>
'vertical-align' has no effect on a grid item.
<li>
the ''::first-line'' and ''::first-letter'' pseudo-elements do not apply to <a>grid containers</a>,
and <a>grid containers</a> do not contribute a first formatted line or first letter to their ancestors.
</ul>
If an element's specified 'display' is ''inline-grid''
and the element is floated or absolutely positioned,
the computed value of 'display' is <a value>grid</a>.
The table in <a href="http://www.w3.org/TR/CSS2/visuren.html#dis-pos-flo">CSS 2.1 Chapter 9.7</a> is thus amended
to contain an additional row,
with ''inline-grid'' in the "Specified Value" column
and ''grid'' in the "Computed Value" column.
<h3 id='intrinsic-sizes'>
Sizing Grid Containers</h3>
A <a>grid container</a> is sized
using the rules of the formatting context in which it participates.
As a block-level box in a <a>block formatting context</a>,
it is sized like any other block-level box that establishes a formatting context,
with an ''auto'' inline size calculated as for in-flow block boxes.
As an inline-level box in an inline formatting context,
it is sized as an atomic inline-level box (such as an inline-block).
In both inline and block formatting contexts,
the <a>grid container</a>’s ''auto'' block size is its max-content size.
<span class="issue">The block layout spec should define this?</span>
The <a>max-content size</a> of a <a>grid container</a> is
the sum of the <a>grid container’s</a> track sizes in the appropriate axis,
when the grid is sized under a <a>max-content constraint</a>.
The <a>min-content size</a> of a <a>grid container</a> is
the sum of the <a>grid container’s</a> track sizes in the appropriate axis,
when the grid is sized under a <a>min-content constraint</a>.
See [[!CSS3-SIZING]] for a definition of the terms in this section.
<!--
██████ ████████ ████ ████████ ████ ████████ ████████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██
██ ████ ████████ ██ ██ ██ ██ ██ ██████ ██ ███ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ████ ████████ ████ ██ ████████ ██ ██ ██████
-->
<h2 id="grid-items">
Grid Items</h2>
The contents of a <a>grid container</a> consists of zero or more <dfn export id="grid-item" lt="grid item">grid items</dfn>:
each child of a <a>grid container</a>
becomes a <a>grid item</a>,
and each contiguous run of text that is directly contained inside a <a>grid container</a>
is wrapped in an anonymous <a>grid item</a>.
However, an anonymous grid item that contains only
<a href="http://www.w3.org/TR/css-text/#white-space-processing">white space</a>
is not rendered, as if it were ''display:none''.
<div class="example">
Examples of grid items:
<pre>
<div style="display:grid">
<!-- grid item: block child -->
<div id="item1">block</div>
<!-- grid item: floated element; floating is ignored -->
<div id="item2" style="float: left;">float</div>
<!-- grid item: anonymous block box around inline content -->
anonymous item 3
<!-- grid item: inline child -->
<span>
item 4
<!-- grid items do not split around blocks -->
<div id=not-an-item>item 4</div>
item 4
</span>
</div></pre>
</div>
The 'display' value of a <a>grid item</a> is <a>blockified</a>:
if the specified 'display' of an in-flow child of an element generating a <a>grid container</a>
is an inline-level value, it computes to its block-level equivalent.
(See <a href="http://www.w3.org/TR/CSS21/visuren.html#dis-pos-flo">CSS2.1§9.7</a> [[!CSS21]]
and <a href="http://www.w3.org/TR/css-display/#transformations">CSS Display</a> [[!CSS3-DISPLAY]]
for details on this type of 'display' value conversion.)
Some values of 'display' trigger the generation of anonymous boxes.
For example, a misparented ''display/table-cell'' child is fixed up
by <a href="http://www.w3.org/TR/CSS21/tables.html#anonymous-boxes">generating anonymous <css>table</css> and <css>table-row</css> elements</a> around it. [[!CSS21]]
This fixup must occur <em>before</em> a <a>grid container</a>’s children are promoted to <a>grid items</a>.
For example, given two contiguous child elements with ''display:table-cell'',
an anonymous table wrapper box around them becomes the <a>grid item</a>.
<p class='note'>
Future display types may generate anonymous containers (e.g. ruby) or otherwise mangle the box tree (e.g. run-ins).
It is intended that grid item determination run after these operations.
A <a>grid item</a> establishes a new formatting context for its contents.
The type of this formatting context is determined by its 'display' value, as usual.
The computed 'display' of a <a>grid item</a>
is determined by applying the table in
<a href="http://www.w3.org/TR/CSS2/visuren.html#dis-pos-flo">CSS 2.1 Chapter 9.7</a>.
However, grid items are <dfn>grid-level</dfn> boxes, not block-level boxes:
they participate in their container's <a>grid formatting context</a>,
not in a block formatting context.
A <a>grid item</a> is sized within the containing block defined by its <a>grid area</a>
similarly to an equivalent block-level box in an equivalently-sized containing block,
except that ''margin/auto'' margins and the <a>box alignment properties</a>
have special effects. (See [[#alignment]].)
The ''min-width/auto'' value of 'min-width' and 'min-height'
behaves on <a>grid items</a> in the relevant axis
analogously to its behavior on <a>flex items</a> in the <a>main axis</a>.
See [[#min-size-auto]].
ISSUE: Review implications of intrinsic ratio and Grid's 2D nature.
<!--
<h3 id="position-grid">
Non-children Grid Items</h3>
<p class="issue">
This is a proposal to create the ability to have descendants of a grid item participate in a grid layout,
similar to the behavior defined by the Template Layout module.
A descendant of the grid can be pulled out of flow and participate directly in the grid
by assigning it ''position: grid''.
An element with ''position: grid'' is pulled out of flow and participates as a grid item
belonging to the first ancestor with ''display: grid''.
If the element is positioned using named lines or slots,
it belongs to the first ancestor with ''display: grid'' that has all of the corresponding named lines/slots.
If no such ancestor exists, the item remains in flow.
<p class="issue">
Alternatively, the item can just go into the first grid,
and missing names are treated as <a value for="<grid-line>">auto</a>.
-->
<h3 id="visibility-collapse">
Collapsed Grid Items: the 'visibility' property</h3>
<p class='issue'>
We want the ability to collapse grid tracks
(similar to <a href="http://www.w3.org/TR/css3-flexbox/#visibility-collapse">collapsing flex items</a>
or <a href="http://www.w3.org/TR/CSS21/tables.html#dynamic-effects">table rows/columns</a>),
but we're not sure exactly how to do it.
Ideas welcome, please <a href="mailto:www-style@w3.org?Subject=%5Bcss-grid%5D%20Collapsing%20Grid%20Tracks">post them to www-style@w3.org</a>.
<h3 id='order-property'>
Reordered Grid Items: the 'order' property</h3>
The 'order' property also applies to <a>grid items</a>.
It affects their <a href="#grid-auto-flow-property">auto-placement</a> and <a href="#z-order">painting order</a>.
Advisement: As with reordering flex items,
the 'order' property must only be used
when the visual order needs to be <em>out-of-sync</em>
with the speech and navigation order;
otherwise the underlying document source should be reordered instead.
See <a href="https://drafts.csswg.org/css-flexbox-1/#order-accessibility">Reordering and Accessibility</a>
in [[CSS-FLEXBOX-1]].
<h3 id='z-order'>
Z-axis Ordering: the 'z-index' property</h3>
<a>Grid items</a> can overlap when they are positioned into intersecting <a>grid areas</a>,
or even when positioned in non-intersecting areas because of negative margins or positioning.
The painting order of <a>grid items</a> is exactly the same as inline blocks [[CSS21]],
except that <a>order-modified document order</a> is used in place of raw document order,
and 'z-index' values other than <a value for=z-index>auto</a> create a stacking context even if 'position' is ''static''.
Thus the 'z-index' property can easily be used to control the z-axis order of grid items.
<p class='note'>
Note: Descendants that are positioned outside a grid item still participate in any stacking context established by the grid item.
<div class="example">
The following diagram shows several overlapping grid items,
with a combination of implicit source order
and explicit 'z-index'
used to control their stacking order.
<figure>
<img src="images/drawing-order.png" />
<figcaption>Drawing order controlled by z-index and source order.</figcaption>
</figure>
<pre>
<style type="text/css">
#grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr
}
#A { grid-column: 1 / span 2; grid-row: 2; align-self: end; }
#B { grid-column: 1; grid-row: 1; z-index: 10; }
#C { grid-column: 2; grid-row: 1; align-self: start; margin-left: -20px; }
#D { grid-column: 2; grid-row: 2; justify-self: end; align-self: start; }
#E { grid-column: 1 / span 2; grid-row: 1 / span 2;
z-index: 5; justify-self: center; align-self: center; }
</style>
<div id="grid">
<div id="A">A</div>
<div id="B">B</div>
<div id="C">C</div>
<div id="D">D</div>
<div id="E">E</div>
</div>
</pre>
</div>
<!--
██ ██ ████ ██ ██
███ ███ ██ ███ ██ ██ ██
████ ████ ██ ████ ██ ██ ██
██ ███ ██ ██ ██ ██ ██ ███████ █████████
██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ███ ██ ██
██ ██ ████ ██ ██
-->
<h3 id="min-size-auto">
Implied Minimum Size of Grid Items</h3>
To provide a more reasonable default minimum size for <a>grid items</a>,
this specification defines the effects of the 'min-width'/'min-height' ''min-width/auto'' value
for <a>grid items</a>.
On a <a>grid item</a> whose 'overflow' is ''overflow/visible'',
when ''min-width/auto'' is specified on the <a>grid item</a>,
the following table gives the minimum size in that dimension:
<table class=data>
<colgroup span=2></colgroup>
<colgroup span=1></colgroup>
<thead>
<tr>
<th>Specified Size
<th>Transferred Size
<th>Minimum Size
<tbody>
<tr>
<td>
<td>
<td><var>content size</var>
<tr>
<td>✓
<td>
<td>min(<var>specified size</var>, <var>content size</var>)
<tr>
<td>
<td>✓
<td>min(<var>transferred size</var>, <var>content size</var>)
<tr>
<td>✓
<td>✓
<td>min(<var>specified size</var>, <var>content size</var>)
</table>
Where:
<dl>
<dt><var>specified size</var>
<dd>
If the item’s computed width/height is <a>definite</a>,
then the <var>specified size</var> is that size
(clamped by its <a>max size property</a> in that dimension if it's <a>definite</a>).
It is otherwise undefined.
<dt><var>transferred size</var>
<dd>
If the item has an intrinsic aspect ratio
and its computed height/width is <a>definite</a>,
then the <var>transferred size</var> is that size
(clamped by its <a lt="min size property">min and max size properties</a> in that dimension if they are <a>definite</a>),
converted through the aspect ratio.
It is otherwise undefined.
<dt><var>content size</var>
<dd>
The <var>content size</var> is the <a>min-content size</a> in that dimension,
clamped, if it has an aspect ratio, by any <a>definite</a> <a lt="min size property">min and max size properties</a>
in the perpendicular dimension
converted through the aspect ratio,
and then further clamped by the <a>max size property</a> in the relevant dimension if that is <a>definite</a>.
</dl>
<div class="note" id="min-size-opt">
Note that while a content-based minimum size is often appropriate,
and helps prevent content from overlapping or spilling outside its container,
in some cases it is not:
In particular, if flex sizing is being used for a major content area of a document,
it is better to set an explicit font-relative minimum width such as ''min-width: 12em''.
A content-based minimum width could result in a large table or large image
stretching the size of the entire content area into an overflow zone,
and thereby making lines of text gratuitously long and hard to read.
Note also, when content-based sizing is used on an item with large amounts of content,
the layout engine must traverse all of this content before finding its minimum size,
whereas if the author sets an explicit minimum, this is not necessary.
(For items with small amounts of content, however,
this traversal is trivial and therefore not a performance concern.)
</div>
<!--
████████ ██ ██ ████████ ██ ████ ██████ ████ ████████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ███ ████████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██ ██ ██ ████████ ████ ██████ ████ ██
-->
<h2 id='grid-definition'>
The Explicit Grid</h2>
The three properties 'grid-template-rows', 'grid-template-columns', and 'grid-template-areas'
together define the <dfn export local-lt="explicit">explicit grid</dfn> of a <a>grid container</a>.
The 'grid-template' property is a shorthand that sets all three at the same time.
The final grid may end up larger due to <a>grid items</a> placed outside the <a>explicit grid</a>;
in this case, any implicit tracks are sized by the 'grid-auto-rows' and 'grid-auto-columns' properties.
The size of the <a>explicit grid</a> is determined by the larger of
the number of rows/columns defined by 'grid-template-areas'
and the number of rows/columns sized by 'grid-template-rows'/'grid-template-columns'.
Any rows/columns defined by 'grid-template-areas' but not sized by 'grid-template-rows'/'grid-template-columns'
take their size from the 'grid-auto-rows'/'grid-auto-columns' properties.
If these properties don't define <em>any</em> <a>explicit</a> tracks,
the <a>explicit grid</a> still contains one <a>grid line</a> in each axis.
Numeric indexes in the <a>grid-placement properties</a>
count from the edges of the <a>explicit grid</a>.
Positive indexes count from the <a>start</a> side
(starting from 1 for the <a>start</a>-most <a>explicit</a> line),
while negative indexes count from the <a>end</a> side
(starting from -1 for the <a>end</a>-most <a>explicit</a> line).
<h3 id='track-sizing'>
Track Sizing: the 'grid-template-rows' and 'grid-template-columns' properties</h3>
<pre class='propdef'>
Name: grid-template-columns, grid-template-rows
Value: none | <<track-list>> | <<auto-track-list>> | subgrid <<line-name-list>>?
Initial: none
Applies to: <a>grid containers</a>
Inherited: no
Percentages: refer to corresponding dimension of the content area
Media: visual
Computed value: As specified, with lengths made absolute
</pre>
These properties specify,
as a space-separated <dfn export>track list</dfn>,
the line names and <a>track sizing functions</a> of the <a>grid</a>.
Each <dfn lt="track sizing function|sizing function">track sizing function</dfn> can be specified as a length,
a percentage of the <a>grid container</a>’s size,
a measurement of the contents occupying the column or row,
or a fraction of the free space in the grid.
It can also be specified as a range using the ''minmax()'' notation,
which can combine any of the previously mentioned mechanisms
to specify separate <a lt="min track sizing function">min</a>
and <a>max track sizing functions</a> for the column or row.
The 'grid-template-columns' property specifies the <a>track list</a> for the grid's columns,
while 'grid-template-rows' specifies the <a>track list</a> for the grid's rows.
The <dfn value for="grid-template-rows, grid-template-columns">none</dfn> value indicates that there is no <a>explicit grid</a>;
any rows/columns will be implicitly generated,
and their size will be determined by the 'grid-auto-rows' and 'grid-auto-columns' properties.
The <dfn value for="grid-template-rows, grid-template-columns">subgrid</dfn> value indicates that the grid will align to its parent grid in that axis.
Rather than specifying the sizes of rows/columns explicitly,
they'll be taken from the parent grid's definition.
The syntax of a <a>track list</a> is:
<pre>
<dfn><track-list></dfn> = [ <<line-names>>? [ <<track-size>> | <<track-repeat>> ] ]+ <<line-names>>?
<dfn><auto-track-list></dfn> = [ <<line-names>>? [ <<fixed-size>> | <<fixed-repeat>> ] ]+ <<auto-repeat>>
[ <<line-names>>? [ <<fixed-size>> | <<fixed-repeat>> ] ]+ <<line-names>>?
<dfn><line-name-list></dfn> = [ <<line-names>> | <<name-repeat>> ]+
<dfn><track-size></dfn> = <<track-breadth>> | minmax( <<track-breadth>> , <<track-breadth>> )
<dfn><fixed-size></dfn> = <<fixed-breadth>> | minmax( <<fixed-breadth>> , <<track-breadth>> )
<dfn><track-breadth></dfn> = <<length>> | <<percentage>> | <<flex>> | min-content | max-content | auto
<dfn><fixed-breadth></dfn> = <<length>> | <<percentage>>