--- title: width slug: Web/CSS/width tags: - CSS - CSS Property - NeedsBrowserCompatibility - NeedsMobileBrowserCompatibility - Reference translation_of: Web/CSS/width ---
{{CSSRef}}
La propietat CSS width especifica l'amplada d'un element. Per defecte, estableix l'amplada de l'àrea de contingut. però si {{cssxref("box-sizing")}} està establert a border-box, aquest determina l'amplada de l'àrea de vora.
Les propietats {{cssxref("min-width")}} i {{cssxref("max-width")}} anul·la {{cssxref("width")}}.
/* <length> values */ width: 300px; width: 25em; /* <percentage> value */ width: 75%; /* Keyword values */ width: max-content; width: min-content; width: fit-content(20em); width: auto; /* Global values */ width: inherit; width: initial; width: unset;
La propietat width s'especifica com a:
min-content, max-content, fit-content, auto.<length> o una <percentage>.automax-contentmin-contentfit-content({{cssxref("<length-percentage>")}})min(max-content, max(min-content, <length-percentage>)).Ens hem d'assegurar que els elements establerts amb un width no estan truncats i que no enfosqueixin altres continguts quan la pàgina s'ampliï per augmentar la mida del text.
{{CSSInfo}}
p.goldie {
background: gold;
}
<p class="goldie">The Mozilla community produces a lot of great software.</p>
{{EmbedLiveSample('Default_width', '500px', '64px')}}
.px_length {
width: 200px;
background-color: red;
color: white;
border: 1px solid black;
}
.em_length {
width: 20em;
background-color: white;
color: red;
border: 1px solid black;
}
<div class="px_length">Width measured in px</div> <div class="em_length">Width measured in em</div>
{{EmbedLiveSample('Pixels_and_ems', '500px', '64px')}}
.percent {
width: 20%;
background-color: silver;
border: 1px solid red;
}
<div class="percent">Width in percentage</div>
{{EmbedLiveSample('Percentage', '500px', '64px')}}
p.maxgreen {
background: lightgreen;
width: intrinsic; /* Safari/WebKit uses a non-standard name */
width: -moz-max-content; /* Firefox/Gecko */
width: -webkit-max-content; /* Chrome */
width: max-content;
}
<p class="maxgreen">The Mozilla community produces a lot of great software.</p>
{{EmbedLiveSample('max-content', '500px', '64px')}}
p.minblue {
background: lightblue;
width: -moz-min-content; /* Firefox */
width: -webkit-min-content; /* Chrome */
}
<p class="minblue">The Mozilla community produces a lot of great software.</p>
{{EmbedLiveSample('min-content', '500px', '155px')}}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('CSS4 Sizing', '#width-height-keywords', 'width')}} | {{Spec2('CSS4 Sizing')}} | |
| {{SpecName('CSS3 Sizing', '#width-height-keywords', 'width')}} | {{Spec2('CSS3 Sizing')}} | Added the max-content, min-content, fit-content keywords. |
| {{SpecName('CSS2.1', 'visudet.html#the-width-property', 'width')}} | {{Spec2('CSS2.1')}} | Precises on which element it applies to. |
| {{SpecName('CSS1', '#width', 'width')}} | {{Spec2('CSS1')}} | Initial definition. |
{{Compat("css.properties.width")}}