CSS width Property

The CSS width property sets the preferred width of an element using lengths, percentages, intrinsic sizing keywords, or other supported sizing values.

By default, width determines the width of an element's content box. When box-sizing: border-box is applied, the specified width includes padding and borders. The property works with min-width, max-width, and layout rules to control how elements respond to available space.

width Syntax

The width property accepts the auto keyword, non-negative lengths and percentages, intrinsic sizing keywords, and selected sizing functions.

Basic Syntax

.element {
  width: 300px;
}

Common Values

.element {
  width: auto;
}
.element {
  width: 250px;
}
.element {
  width: 75%;
}
.element {
  width: 20rem;
}
.element {
  width: 50vw;
}
.element {
  width: min-content;
}
.element {
  width: max-content;
}
.element {
  width: fit-content;
}
.element {
  width: fit-content(300px);
}
.element {
  width: stretch;
}

Calculated Widths

.element {
  width: calc(100% - 40px);
}
.element {
  width: min(100%, 600px);
}
.element {
  width: clamp(200px, 50vw, 600px);
}

Anchor-Based Width

.element {
  width: anchor-size(width);
}

Anchor-based sizing is a newer feature that requires a valid anchor relationship and browser support.

CSS-Wide Keywords

.element {
  width: initial;
}
.element {
  width: inherit;
}
.element {
  width: unset;
}
.element {
  width: revert;
}
.element {
  width: revert-layer;
}

Invalid Values

.element {
  width: -100px;
}
.element {
  width: 50;
}
.element {
  width: none;
}
.element {
  width: 20deg;
}

Negative lengths, nonzero unitless numbers, none, and angle units are not valid width values.

Property Values

Value Description
auto Allows the layout algorithm to determine the width. This is the initial value.
<length> Specifies a width using units such as px, em, rem, or viewport units.
<percentage> Specifies a width relative to the containing block's width, subject to layout-specific rules.
min-content Uses the smallest intrinsic width that the content can occupy without avoidable overflow.
max-content Uses the content's preferred intrinsic width without soft wrapping.
fit-content Uses an intrinsic size constrained by the available space.
fit-content(<length-percentage>) Uses a specified limit in the intrinsic sizing calculation.
stretch Attempts to make the element's margin box fill the available width.
anchor-size() Derives a size from an anchor element using CSS Anchor Positioning.
calc() Calculates a width from compatible numeric values.
min() Selects the smallest value from compatible sizing expressions.
max() Selects the largest value from compatible sizing expressions.
clamp() Constrains a preferred width between a minimum and maximum.

Property Characteristics

Characteristic Value
Initial value auto
Inherited No
Applies to Elements except non-replaced inline elements, table rows, and row groups
Computed value Specified keyword, percentage, or computed length as applicable
Animation type Length, percentage, or calculated value where interpolation is supported
Percentages Relative to the containing block's width
Negative values Not allowed
Specification CSS Box Sizing Module Level 3 and Level 4

Note: The final rendered width may differ from the declared value because of min-width, max-width, box sizing, layout constraints, and the element's display type.

How It Works

The width property provides a preferred horizontal size for an element. The browser combines this value with the box model and the element's layout context to calculate its final dimensions.

Fixed Width

.box {
  width: 300px;
}

The content box is 300 pixels wide when the default content-box sizing model applies and no other constraints change the result.

Percentage Width

.box {
  width: 50%;
}

The box uses half the width of its containing block when that percentage can be resolved.

Automatic Width

.box {
  width: auto;
}

The browser determines the width according to the layout rules for the element.

The Display Type Matters

A normal block-level element behaves differently from an inline element, flex item, grid item, or absolutely positioned element.

Width Is Not Always the Final Size

The declared width may be limited by minimum and maximum constraints or adjusted by a layout algorithm.

Content Overflow

Setting a width does not guarantee that all content fits inside it. Long text, images, and other content can overflow unless additional rules handle the available space.

The auto Value

The auto value lets the browser determine an element's width according to its formatting context and available space.

Syntax

.element {
  width: auto;
}

Block Elements

A normal block-level element with automatic width generally fills the available inline space of its containing block after accounting for margins, borders, and padding.

.content {
  width: auto;
  padding: 20px;
}

Inline Elements

For ordinary non-replaced inline elements, the width property does not set their rendered width. Their size follows inline formatting rules.

Flex and Grid Items

Automatic width can depend on flex sizing, grid tracks, intrinsic content dimensions, and alignment.

Images

img {
  width: auto;
  height: auto;
}

Images with intrinsic dimensions normally use their natural sizing behavior when no other constraints apply.

When to Use It

Use auto when the layout system should determine an element's width rather than enforcing a specific measurement.

Length Values

Length values define widths using absolute or relative CSS units.

Pixels

.box {
  width: 300px;
}

Pixels provide a fixed CSS measurement that does not automatically scale with the containing block.

Relative Font Units

.box {
  width: 20em;
}
.card {
  width: 25rem;
}

The em unit is relative to the element's font size, while rem is relative to the root element's font size.

Viewport Units

.panel {
  width: 50vw;
}

The vw unit is relative to the viewport width.

Dynamic Viewport Units

.panel {
  width: 50dvw;
}

Dynamic viewport units reflect the dynamic viewport size. They can be useful in layouts affected by browser interface changes.

Container Query Units

.component {
  width: 80cqw;
}

The cqw unit is based on the query container's width, with fallback sizing rules when no eligible query container exists.

Choosing a Unit

Use pixels for precise measurements, font-relative units for typography-related sizing, and percentages or container-relative units for flexible layouts.

Percentage Values

A percentage width is generally calculated relative to the width of the element's containing block.

Syntax

.element {
  width: 75%;
}

Parent and Child Example

<div class="parent">
  <div class="child">Child element</div>
</div>
.parent {
  width: 400px;
}
.child {
  width: 50%;
}

The child's content box is 200 pixels wide when the containing block is 400 pixels wide and no other constraints intervene.

Full Width

.element {
  width: 100%;
}

The content box fills the containing block's width. With the default box sizing, padding and borders are added outside that width.

Avoiding Overflow

.element {
  width: 100%;
  box-sizing: border-box;
}

This includes padding and borders within the specified width.

Percentage Resolution

In some layout situations, a percentage cannot be resolved immediately because the containing block's size depends on its content. The browser follows the applicable sizing rules to determine the result.

When to Use It

Percentage widths are useful for flexible content regions, form controls, and components that should scale with their containers.

Intrinsic Sizing

Intrinsic sizing keywords calculate an element's width from its content rather than using a fixed measurement.

The min-content Value

.element {
  width: min-content;
}

The browser uses the smallest intrinsic width that the content can occupy, based on available wrapping opportunities.

The max-content Value

.element {
  width: max-content;
}

The browser uses the content's maximum intrinsic width, generally without soft wrapping.

Comparison

Value Behavior
min-content Shrinks to the smallest intrinsic content width.
max-content Expands to the maximum intrinsic content width.
fit-content Uses an intrinsic width constrained by available space.

Example

.label {
  width: max-content;
}

This can make a label expand to accommodate its content without ordinary soft wrapping.

Possible Overflow

The max-content value can produce an element wider than its container.

When to Use It

Intrinsic sizing is useful for content-based components, labels, menus, and layouts that should adapt to the natural dimensions of their content.

The fit-content Values

The fit-content keyword and fit-content() function both constrain intrinsic sizing, but they are not interchangeable in every context.

fit-content Keyword

.element {
  width: fit-content;
}

The browser uses a content-based width constrained by the available space.

fit-content() Function

.element {
  width: fit-content(300px);
}

The function provides a specific length or percentage limit for the intrinsic sizing calculation.

Percentage Limit

.element {
  width: fit-content(80%);
}

Intrinsic Sizing Formula

Conceptually, the fit-content sizing calculation clamps the available or specified limit between the element's minimum-content and maximum-content sizes.

Practical Example

.notice {
  width: fit-content;
  max-width: 100%;
  padding: 15px;
  box-sizing: border-box;
}

The notice can size itself to its content while respecting the container's maximum width.

Browser Compatibility

The fit-content keyword is broadly supported in modern browsers. The function form has different compatibility requirements and should be tested separately.

The stretch Value

The stretch value attempts to size an element so its margin box fills the available width of its containing block.

Syntax

.element {
  width: stretch;
}

Margin Box Sizing

Unlike width: 100%, which normally sizes the content box, stretch uses the margin box when calculating the available width.

Example

.panel {
  width: stretch;
  margin: 10px;
  padding: 20px;
  border: 1px solid #ccc;
}

The panel attempts to fill the available width while accounting for its margins, padding, and borders.

Fallback

.panel {
  width: auto;
  width: stretch;
}

Browsers that do not recognize stretch retain the preceding supported declaration.

Browser Support

Support for the unprefixed stretch value varies across browser versions. Check compatibility before relying on it.

Anchor-Based Sizing

The anchor-size() function allows an element to derive its width from an anchor element.

Basic Syntax

.popover {
  width: anchor-size(width);
}

Named Anchor Example

.trigger {
  anchor-name: --menu-anchor;
}
.menu {
  position: absolute;
  position-anchor: --menu-anchor;
  width: anchor-size(width);
}

The menu can use the anchor element's width when the anchor relationship is valid and the browser supports anchor sizing.

Fallback Size

.menu {
  width: anchor-size(width, 250px);
}

The fallback value can be used when the requested anchor size cannot be resolved.

Practical Uses

Anchor-based sizing can help align popovers, menus, and floating interface elements with their associated controls.

Browser Support

CSS Anchor Positioning is a newer feature. Use a conventional width fallback and test compatibility before relying on anchor-based sizing.

Width and the Box Model

The box-sizing property determines whether the specified width applies to the content box or includes padding and borders.

content-box

.box {
  box-sizing: content-box;
  width: 300px;
  padding: 20px;
  border: 5px solid #0066cc;
}

The content box is 300 pixels wide. With 20 pixels of padding and a 5-pixel border on each side, the outer border-box width becomes 350 pixels.

border-box

.box {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 5px solid #0066cc;
}

The total border-box width is 300 pixels. The content area is reduced to accommodate padding and borders.

Comparison

Measurement content-box border-box
Declared width 300px 300px
Content width 300px 250px
Horizontal padding 40px 40px
Horizontal borders 10px 10px
Total border-box width 350px 300px

Global Box Sizing

*, *::before, *::after {
  box-sizing: border-box;
}

This common CSS rule makes declared widths easier to manage because padding and borders are included within the specified dimensions.

Margins

Margins remain outside the border box and are not included in the specified width under either sizing model.

Minimum and Maximum Width

The min-width and max-width properties constrain the width calculated from width.

Minimum Width

.element {
  width: 50%;
  min-width: 200px;
}

The element is prevented from becoming narrower than 200 pixels, even when 50% of the containing block is smaller.

Maximum Width

.element {
  width: 100%;
  max-width: 600px;
}

The element can fill a narrow container but does not exceed 600 pixels in width.

Combined Constraints

.element {
  width: 50%;
  min-width: 200px;
  max-width: 600px;
}

Priority of Constraints

If the calculated width is smaller than min-width, the minimum applies. If it exceeds max-width, the maximum applies. When the minimum exceeds the maximum, the minimum takes precedence.

Responsive Content Container

.content {
  width: 100%;
  max-width: 960px;
  margin-inline: auto;
  box-sizing: border-box;
}

This creates a centered container that can shrink with the available space.

Images

img {
  max-width: 100%;
  height: auto;
}

This common pattern allows images to shrink when necessary without forcing smaller images to expand.

Width in Different Layouts

The effect of width depends on the element's formatting context.

Block Elements

.block {
  display: block;
  width: 300px;
}

A block element can use the specified width while participating in normal block layout.

Inline Elements

.inline {
  display: inline;
  width: 300px;
}

The width declaration does not apply to ordinary non-replaced inline elements such as a typical <span>.

Inline-Block Elements

.inline-block {
  display: inline-block;
  width: 200px;
}

Inline-block elements accept width while remaining in inline formatting flow.

Flex Items

.flex-container {
  display: flex;
}
.flex-item {
  width: 200px;
}

Flex items can use the specified width as part of their sizing calculation. Flex shrinking, growing, and flex-basis can change the final result.

Grid Items

.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}
.grid-item {
  width: 80%;
}

Grid items are sized within their grid areas. Width and alignment rules can affect whether an item stretches or occupies only part of its assigned area.

Absolutely Positioned Elements

.positioned {
  position: absolute;
  width: 250px;
}

The element's final horizontal position and dimensions depend on its containing block and inset properties.

Replaced Elements

Images and other replaced elements can use intrinsic dimensions, aspect ratios, and explicitly specified widths.

Responsive Widths

Responsive layouts use flexible widths and constraints so elements adapt to different screen and container sizes.

Fluid Width

.container {
  width: 90%;
}

Fluid Width With a Maximum

.container {
  width: 90%;
  max-width: 1200px;
  margin-inline: auto;
}

Using min()

.container {
  width: min(90%, 1200px);
  margin-inline: auto;
}

The element uses whichever value is smaller.

Using clamp()

.panel {
  width: clamp(200px, 50vw, 600px);
}

The preferred width is 50% of the viewport width, constrained between 200 and 600 pixels.

Using calc()

.container {
  width: calc(100% - 40px);
}

This subtracts 40 pixels from the containing block's available width.

Media Queries

.sidebar {
  width: 280px;
}
@media (max-width: 768px) {
  .sidebar {
    width: 100%;
  }
}

Container Queries

.component-wrapper {
  container-type: inline-size;
}
.component {
  width: 100%;
}
@container (min-width: 600px) {
  .component {
    width: 50%;
  }
}

Avoiding Unnecessary Minimums

Be careful with large fixed minimum widths. They can prevent content from fitting on narrow screens.

Practical Recommendation

For general page containers, a flexible width combined with max-width is often simpler and more reliable than a fixed width.

Inheritance

The width property is not inherited. Child elements calculate their widths independently unless inheritance is explicitly requested.

Parent and Child

.parent {
  width: 400px;
}
.child {
  width: auto;
}

The child does not inherit the parent's declared width. Its width is calculated according to its own layout rules.

Explicit Inheritance

.child {
  width: inherit;
}

This makes the child inherit the parent's computed width value.

Percentage Width Is Not Inheritance

.parent {
  width: 400px;
}
.child {
  width: 50%;
}

The child uses a percentage of its containing block rather than inheriting the parent's width declaration.

Restore the Initial Value

.element {
  width: initial;
}

This restores auto.

Using unset

.element {
  width: unset;
}

Because width is not inherited, unset behaves like initial.

Practical Examples

Fixed-Width Card

.card {
  width: 300px;
  box-sizing: border-box;
}

Responsive Card

.card {
  width: 100%;
  max-width: 350px;
  box-sizing: border-box;
}

Centered Content Container

.content {
  width: min(90%, 1100px);
  margin-inline: auto;
}

Full-Width Form Control

.form-control {
  width: 100%;
  box-sizing: border-box;
}

Content-Sized Button

.button {
  width: fit-content;
  padding: 10px 20px;
}

Responsive Image

.responsive-image {
  max-width: 100%;
  height: auto;
}

Flexible Sidebar

.sidebar {
  width: 25%;
  min-width: 220px;
  max-width: 320px;
}

Width Based on the Viewport

