aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/grid-template-columns/index.html
blob: 00ed19a00f6f0085a3171fa4268fb139b47cb0fc (plain)
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
---
title: grid-template-columns
slug: Web/CSS/grid-template-columns
tags:
  - grid-template-columns
translation_of: Web/CSS/grid-template-columns
---
<p><strong><code>grid-template-columns</code></strong> 该属性是基于 {{glossary("grid column", "网格列")}}. 的维度,去定义网格线的名称和网格轨道的尺寸大小。</p>

<div>{{EmbedInteractiveExample("pages/css/grid-template-columns.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="brush: css no-line-numbers">/* Keyword value */
grid-template-columns: none;

/* &lt;track-list&gt; values */
grid-template-columns: 100px 1fr;
grid-template-columns: [linename] 100px;
grid-template-columns: [linename1] 100px [linename2 linename3];
grid-template-columns: minmax(100px, 1fr);
grid-template-columns: fit-content(40%);
grid-template-columns: repeat(3, 200px);

/* &lt;auto-track-list&gt; values */
grid-template-columns: 200px repeat(auto-fill, 100px) 300px;
grid-template-columns: minmax(100px, max-content)
                       repeat(auto-fill, 200px) 20%;
grid-template-columns: [linename1] 100px [linename2]
                       repeat(auto-fit, [linename3 linename4] 300px)
                       100px;
grid-template-columns: [linename1 linename2] 100px
                       repeat(auto-fit, [linename1] 300px) [linename3];

/* Global values */
grid-template-columns: inherit;
grid-template-columns: initial;
grid-template-columns: unset;
</pre>

<h3 id="值"></h3>

<dl>
 <dt><code>none</code></dt>
 <dd>这个关键字表示不明确的网格。所有的列和其大小都将由{{cssxref("grid-auto-columns")}} 属性隐式的指定。</dd>
 <dt>{{cssxref("&lt;length&gt;")}}</dt>
 <dd>非负值的长度大小。</dd>
 <dt>{{cssxref("&lt;percentage&gt;")}}</dt>
 <dd>非负值且相对于网格容器的 {{cssxref("percentage", "&lt;百分比&gt;")}}。 如果网格容器的尺寸大小依赖网格轨道的大小(比如 <font face="consolas, Liberation Mono, courier, monospace">inline-grid</font> ),则百分比值将被视为<code>auto</code></dd>
 <dd>为了遵守网格的百分比,网格轨道本身定义的大小,将自动被调整为相对网格容器大小,并且是以最小量将网格轨道调整到最终的大小。</dd>
 <dt>{{cssxref("&lt;flex&gt;")}}</dt>
 <dd>非负值,用单位 <code>fr</code> 来定义网格轨道大小的弹性系数。 每个定义了 <code>&lt;flex&gt;</code> 的网格轨道会按比例分配剩余的可用空间。当外层用一个 <code>minmax()</code> 表示时,它将是一个自动最小值(即 <code>minmax(auto, &lt;flex&gt;)</code> ) .</dd>
 <dt id="max-content"><code>max-content</code></dt>
 <dd>是一个用来表示以网格项的最大的内容来占据网格轨道的关键字。</dd>
 <dt><code>min-content</code></dt>
 <dd>是一个用来表示以网格项的最大的最小内容来占据网格轨道的关键字。</dd>
 <dt>{{cssxref("minmax", "minmax(min, max)")}}</dt>
 <dd>是一个来定义大小范围的属性,大于等于min值,并且小于等于max值。如果max值小于min值,则该值会被视为min值。最大值可以设置为网格轨道系数值<code>&lt;flex&gt;</code> ,但最小值则不行。</dd>
 <dd>
 <p class="note">Note:  该规范在将来可能会允许设置最小值为 <code>flex</code> ,也会调整网格轨道算法(<a href="https://www.w3.org/TR/css-grid-1/#track-sizing-algorithm">track sizing algorithm</a>) 计算出正确的大小。</p>
 </dd>
 <dt id="auto"><code>auto</code></dt>
 <dd>如果该网格轨道为最大时,该属性等同于 <code>&lt;max-content&gt;</code> ,为最小时,则等同于 <code>&lt;min-content&gt;</code> 。</dd>
 <dd>
 <p class="note">Note: 网格轨道大小为 <code>auto</code> (且只有为 <code>auto</code> ) 时,才可以被属性{{cssxref("align-content")}}{{cssxref("justify-content")}} 拉伸 。</p>
 </dd>
 <dt id="fit-content()"><code>{{cssxref("fit-content", "fit-content( [ &lt;length&gt; | &lt;percentage&gt; ] )")}}</code></dt>
 <dd>相当于公式 <code>min(max-content, max(auto, <var>argument</var>))</code>,类似于<code>auto</code> 的计算(即 <code>minmax(auto, max-content)</code>),除了网格轨道大小值是确定下来的,否则该值都大于 <code>auto</code> 的最小值。</dd>
 <dt>{{cssxref("repeat", "repeat( [ &lt;positive-integer&gt; | auto-fill | auto-fit ] , &lt;track-list&gt; )")}}</dt>
 <dd>表示网格轨道的重复部分,以一种更简洁的方式去表示大量而且重复列的表达式。</dd>
</dl>

<h3 id="正式语法">正式语法</h3>

<pre class="syntaxbox">{{csssyntax}}</pre>

<h2 id="示例">示例</h2>

<h3 id="CSS">CSS</h3>

<pre class="brush:css">#grid {
  display: grid;
  width: 100%;
  grid-template-columns: 50px 1fr;
}

#areaA {
  background-color: lime;
}

#areaB {
  background-color: yellow;
}</pre>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;div id="grid"&gt;
  &lt;div id="areaA"&gt;A&lt;/div&gt;
  &lt;div id="areaB"&gt;B&lt;/div&gt;
&lt;/div&gt;</pre>

<h3 id="结果">结果</h3>

<p>{{EmbedLiveSample("示例", "100%", "20px")}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("CSS3 Grid", "#propdef-grid-template-columns", "grid-template-columns")}}</td>
   <td>{{Spec2("CSS3 Grid")}}</td>
   <td>初始定义</td>
  </tr>
 </tbody>
</table>

<p>{{cssinfo}}</p>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("css.properties.grid-template-columns")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>相关CSS属性: {{cssxref("grid-template-rows")}}, {{cssxref("grid-template-areas")}}, {{cssxref("grid-template")}}</li>
 <li>网格布局教程: <em><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout#Grid_Tracks">Basic concepts of grid layout - Grid Tracks</a></em></li>
 <li>视频教程: <em><a href="http://gridbyexample.com/video/series-define-a-grid/">Defining a Grid</a></em></li>
</ul>

<section class="Quick_links" id="Quick_Links">
<ol>
 <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li>
 <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li>
 <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout">CSS Grid Layout</a></li>
 <li data-default-state="open"><a href="#"><strong>Guides</strong></a>
  <ol>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout">Basics concepts of grid layout</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Relationship_of_Grid_Layout">Relationship to other layout methods</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Line-based_Placement_with_CSS_Grid">Line-based placement</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Grid_Template_Areas">Grid template areas</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Layout_using_Named_Grid_Lines">Layout using named grid lines</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Auto-placement_in_CSS_Grid_Layout">Auto-placement in grid layout</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Box_Alignment_in_CSS_Grid_Layout">Box alignment in grid layout</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/CSS_Grid,_Logical_Values_and_Writing_Modes">Grids, logical values and writing modes</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/CSS_Grid_Layout_and_Accessibility">CSS Grid Layout and Accessibility</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/CSS_Grid_and_Progressive_Enhancement">CSS Grid Layout and Progressive Enhancement</a></li>
   <li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Realizing_common_layouts_using_CSS_Grid_Layout">Realizing common layouts using grids</a></li>
  </ol>
 </li>
 <li data-default-state="open"><a href="#"><strong>Properties</strong></a>
  <ol>
   <li><a href="/en-US/docs/Web/CSS/grid">grid</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-area">grid-area</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-auto-columns">grid-auto-columns</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-auto-flow">grid-auto-flow</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-auto-rows">grid-auto-rows</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-column">grid-column</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-column-end">grid-column-end</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-column-gap">grid-column-gap</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-column-start">grid-column-start</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-gap">grid-gap</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-row">grid-row</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-row-end">grid-row-end</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-row-gap">grid-row-gap</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-row-start">grid-row-start</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-template">grid-template</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-template-areas">grid-template-areas</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-template-columns">grid-template-columns</a></li>
   <li><a href="/en-US/docs/Web/CSS/grid-template-rows">grid-template-rows</a></li>
  </ol>
 </li>
 <li data-default-state="open"><a href="#"><strong>Glossary</strong></a>
  <ol>
   <li><a href="/en-US/docs/Glossary/Grid_lines">Grid lines</a></li>
   <li><a href="/en-US/docs/Glossary/Grid_tracks">Grid tracks</a></li>
   <li><a href="/en-US/docs/Glossary/Grid_cell">Grid cell</a></li>
   <li><a href="/en-US/docs/Glossary/Grid_areas">Grid areas</a></li>
   <li><a href="/en-US/docs/Glossary/Gutters">Gutters</a></li>
   <li><a href="/en-US/docs/Glossary/Grid_rows">Grid row</a></li>
   <li><a href="/en-US/docs/Glossary/Grid_column">Grid column</a></li>
  </ol>
 </li>
</ol>
</section>