aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/web/css/flex/index.html
blob: c10382dbf70d38cc84ae7efd70d68d0865ae49c3 (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
---
title: flex
slug: Web/CSS/flex
translation_of: Web/CSS/flex
---
<div>{{CSSRef}}</div>

<p>A propriedade flex do CSS, define como um ítem será posicionado para no espaço disponível dentro de seu container.</p>

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



<h2 id="Propriedades">Propriedades</h2>

<p>Esta propriedade é uma abreviação das seguintes propriedades CSS:</p>

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

<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>By default flex items don't shrink below their minimum content size. To change this, set the item's {{cssxref("min-width")}} or {{cssxref("min-height")}}.</p>
</div>

<h2 id="Syntax" name="Syntax">Sintaxe</h2>

<pre class="brush:css no-line-numbers notranslate">/* Propriedades principais */
flex: auto;
flex: initial;
flex: none;

/* Valor único, sem unidade: flex-grow */
flex: 2;

/* Valor único, unidade width/height: flex-basis */
flex: 10em;
flex: 30%;
flex: min-content;

/* Dois valores: flex-grow | flex-basis */
flex: 1 30px;

/* Dois valores: flex-grow | flex-shrink */
flex: 2 2;

/* Três valores: flex-grow | flex-shrink | flex-basis */
flex: 2 2 10%;

/* Valores globais */
flex: inherit;
flex: initial;
flex: unset;
</pre>

<p>The <code>flex</code> property may be specified using one, two, or three values.</p>

<ul>
 <li><strong>One-value syntax</strong>: the value must be one of:

  <ul>
   <li>a <code>&lt;number&gt;</code>: In this case it is interpreted as <code>flex: &lt;number&gt; 1 0</code>; the <code><a href="#&lt;'flex-shrink'>">&lt;flex-shrink&gt;</a></code> value is assumed to be 1 and the <code><a href="#&lt;'flex-basis'>">&lt;flex-basis&gt;</a></code> value is assumed to be <code>0</code>.</li>
   <li>one of the keywords: <code><a href="#none">none</a></code>, <code><a href="#auto">auto</a></code>, or <code>initial</code>.</li>
  </ul>
 </li>
 <li><strong>Two-value syntax</strong>: the first value must be a {{cssxref("&lt;number&gt;")}} and it is interpreted as <code><a href="#&lt;'flex-grow'>">&lt;flex-grow&gt;</a></code>. The second value must be one of:
  <ul>
   <li>a {{cssxref("&lt;number&gt;")}}: then it is interpreted as <code><a href="#&lt;'flex-shrink'>">&lt;flex-shrink&gt;</a></code>.</li>
   <li>a valid value for {{cssxref("width")}}: then it is interpreted as <code><a href="#&lt;'flex-basis'>">&lt;flex-basis&gt;</a></code>.</li>
  </ul>
 </li>
 <li><strong>Three-value syntax:</strong> the values must be in the following order:
  <ol>
   <li>a {{cssxref("&lt;number&gt;")}} for <code><a href="#&lt;'flex-grow'>">&lt;flex-grow&gt;</a></code>.</li>
   <li>a {{cssxref("&lt;number&gt;")}} for <code><a href="#&lt;'flex-grow'>">&lt;flex-shrink&gt;</a></code>.</li>
   <li>a valid value for {{cssxref("width")}} for <code><a href="#&lt;'flex-basis'>">&lt;flex-basis&gt;</a></code>.</li>
  </ol>
 </li>
</ul>

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

<dl>
 <dt><code>initial</code></dt>
 <dd>The item is sized according to its <code>width</code> and <code>height</code> properties. It shrinks to its minimum size to fit the container, but does not grow to absorb any extra free space in the flex container. This is equivalent to setting "<code>flex: 0 1 auto</code>".</dd>
 <dt><a id="auto" name="auto"><code>auto</code></a></dt>
 <dd>The item is sized according to its <code>width</code> and <code>height</code> properties, but grows to absorb any extra free space in the flex container, and shrinks to its minimum size to fit the container. This is equivalent to setting "<code>flex: 1 1 auto</code>".</dd>
 <dt><a id="none" name="none"><code>none</code></a></dt>
 <dd>The item is sized according to its <code>width</code> and <code>height</code> properties. It is fully inflexible: it neither shrinks nor grows in relation to the flex container. This is equivalent to setting "<code>flex: 0 0 auto</code>".</dd>
 <dt><a id="&lt;'flex-grow'>" name="&lt;'flex-grow'>"><code>&lt;'flex-grow'&gt;</code></a></dt>
 <dd>Defines the {{cssxref("flex-grow")}} of the flex item. Negative values are considered invalid. Defaults to <code>1</code> when omitted.</dd>
 <dt><a id="&lt;'flex-shrink'>" name="&lt;'flex-shrink'>"><code id="&lt;'flex-shrink'>">&lt;'flex-shrink'&gt;</code></a></dt>
 <dd>Defines the {{cssxref("flex-shrink")}} of the flex item. Negative values are considered invalid. Defaults to <code>1</code> when omitted.</dd>
 <dt><a id="&lt;'flex-basis'>" name="&lt;'flex-basis'>"><code id="&lt;'flex-basis'>">&lt;'flex-basis'&gt;</code></a></dt>
 <dd>Defines the {{cssxref("flex-basis")}} of the flex item. A preferred size of <code>0</code> must have a unit to avoid being interpreted as a flexibility. Defaults to <code>0</code> when omitted.</dd>
</dl>

<h3 id="Formal_syntax">Formal syntax</h3>

{{csssyntax}}

<h2 id="Example">Example</h2>

<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>

<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>

<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>

<h3 id="Result">Result</h3>

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

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Flexbox', '#flex-property', 'flex')}}</td>
   <td>{{Spec2('CSS3 Flexbox')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

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

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>

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

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li>CSS Flexbox Guide: <em><a href="/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox">Basic Concepts of Flexbox</a></em></li>
 <li>CSS Flexbox Guide: <em><a href="/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Controlling_Ratios_of_Flex_Items_Along_the_Main_Ax">Controlling Ratios of flex items along the main axis</a></em></li>
</ul>