.panel {
  width: 60vw;
  max-width: 800px;
}

Calculated Width

.panel {
  width: calc(100% - 32px);
}

Fluid Width With Limits

.panel {
  width: clamp(200px, 70%, 700px);
}

width Example

This interactive example demonstrates how different width values affect an element inside a fixed-width demonstration container.

Select a width value to compare fixed, percentage, automatic, and intrinsic sizing behavior.

HTML Example

<div class="width-demo">
  <label for="width-select">Choose a width value:</label>
  <select id="width-select">
    <option value="auto">auto</option>
    <option value="150px">150px</option>
    <option value="50%">50%</option>
    <option value="75%">75%</option>
    <option value="100%">100%</option>
    <option value="min-content">min-content</option>
    <option value="max-content">max-content</option>
    <option value="fit-content">fit-content</option>
  </select>
  <div class="width-container">
    <div class="width-box">Responsive CSS Width Example</div>
  </div>
</div>

CSS Example

.width-demo label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
}
.width-demo select {
  margin-bottom: 15px;
  padding: 8px;
}
.width-container {
  width: 100%;
  max-width: 400px;
  min-height: 110px;
  padding: 10px;
  border: 2px dashed #999;
  box-sizing: border-box;
}
.width-box {
  width: auto;
  padding: 15px;
  background-color: #e5eef7;
  border: 2px solid #0066cc;
  box-sizing: border-box;
}

JavaScript Example

const select = document.querySelector("#width-select");
const box = document.querySelector(".width-box");
select.addEventListener("change", () => {
  box.style.width = select.value;
});
Browser Result
Responsive CSS Width Example

Understanding the Results

The dashed container represents the available width. The blue box changes size according to the selected value.

Selecting auto

The block fills the available width inside its container.

Selecting 150px

The box uses a fixed border-box width of 150 pixels.

Selecting 50%

The box occupies half the width of its containing block.

Selecting 75%

The box occupies three-quarters of its containing block's width.

Selecting 100%

The box fills the containing block's width. Because it uses border-box, its padding and borders are included.

Selecting min-content

The box shrinks toward the smallest intrinsic width of its text, wrapping at available opportunities.

Selecting max-content

The box expands toward the text's maximum intrinsic width and may overflow the container.

Selecting fit-content

The box uses a content-based width constrained by the available space.

Experiment With Box Sizing

.width-box {
  box-sizing: content-box;
}

Changing to content-box makes padding and borders add to the declared width.

Accessibility Considerations

The width property affects layout dimensions and can influence how easily users read and interact with content at different screen sizes and zoom levels.

Support Text Reflow

Avoid fixed widths that force users to scroll horizontally when text is enlarged or the viewport becomes narrow.

Use Flexible Containers

.content {
  width: 100%;
  max-width: 900px;
  box-sizing: border-box;
}

Maintain Readable Line Lengths

Very wide text blocks can be difficult to read. Use max-width to maintain comfortable line lengths.

Avoid Clipped Content

Fixed-width controls and containers should accommodate translated text, larger fonts, and longer labels.

Responsive Images

Use flexible image sizing to prevent images from extending beyond narrow containers.

Interactive Controls

Ensure that buttons, links, and form fields remain usable when their surrounding layout changes.

Zoom Testing

Test layouts at increased browser zoom and reduced viewport widths to identify overflow or hidden content.

Semantic HTML

Width changes presentation only. Use appropriate HTML elements to communicate document structure and meaning.

Common Mistakes

Using a Nonzero Number Without a Unit

.element {
  width: 300;
}

Use a valid unit such as 300px. A unitless zero is allowed, but nonzero lengths require units unless expressed through a valid calculation that produces a length.

Using Negative Widths

.element {
  width: -100px;
}

Negative width values are invalid.

Expecting Width to Apply to Ordinary Inline Elements

The property does not set the width of non-replaced inline elements such as a typical <span>.

Forgetting Padding and Borders

