Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

width — an element’s preferred width

The width property specifies an element box’s preferred physical width. It accepts lengths, percentages, and auto, among other values; the final size also depends on box sizing, constraints, and layout.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

/* Selected values from CSS Box Sizing Levels 3 and 4; not the full grammar. */
width: auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | stretch | contain;
/* CSS-wide keywords are also accepted. */

width sets the preferred width along the physical axis, independently of writing direction. Its initial value is auto, and it is not inherited. It does not apply to non-replaced inline elements.

Lengths and percentages must be nonnegative. A percentage refers to the containing block’s width, not necessarily the parent’s width. auto leaves the calculation to layout rules and does not simply mean 100%.

With box-sizing: content-box, the width describes the content; with box-sizing: border-box, it also includes padding and borders, but not margins. min-width and max-width constrain the result, and the layout algorithm may change the final size.

min-content and max-content refer to content-based sizes. fit-content accounts for available space, whereas fit-content() accepts an explicit limit. stretch fills available space using the margin box. The syntax describes the specification, not guaranteed support for every value in the target browser.

Examples

<!doctype html>
<meta charset="utf-8">
<title>width</title>
<style>
  .container { width: 400px; }
  .box {
    box-sizing: border-box;
    width: 50%;
    padding: 10px;
    border: 2px solid;
  }
</style>
<div class="container"><div class="box">width</div></div>
<pre id="result"></pre>
<script>
  const box = document.querySelector('.box');
  document.querySelector('#result').textContent =
    box.getBoundingClientRect().width + 'px';
</script>

Result

width
200px

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources