aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/css/flex/index.html
blob: 5057c9f9515a7c91f8195715788178f191ac9070 (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
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
---
title: flex
slug: Web/CSS/flex
tags:
  - CSS
  - CSS Flexible Boxes
  - CSS Property
  - Reference
  - 'recipe:css-shorthand-property'
translation_of: Web/CSS/flex
---
<div>{{CSSRef}}</div>

<p><span class="seoSummary"><strong><code>flex</code></strong><a href="/ja/docs/Web/CSS">CSS</a><a href="/ja/docs/Web/CSS/Shorthand_properties">一括指定プロパティ</a>で、フレックス<em>アイテム</em>をフレックスコンテナーの領域に収めるために、どのように伸長・収縮させるかを指定します。</span></p>

<div>{{EmbedInteractiveExample("pages/css/flex.html")}}</div>

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<h2 id="Constituent_properties" name="Constituent_properties">構成するプロパティ</h2>

<p>このプロパティは以下の CSS プロパティの一括指定です。</p>

<ul>
 <li>{{cssxref("flex-grow")}}</li>
 <li>{{cssxref("flex-shrink")}}</li>
 <li>{{cssxref("flex-basis")}}</li>
</ul>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="brush:css no-line-numbers notranslate">/* キーワード値 */
flex: auto;
flex: initial;
flex: none;

/* 単位がない数値を1つ指定: flex-grow */
flex: 2;

/* 幅または高さを1つ指定: flex-basis */
flex: 10em;
flex: 30%;
flex: min-content;

/* 値を2つ指定: flex-grow | flex-basis */
flex: 1 30px;

/* 値を2つ指定: flex-grow | flex-shrink */
flex: 2 2;

/* 値を3つ指定: flex-grow | flex-shrink | flex-basis */
flex: 2 2 10%;

/* グローバル値 */
flex: inherit;
flex: initial;
flex: unset;
</pre>

<p><code>flex</code> プロパティは1つ、2つ、3つの値を取ることがあります。</p>

<ul>
 <li><strong>値1つの構文:</strong> 値は以下のうちの1つです。

  <ul>
   <li><code>&lt;number&gt;</code>: この場合は <code>flex: &lt;number&gt; 1 0</code> と解釈されます。 <code><a href="#&lt;'flex-shrink'>">&lt;flex-shrink&gt;</a></code> の値は 1 と想定され、 <code><a href="#&lt;'flex-basis'>">&lt;flex-basis&gt;</a></code> の値は <code>0</code> と想定されます。</li>
   <li>キーワード: <code><a href="#none">none</a></code>, <code><a href="#auto">auto</a></code>, <code>initial</code> のいずれか。</li>
  </ul>
 </li>
 <li><strong>値2つの構文:</strong>
  <ul>
   <li>1つ目は次の値でなければなりません。
    <ul>
     <li>{{cssxref("&lt;number&gt;")}} であり、 <code><a href="#&lt;'flex-grow'>">&lt;flex-grow&gt;</a></code> として解釈される。</li>
    </ul>
   </li>
   <li>2つ目は次の値でなければなりません。
    <ul>
     <li>{{cssxref("&lt;number&gt;")}}: <code><a href="#&lt;'flex-shrink'>">&lt;flex-shrink&gt;</a></code> として解釈される。</li>
     <li>{{cssxref("width")}} として有効な値: <code><a href="#&lt;'flex-basis'>">&lt;flex-basis&gt;</a></code> として解釈される。</li>
    </ul>
   </li>
  </ul>
 </li>
 <li><strong>値3つの構文:</strong> 値は以下の順序でなければなりません。
  <ol>
   <li>最初の値は単位なしの {{cssxref("&lt;number&gt;")}} でなければならず、 <code><a href="#&lt;'flex-grow'>">&lt;flex-grow&gt;</a></code> として解釈されます。</li>
   <li>2つ目の値は単位なしの {{cssxref("&lt;number&gt;")}} でなければならず、 <code><a href="#&lt;'flex-grow'>">&lt;flex-shrink&gt;</a></code> として解釈されます。</li>
   <li>3つ目の値は {{cssxref("width")}} として有効な値でなければならず、 <code><a href="#&lt;'flex-basis'>">&lt;flex-basis&gt;</a></code> として解釈されます。</li>
  </ol>
 </li>
</ul>

<h3 id="Values" name="Values"></h3>

<dl>
 <dt><code>initial</code></dt>
 <dd>アイテムは <code>width</code> および <code>height</code> プロパティによって大きさが決められます。コンテナーに合うように最小サイズまで収縮しますが、フレックスコンテナーの空き領域を埋めるために伸長することはありません。これは "<code>flex: 0 1 auto</code>" と同等です。</dd>
 <dt id="auto"><code>auto</code></dt>
 <dd>アイテムは <code>width</code> および <code>height</code> プロパティによって大きさが決められますが、フレックスコンテナーの空き領域を埋めるために伸長したり、コンテナーに合うように最小サイズまで収縮したりします。これは "<code>flex: 1 1 auto</code>" と同等です。</dd>
 <dt id="none"><code>none</code></dt>
 <dd>アイテムは <code>width</code> および <code>height</code> プロパティによって大きさが決められます。大きさは完全に固定で、フレックスコンテナーに連動して収縮したり伸長したりすることはありません。これは"<code>flex: 0 0 auto</code>" と同等です。</dd>
 <dt id="&lt;'flex-grow'>"><code>&lt;'flex-grow'&gt;</code></dt>
 <dd>フレックスアイテムの {{cssxref("flex-grow")}} を定義します。負の値は無効とみなされます。省略時の既定値は <code>1</code> です。 (初期値は <code>0</code>)</dd>
 <dt id="&lt;'flex-shrink'>"><code>&lt;'flex-shrink'&gt;</code></dt>
 <dd>フレックスアイテムの {{cssxref("flex-shrink")}} を定義します。負の値は無効とみなされます。省略時の既定値は <code>1</code> です。 (初期値は <code>1</code>)</dd>
 <dt id="&lt;'flex-basis'>"><code>&lt;'flex-basis'&gt;</code></dt>
 <dd>フレックスアイテムの {{cssxref("flex-basis")}} を定義します。希望サイズが <code>0</code> ならば、フレキシビリティとして解釈されないように単位をつけなければなりません。省略時の既定値は <code>1</code> です。 (初期値は <code>auto</code>)</dd>
</dl>

<h2 id="Description" name="Description">解説</h2>

<p>多くの場合、 <code>flex</code> には <code>auto</code>, <code>initial</code>, <code>none</code>, または単位のない正の数を設定してください。これらの値の効果を確認するには、以下のフレックスコンテナーの大きさを変更してみてください。</p>

<div id="flex">
<pre class="hidden brush: html notranslate">&lt;div class="flex-container"&gt;
  &lt;div class="item auto"&gt;auto&lt;/div&gt;
  &lt;div class="item auto"&gt;auto&lt;/div&gt;
  &lt;div class="item auto"&gt;auto&lt;/div&gt;
&lt;/div&gt;

&lt;div class="flex-container"&gt;
  &lt;div class="item auto"&gt;auto&lt;/div&gt;
  &lt;div class="item initial"&gt;initial&lt;/div&gt;
  &lt;div class="item initial"&gt;initial&lt;/div&gt;
&lt;/div&gt;

&lt;div class="flex-container"&gt;
  &lt;div class="item auto"&gt;auto&lt;/div&gt;
  &lt;div class="item auto"&gt;auto&lt;/div&gt;
  &lt;div class="item none"&gt;none&lt;/div&gt;
&lt;/div&gt;

&lt;div class="flex-container"&gt;
  &lt;div class="item initial"&gt;initial&lt;/div&gt;
  &lt;div class="item none"&gt;none&lt;/div&gt;
  &lt;div class="item none"&gt;none&lt;/div&gt;
&lt;/div&gt;

&lt;div class="flex-container"&gt;
  &lt;div class="item four"&gt;4&lt;/div&gt;
  &lt;div class="item two"&gt;2&lt;/div&gt;
  &lt;div class="item one"&gt;1&lt;/div&gt;
&lt;/div&gt;
</pre>

<pre class="hidden brush: css notranslate">* {
  box-sizing: border-box;
}

.flex-container {
  background-color: #F4F7F8;
  resize: horizontal;
  overflow: hidden;
  display: flex;
  margin: 1em;
}

.item {
  margin: 1em;
  padding: 0.5em;
  width: 110px;
  min-width: 0;
  background-color: #1B5385;
  color: white;
  font-family: monospace;
  font-size: 13px;
}

.initial {
  flex: initial;
}

.auto {
  flex: auto;
}

.none {
  flex: none;
}

.four {
  flex: 4;
}

.two {
  flex: 2;
}

.one {
  flex: 1;
}
</pre>

<p>{{EmbedLiveSample("flex", 1200, 370, "", "", "example-outcome-frame")}}</p>

<p>既定ではフレックスアイテムは最小コンテンツサイズよりも収縮することはありません。これを変更するには、 {{cssxref("min-width")}} または {{cssxref("min-height")}} を設定してください。</p>
</div>

<h2 id="Formal_definition" name="Formal_definition">公式定義</h2>

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

<h2 id="Formal_syntax" name="Formal_syntax">形式文法</h2>

{{csssyntax}}

<h2 id="Examples" name="Examples"></h2>

<h3 id="Setting_flex_auto" name="Setting_flex_auto">flex: auto の設定</h3>

<h4 id="HTML">HTML</h4>

<pre class="brush: html notranslate">&lt;div id="flex-container"&gt;
  &lt;div class="flex-item" id="flex"&gt;Flex box (click to toggle raw box)&lt;/div&gt;
  &lt;div class="raw-item" id="raw"&gt;Raw box&lt;/div&gt;
&lt;/div&gt;
</pre>

<h4 id="CSS">CSS</h4>

<pre class="brush: css notranslate">#flex-container {
  display: flex;
  flex-direction: row;
}

#flex-container &gt; .flex-item {
  flex: auto;
}

#flex-container &gt; .raw-item {
  width: 5rem;
}
</pre>

<div class="hidden">
<pre class="brush: js notranslate">var flex = document.getElementById("flex");
var raw = document.getElementById("raw");
flex.addEventListener("click", function() {
  raw.style.display = raw.style.display == "none" ? "block" : "none";
});
</pre>

<pre class="brush: css notranslate">#flex-container {
  width: 100%;
  font-family: Consolas, Arial, sans-serif;
}

#flex-container &gt; div {
  border: 1px solid #f00;
  padding: 1rem;
}

#flex-container &gt; .raw-item {
  border: 1px solid #000;
}
</pre>
</div>

<h4 id="Result" name="Result">結果</h4>

<p>{{EmbedLiveSample('Setting_flex_auto','100%','60')}}</p>

<h2 id="Specifications" name="Specifications">仕様書</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 Flexbox', '#flex-property', 'flex')}}</td>
   <td>{{Spec2('CSS3 Flexbox')}}</td>
   <td>初回定義</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

<p>{{Compat("css.properties.flex")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>CSS フレックスボックスガイド: <em><a href="/ja/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox">フレックスボックスの基本概念</a></em></li>
 <li>CSS フレックスボックスガイド: <em><a href="/ja/docs/Web/CSS/CSS_Flexible_Box_Layout/Controlling_Ratios_of_Flex_Items_Along_the_Main_Ax">主軸に沿ったフレックスアイテムの比率の制御</a></em></li>
</ul>