With content-box, padding and borders increase the total rendered width.

Assuming width: 100% Always Fits

Padding, borders, margins, and minimum sizing constraints can cause overflow.

Confusing width and max-width

The width property sets a preferred width. The max-width property sets an upper constraint.

Ignoring Flexbox Sizing

Flex items may shrink or grow according to flex layout rules even when a width is specified.

Ignoring Grid Track Sizing

Grid items are affected by their assigned tracks, intrinsic sizes, and alignment rules.

Using Large Fixed Widths on Small Screens

Fixed widths can produce horizontal scrolling or clipped content.

Assuming max-content Fits the Container

The max-content value can produce an element wider than the available space.

Ignoring Minimum Width Constraints

A minimum width can prevent an element from shrinking even when its declared width is smaller.

Assuming New Sizing Values Have Universal Support

Features such as stretch, fit-content(), and anchor-size() require compatibility checks.

Important Considerations

Keep the following points in mind when using width:

  • The property sets an element's preferred width.
  • Its initial value is auto.
  • The property is not inherited.
  • Negative width values are invalid.
  • Nonzero length values generally require valid CSS units.
  • The property does not apply to ordinary non-replaced inline elements.
  • The auto value is resolved according to the element's layout context.
  • Percentage widths generally depend on the containing block.
  • Length values can use absolute, font-relative, viewport-relative, and container-relative units.
  • The min-content value uses the minimum intrinsic content width.
  • The max-content value uses the maximum intrinsic content width.
  • The fit-content keyword provides constrained intrinsic sizing.
  • The fit-content() function accepts a length or percentage limit.
  • The stretch value sizes the margin box toward the available width.
  • The anchor-size() function can derive dimensions from an anchor element.
  • The calc(), min(), max(), and clamp() functions support calculated sizing.
  • The default content-box model excludes padding and borders from the declared width.
  • The border-box model includes padding and borders within the declared width.
  • Margins remain outside the border box.
  • The min-width and max-width properties can override the preferred width.
  • Flexbox and Grid can influence the final rendered width.
  • Intrinsic sizing can produce overflow when content is wider than its container.
  • Flexible widths and maximum constraints are useful for responsive layouts.
  • Use max-width: 100% for images that should shrink within their containers.
  • Test layouts with long content, increased text sizes, and narrow viewports.
  • Check compatibility when using newer sizing keywords and functions.

Browser Support

The CSS width property is widely available across modern browsers and has been supported for many years.

Basic length, percentage, and auto values have broad compatibility. Intrinsic sizing keywords and newer features such as stretch, fit-content(), and anchor-size() have different support requirements.

For current compatibility information, visit Can I Use? and search for the specific sizing feature.

Standard Declaration

.element {
  width: 300px;
}

Responsive Declaration

.element {
  width: 100%;
  max-width: 600px;
  box-sizing: border-box;
}

Feature Detection

@supports (width: stretch) {
  .element {
    width: stretch;
  }
}

Fallback Example

.element {
  width: 100%;
}
@supports (width: stretch) {
  .element {
    width: stretch;
  }
}

Anchor Sizing Detection

@supports (width: anchor-size(width)) {
  .popover {
    width: anchor-size(width);
  }
}

Feature detection checks whether the browser recognizes the syntax. Anchor sizing still requires a valid anchor relationship to produce the intended result.

Vendor Prefixes

The standard width property does not require vendor prefixes. Some newer sizing values have had prefixed implementations in certain browsers.

Testing Recommendations

Test percentage widths, intrinsic sizing, box sizing, Flexbox and Grid behavior, and newer sizing functions across the browsers you intend to support.

Specifications

The width property is defined in CSS Box Sizing Module Level 3, with additional sizing features described in CSS Box Sizing Module Level 4.

CSS Box Sizing Module Level 3 - Preferred Size Properties

CSS Box Sizing Module Level 4

These specifications define preferred sizing, intrinsic dimensions, and newer sizing mechanisms.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: width