Skip to content

Commit dc2ae62

Browse files
estellepepelsbey
andauthored
Update CSS width properrty (mdn#43111)
* 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>
1 parent d783c87 commit dc2ae62

File tree

2 files changed

+207
-55
lines changed

2 files changed

+207
-55
lines changed

files/en-us/web/css/reference/properties/width/index.md

Lines changed: 195 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ The specified value of `width` applies to the content area so long as its value
5959
/* <length> values */
6060
width: 300px;
6161
width: 25em;
62-
width: anchor-size(width);
63-
width: anchor-size(--my-anchor inline, 120%);
6462

6563
/* <percentage> value */
6664
width: 75%;
@@ -69,10 +67,15 @@ width: 75%;
6967
width: max-content;
7068
width: min-content;
7169
width: fit-content;
72-
width: fit-content(20em);
7370
width: auto;
7471
width: stretch;
7572

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+
7679
/* Global values */
7780
width: inherit;
7881
width: initial;
@@ -95,8 +98,12 @@ width: unset;
9598
- : The intrinsic minimum width.
9699
- {{cssxref("fit-content")}}
97100
- : 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-positioned element, the first parameter defaults to the associated anchor's width. If applied to a non-anchor-positioned element, it sets the width to the fallback value. If no fallback is defined, the declaration is ignored.
103+
- {{cssxref("calc-size()")}}
104+
- : Sets the width to a modified intrinsic size.
98105
- [`fit-content(<length-percentage>)`](/en-US/docs/Web/CSS/Reference/Values/fit-content_function)
99-
- : 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>))`.
100107
- `stretch`
101108
- : 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).
102109

@@ -119,116 +126,183 @@ Ensure that elements set with a `width` aren't truncated and/or don't obscure ot
119126

120127
### Default width
121128

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+
122144
```css
123-
p.gold {
145+
p {
124146
background: gold;
125147
}
148+
p.has-width {
149+
width: auto;
150+
}
126151
```
127152

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+
128167
```html
129-
<p class="gold">The MDN community writes really great documentation.</p>
168+
<div class="px_length">Width measured in px</div>
169+
<div class="em_length">Width measured in em</div>
130170
```
131171

132-
{{EmbedLiveSample('Default_width', '500px', '64px')}}
172+
#### CSS
133173

134-
### Example using pixels and ems
174+
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.
135175

136176
```css
137177
.px_length {
138178
width: 200px;
179+
139180
background-color: red;
140181
color: white;
141182
border: 1px solid black;
142183
}
143184

144185
.em_length {
145186
width: 20em;
187+
146188
background-color: white;
147189
color: red;
148190
border: 1px solid black;
149191
}
150192
```
151193

194+
#### Results
195+
196+
{{EmbedLiveSample("Using length units", 600, 60)}}
197+
198+
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+
152208
```html
153-
<div class="px_length">Width measured in px</div>
154-
<div class="em_length">Width measured in em</div>
209+
<div class="percent">Width in percentage</div>
155210
```
156211

157-
{{EmbedLiveSample('Example using pixels and ems', '500px', '64px')}}
212+
#### CSS
158213

159-
### Example with percentage
214+
We set the `width` of the element to be `20%` of the width of its parent container.
160215

161216
```css
162217
.percent {
163218
width: 20%;
219+
164220
background-color: silver;
165221
border: 1px solid red;
166222
}
167223
```
168224

169-
```html
170-
<div class="percent">Width in percentage</div>
171-
```
225+
#### Results
172226

173-
{{EmbedLiveSample('Example using percentage', '500px', '64px')}}
227+
{{EmbedLiveSample("Using percentages", 600, 60)}}
174228

175-
### Example using "max-content"
229+
### Using intrinsic sizes
176230

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

184237
```html
185238
<p class="max-green">The MDN community writes really great documentation.</p>
239+
<p class="min-blue">The MDN community writes really great documentation.</p>
240+
<p class="min-pink">The MDN community writes really great documentation.</p>
186241
```
187242

188-
{{EmbedLiveSample('Example using "max-content"', '500px', '64px')}}
243+
#### CSS
189244

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

192247
```css
248+
p.max-green {
249+
width: max-content;
250+
251+
background-color: lightgreen;
252+
border-style: dotted;
253+
}
254+
193255
p.min-blue {
194-
background: lightblue;
195256
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;
196267
}
197268
```
198269

199-
```html
200-
<p class="min-blue">The MDN community writes really great documentation.</p>
270+
```css hidden
271+
@supports not (width: calc-size(min-content, size * 2)) {
272+
body::after {
273+
content: "Your browser doesn't support the calc-size() function yet.";
274+
background-color: wheat;
275+
display: block;
276+
text-align: center;
277+
padding: 1em;
278+
}
279+
}
201280
```
202281

203-
{{EmbedLiveSample('Example using "min-content"', '500px', '155px')}}
282+
#### Results
283+
284+
{{EmbedLiveSample("Using intrinsic sizes", 600, 230)}}
285+
286+
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`.
204287

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

207292
#### HTML
208293

294+
We include a parent container with two child elements.
295+
209296
```html
210297
<div class="parent">
211298
<div class="child">text</div>
212-
</div>
213-
214-
<div class="parent">
215299
<div class="child stretch">stretch</div>
216300
</div>
217301
```
218302

219303
#### CSS
220304

221-
```css hidden
222-
@supports not (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`.
232306

233307
```css
234308
.parent {
@@ -247,9 +321,83 @@ p.min-blue {
247321
}
248322
```
249323

324+
```css hidden
325+
@supports not (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+
250336
#### Result
251337

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+
<div class="anchor">⚓︎</div>
352+
353+
<div class="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: 1px solid black;
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: 1px solid #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.
253401

254402
## Specifications
255403

@@ -265,7 +413,8 @@ p.min-blue {
265413
- {{cssxref("box-sizing")}}
266414
- {{cssxref("min-width")}}, {{cssxref("max-width")}}
267415
- {{cssxref("block-size")}}, {{cssxref("inline-size")}}
268-
- {{cssxref("anchor-size()")}}
269416
- SVG {{SVGAttr("width")}} attribute
270417
- [Introduction to the CSS box model](/en-US/docs/Web/CSS/Guides/Box_model/Introduction) guide
271418
- [CSS box model](/en-US/docs/Web/CSS/Guides/Box_model) module
419+
- [CSS anchor positioning](/en-US/docs/Web/CSS/Guides/Anchor_positioning) module
420+
- [CSS values and units](/en-US/docs/Web/CSS/Guides/Values_and_units) module

0 commit comments

Comments
 (0)