- Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
227 lines (171 loc) · 6.31 KB
/
index.md
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
---
title: width
slug: Web/CSS/width
page-type: css-property
browser-compat: css.properties.width
---
{{CSSRef}}
The **`width`**[CSS](/en-US/docs/Web/CSS) property sets an element's width. By default, it sets the width of the [content area](/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model#content_area), but if {{cssxref("box-sizing")}} is set to `border-box`, it sets the width of the [border area](/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model#border_area).
{{InteractiveExample("CSS Demo: width")}}
```css interactive-example-choice
width: 150px;
```
```css interactive-example-choice
width: 20em;
```
```css interactive-example-choice
width: 75%;
```
```css interactive-example-choice
width: auto;
```
```htmlinteractive-example
<section class="default-example" id="default-example">
<div class="transition-all" id="example-element">
This is a box where you can change the width.
</div>
</section>
```
```css interactive-example
#example-element {
display: flex;
flex-direction: column;
background-color: #5b6dcd;
height: 80%;
justify-content: center;
color: #ffffff;
}
```
The specified value of `width` applies to the content area so long as its value remains within the values defined by {{cssxref("min-width")}} and {{cssxref("max-width")}}.
- If the value for `width` is less than the value for `min-width`, then `min-width` overrides `width`.
- If the value for `width` is greater than the value for `max-width`, then `max-width` overrides `width`.
> [!NOTE]
> As a geometric property, `width` also applies to the {{SVGElement("svg")}}, {{SVGElement("rect")}}, {{SVGElement("image")}}, and {{SVGElement("foreignObject")}} SVG elements, with `auto` resolving to `100%` for `<svg>` and `0` for other elements, and percent values being relative to the SVG viewport width for `<rect>`. The CSS `width` property value overrides any SVG {{SVGAttr("width")}} attribute value set on the SVG element.
## Syntax
```css
/* <length> values */
width: 300px;
width: 25em;
width: anchor-size(width);
width: anchor-size(--myAnchor inline, 120%);
/* <percentage> value */
width: 75%;
/* Keyword values */
width: max-content;
width: min-content;
width: fit-content;
width: fit-content(20em);
width: auto;
width: stretch;
/* Global values */
width: inherit;
width: initial;
width: revert;
width: revert-layer;
width: unset;
```
### Values
- {{cssxref("<length>")}}
- : Defines the width as a distance value.
- {{cssxref("<percentage>")}}
- : Defines the width as a percentage of the [containing block](/en-US/docs/Web/CSS/CSS_display/Containing_block)'s width.
- `auto`
- : The browser will calculate and selecta width for the specified element.
- `max-content`
- : The intrinsic preferred width.
- `min-content`
- : The intrinsic minimum width.
- `fit-content`
- : Use the available space, but not more than [max-content](/en-US/docs/Web/CSS/max-content), i.e., `min(max-content, max(min-content, stretch))`.
- `fit-content({{cssxref("<length-percentage>")}})`
- : Uses the fit-content formula with the available space replaced by the specified argument, i.e., `min(max-content, max(min-content, <length-percentage>))`.
- `stretch`
- : 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/CSS_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/box-sizing).
> [!NOTE]
> To check aliases used by browsers for the `stretch` value and its implementation status, see the [Browser compatibility](#browser_compatibility) section.
## Accessibility
Ensure that elements set with a `width` aren't truncated and/or don't obscure other content when the page is zoomed to increase text size.
- [MDN Understanding WCAG, Guideline 1.4 explanations](/en-US/docs/Web/Accessibility/Guides/Understanding_WCAG/Perceivable#guideline_1.4_make_it_easier_for_users_to_see_and_hear_content_including_separating_foreground_from_background)
- [Understanding Success Criterion 1.4.4 | W3C Understanding WCAG 2.0](https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-scale.html)
## Formal definition
{{CSSInfo}}
## Formal syntax
{{csssyntax}}
## Examples
### Default width
```css
p.goldie {
background: gold;
}
```
```html
<pclass="goldie">The MDN community writes really great documentation.</p>
```
{{EmbedLiveSample('Default_width', '500px', '64px')}}
### Example using pixels and ems
```css
.px_length {
width: 200px;
background-color: red;
color: white;
border: 1pxsolidblack;
}
.em_length {
width: 20em;
background-color: white;
color: red;
border: 1pxsolidblack;
}
```
```html
<divclass="px_length">Width measured in px</div>
<divclass="em_length">Width measured in em</div>
```
{{EmbedLiveSample('Example using pixels and ems', '500px', '64px')}}
### Example with percentage
```css
.percent {
width: 20%;
background-color: silver;
border: 1pxsolidred;
}
```
```html
<divclass="percent">Width in percentage</div>
```
{{EmbedLiveSample('Example using percentage', '500px', '64px')}}
### Example using "max-content"
```css
p.max-green {
background: lightgreen;
width: max-content;
}
```
```html
<pclass="max-green">The MDN community writes really great documentation.</p>
```
{{EmbedLiveSample('Example using "max-content"', '500px', '64px')}}
### Example using "min-content"
```css
p.min-blue {
background: lightblue;
width: min-content;
}
```
```html
<pclass="min-blue">The MDN community writes really great documentation.</p>
```
{{EmbedLiveSample('Example using "min-content"', '500px', '155px')}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{cssxref("height")}}
- {{cssxref("box-sizing")}}
- {{cssxref("min-width")}}, {{cssxref("max-width")}}
- {{cssxref("block-size")}}, {{cssxref("inline-size")}}
- {{cssxref("anchor-size()")}}
- SVG {{SVGAttr("width")}} attribute
-[Introduction to the CSS basic box model](/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model)
-[CSS box model](/en-US/docs/Web/CSS/CSS_box_model) module