docs.rodeo

MDN Web Docs mirror

width

{{CSSRef}} 

The width CSS property sets an element’s width. By default, it sets the width of the content area, but if {{cssxref("box-sizing")}}  is set to border-box, it sets the width of the border area.

{{EmbedInteractiveExample("pages/css/width.html")}} 

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")}} .

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

/* <length> values */
width: 300px;
width: 25em;
width: anchor-size(--myAnchor inline, 120%);
width: minmax(100px, anchor-size(width));

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

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.

Formal definition

{{CSSInfo}} 

Formal syntax

{{csssyntax}} 

Examples

Default width

p.goldie {
  background: gold;
}
<p class="goldie">The MDN community writes really great documentation.</p>

{{EmbedLiveSample('Default_width', '500px', '64px')}} 

Example using pixels and ems

.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('Example using pixels and ems', '500px', '64px')}} 

Example with percentage

.percent {
  width: 20%;
  background-color: silver;
  border: 1px solid red;
}
<div class="percent">Width in percentage</div>

{{EmbedLiveSample('Example using percentage', '500px', '64px')}} 

Example using “max-content”

p.max-green {
  background: lightgreen;
  width: max-content;
}
<p class="max-green">The MDN community writes really great documentation.</p>

{{EmbedLiveSample('Example using "max-content"', '500px', '64px')}} 

Example using “min-content”

p.min-blue {
  background: lightblue;
  width: min-content;
}
<p class="min-blue">The MDN community writes really great documentation.</p>

{{EmbedLiveSample('Example using "min-content"', '500px', '155px')}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN