You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* update examples for background-attachment so you can see the effects of each value.
* update examples for background-attachment so you can see the effects of each value.
* update examples for background-attachment so you can see the effects of each value.
* width prop examples
* revert unrelated update
* anchor size example
* revert
* revert
* Apply suggestions from code review
Co-authored-by: Vadim Makeev <hi@pepelsbey.dev>
* Update files/en-us/web/css/reference/properties/width/index.md
---------
Co-authored-by: Vadim Makeev <hi@pepelsbey.dev>
@@ -59,8 +59,6 @@ The specified value of `width` applies to the content area so long as its value
59
59
/* <length> values */
60
60
width: 300px;
61
61
width: 25em;
62
-
width: anchor-size(width);
63
-
width: anchor-size(--my-anchor inline, 120%);
64
62
65
63
/* <percentage> value */
66
64
width: 75%;
@@ -69,10 +67,15 @@ width: 75%;
69
67
width: max-content;
70
68
width: min-content;
71
69
width: fit-content;
72
-
width: fit-content(20em);
73
70
width: auto;
74
71
width: stretch;
75
72
73
+
/* function values */
74
+
width: fit-content(20em);
75
+
width: anchor-size(width);
76
+
width: anchor-size(--my-anchor inline, 120%);
77
+
width: calc-size(max-content, size / 2);
78
+
76
79
/* Global values */
77
80
width: inherit;
78
81
width: initial;
@@ -95,8 +98,12 @@ width: unset;
95
98
- : The intrinsic minimum width.
96
99
- {{cssxref("fit-content")}}
97
100
- : Use the available space, but not more than [max-content](/en-US/docs/Web/CSS/Reference/Values/max-content), i.e., `min(max-content, max(min-content, stretch))`.
101
+
- {{cssxref("anchor-size()")}}
102
+
- : Sets the width relative to a dimension of an anchor element. When defining the `width` of an anchor-positionedelement, the first parameter defaults to the associated anchor's width. If applied to anon-anchor-positionedelement, it sets the width to the fallback value. If no fallback is defined, the declaration is ignored.
- : Uses the fit-content formula with the available space replaced by the specified argument, i.e., `min(max-content, max(min-content, <length-percentage>))`.
106
+
- : Uses the fit-content formula with the available space replaced by the specified argument, clamping the width according to the formula `min(maximum size, max(minimum size, <length-percentage>))`.
100
107
- `stretch`
101
108
- : Sets the width of the element's [margin box](/en-US/docs/Learn_web_development/Core/Styling_basics/Box_model#parts_of_a_box) to the width of its [containing block](/en-US/docs/Web/CSS/Guides/Display/Containing_block#identifying_the_containing_block). It attempts to make the margin box fill the available space in the containing block, so in a way behaving similar to `100%` but applying the resulting size to the margin box rather than the box determined by [box-sizing](/en-US/docs/Web/CSS/Reference/Properties/box-sizing).
102
109
@@ -119,116 +126,183 @@ Ensure that elements set with a `width` aren't truncated and/or don't obscure ot
119
126
120
127
### Default width
121
128
129
+
This example demonstrates basic usage and the default `auto` value.
130
+
131
+
#### HTML
132
+
133
+
We include two paragraphs; one with a class name.
134
+
135
+
```html
136
+
<p>The MDN community writes really great documentation.</p>
137
+
<p class="has-width">The MDN community writes really great documentation.</p>
138
+
```
139
+
140
+
#### CSS
141
+
142
+
We make all paragraphs have a gold background, explicitly setting the second paragraph's `width` to `auto`.
143
+
122
144
```css
123
-
p.gold {
145
+
p {
124
146
background: gold;
125
147
}
148
+
p.has-width {
149
+
width: auto;
150
+
}
126
151
```
127
152
153
+
#### Results
154
+
155
+
{{EmbedLiveSample("Default width", 600, 100)}}
156
+
157
+
As `width` defaults to `auto`, both paragraphs are the same width.
158
+
159
+
### Using length units
160
+
161
+
This example demonstrates length width values.
162
+
163
+
#### HTML
164
+
165
+
We include two {{htmlelement("div")}} elements with some text.
166
+
128
167
```html
129
-
<pclass="gold">The MDN community writes really great documentation.</p>
The `px_length` element is set to `200px` while the `em_length` element is set to be `20em` wide. Both elements also have different {{cssxref("background-color")}}, {{cssxref("color")}}, and {{cssxref("border")}} values to enable differentiating between the two when rendered.
The `px_length` element will always be 200px wide. The rendered width of the `em_length` element depends on the font size.
199
+
200
+
### Using percentages
201
+
202
+
This example demonstrates using percentage values.
203
+
204
+
#### HTML
205
+
206
+
We include one {{htmlelement("div")}} element with some text.
207
+
152
208
```html
153
-
<divclass="px_length">Width measured in px</div>
154
-
<divclass="em_length">Width measured in em</div>
209
+
<divclass="percent">Width in percentage</div>
155
210
```
156
211
157
-
{{EmbedLiveSample('Example using pixels and ems', '500px', '64px')}}
212
+
#### CSS
158
213
159
-
### Example with percentage
214
+
We set the `width` of the element to be `20%` of the width of its parent container.
160
215
161
216
```css
162
217
.percent {
163
218
width: 20%;
219
+
164
220
background-color: silver;
165
221
border: 1pxsolidred;
166
222
}
167
223
```
168
224
169
-
```html
170
-
<divclass="percent">Width in percentage</div>
171
-
```
225
+
#### Results
172
226
173
-
{{EmbedLiveSample('Example using percentage', '500px', '64px')}}
227
+
{{EmbedLiveSample("Using percentages", 600, 60)}}
174
228
175
-
### Example using "max-content"
229
+
### Using intrinsic sizes
176
230
177
-
```css
178
-
p.max-green {
179
-
background: lightgreen;
180
-
width: max-content;
181
-
}
182
-
```
231
+
This example compares `max-content` and `min-content`, and introduces `calc-size`.
232
+
233
+
#### HTML
234
+
235
+
We include three paragraphs with the same content; just their class names differ.
183
236
184
237
```html
185
238
<pclass="max-green">The MDN community writes really great documentation.</p>
239
+
<pclass="min-blue">The MDN community writes really great documentation.</p>
240
+
<pclass="min-pink">The MDN community writes really great documentation.</p>
186
241
```
187
242
188
-
{{EmbedLiveSample('Example using "max-content"', '500px', '64px')}}
243
+
#### CSS
189
244
190
-
### Example using "min-content"
245
+
We set one paragraph's `width` to `max-content`, the second to `min-content`, and the third to be twice the size of the `min-content` by using the `calc-size()` function. Each is given a different {{cssxref("background-color")}} and {{cssxref("border-style")}} to enable differentiating between the two.
191
246
192
247
```css
248
+
p.max-green {
249
+
width: max-content;
250
+
251
+
background-color: lightgreen;
252
+
border-style: dotted;
253
+
}
254
+
193
255
p.min-blue {
194
-
background: lightblue;
195
256
width: min-content;
257
+
258
+
background-color: lightblue;
259
+
border-style: dashed;
260
+
}
261
+
262
+
p.min-pink {
263
+
width: calc-size(min-content, size * 2);
264
+
265
+
background-color: pink;
266
+
border-style: solid;
196
267
}
197
268
```
198
269
199
-
```html
200
-
<pclass="min-blue">The MDN community writes really great documentation.</p>
The `max-content` example is as wide as the text. The `min-content` example is as wide as the widest word. The `calc-size()` example is set to be twice as wide as the `min-content`.
204
287
205
-
### Stretch width to fill the containing block
288
+
### Using the stretch keyword
289
+
290
+
This example demonstrates the `stretch` value within a [flex](/en-US/docs/Web/CSS/Guides/Flexible_box_layout) container.
206
291
207
292
#### HTML
208
293
294
+
We include a parent container with two child elements.
295
+
209
296
```html
210
297
<divclass="parent">
211
298
<divclass="child">text</div>
212
-
</div>
213
-
214
-
<divclass="parent">
215
299
<divclass="child stretch">stretch</div>
216
300
</div>
217
301
```
218
302
219
303
#### CSS
220
304
221
-
```css hidden
222
-
@supportsnot (width: stretch) {
223
-
.parent {
224
-
display: none!important;
225
-
}
226
-
227
-
body::after {
228
-
content: "Your browser doesn't support the `stretch` value yet.";
229
-
}
230
-
}
231
-
```
305
+
We use the {{cssxref("display")}} property to make the parent a flex container, and set the second child's `width` the `stretch`.
232
306
233
307
```css
234
308
.parent {
@@ -247,9 +321,83 @@ p.min-blue {
247
321
}
248
322
```
249
323
324
+
```css hidden
325
+
@supportsnot (width: stretch) {
326
+
body::after {
327
+
content: "Your browser doesn't support the stretch value yet.";
328
+
background-color: wheat;
329
+
display: block;
330
+
text-align: center;
331
+
padding: 1em;
332
+
}
333
+
}
334
+
```
335
+
250
336
#### Result
251
337
252
-
{{EmbedLiveSample('Stretch width to fill the containing block', 'auto', 250)}}
338
+
{{EmbedLiveSample("Using the stretch keyword", "auto", 100)}}
339
+
340
+
By default, flex items are as wide as their content. The `stretch` value makes the element as wide as the available space allows, with the element's margin box otherwise clamped to the width of its containing block.
341
+
342
+
### Using the anchor-size() function
343
+
344
+
This example demonstrates using the `anchor-size()` function to define the width of an anchor-positioned element; we defined its width as a multiple of its anchor's height.
345
+
346
+
#### HTML
347
+
348
+
We specify two {{htmlelement("div")}} elements: one `anchor` element and one `infobox` element that we'll position relative to the anchor.
349
+
350
+
```html
351
+
<divclass="anchor">⚓︎</div>
352
+
353
+
<divclass="infobox">
354
+
<p>Infobox.</p>
355
+
</div>
356
+
```
357
+
358
+
#### CSS
359
+
360
+
We declare the `anchor``<div>` as an anchor element by giving it an {{cssxref("anchor-name")}}. The positioned element has its {{cssxref("position")}} property set to `absolute`, and is associated with the anchor element via its {{cssxref("position-anchor")}} property. We also set absolute {{cssxref("height")}} and {{cssxref("width")}} dimensions on the anchor and define the width of the anchor-positioned element to be the width of the anchor using the `anchor-size()` function as the value of the `width` property. As a bonus, we also use the `anchor-size()` function to define the infobox's {{cssxref("left")}} position, making the gap between the anchor and the infobox one quarter of the height of the anchor.
361
+
362
+
```css hidden
363
+
.anchor {
364
+
anchor-name: --my-anchor;
365
+
width: 120px;
366
+
height: 60px;
367
+
368
+
font-size: 2rem;
369
+
background-color: lightpink;
370
+
text-align: center;
371
+
align-content: center;
372
+
outline: 1pxsolidblack;
373
+
}
374
+
375
+
.infobox {
376
+
position-anchor: --my-anchor;
377
+
position: absolute;
378
+
position-area: right;
379
+
width: anchor-size(width);
380
+
381
+
left: calc( anchor-size(height) / 4 )
382
+
383
+
align-content: center;
384
+
color: darkblue;
385
+
background-color: azure;
386
+
outline: 1pxsolid#dddddd;
387
+
}
388
+
```
389
+
390
+
```css hidden
391
+
body {
392
+
padding: 5em;
393
+
}
394
+
```
395
+
396
+
#### Results
397
+
398
+
{{EmbedLiveSample("Using the anchor-size() function", "auto", 200)}}
399
+
400
+
Note how the infobox's width is always the same as the anchor element's width.
0 commit comments