CSS width Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The width property sets an element’s horizontal size. By default it sizes the content area. With box-sizing: border-box, it sizes the border box (content + padding + border).

It is not inherited. The initial value is auto. min-width and max-width can override it when the preferred width is too small or too large.

Baseline · Widely available.

Not deprecated. Length, percentage, and auto work in all major browsers. Intrinsic keywords have strong modern support.

Limited / newer:

stretch, anchor-size(), and calc-size() are newer sizing features. Prefer length, %, auto, and *-content for everyday work.

CSS Syntax

syntax.css
width: auto | <length> | <percentage> | max-content | min-content | fit-content;

Example: width: 300px;

Property Values

ValueDescriptionTry it
autoBrowser picks the width. Initial valueTry it
<length>Fixed size such as 200px or 20emTry it
<percentage>Percent of the containing block’s widthTry it
max-contentAs wide as the content prefers (often one long line)Try it
min-contentAs narrow as possible without overflow (often widest word)Try it
fit-contentUse available space, capped near max-contentTry it
initialResets to autoTry it
inheritTakes the parent’s widthTry it

How to Use width

Pick a sizing method, then check how padding and limits interact.

  1. Choose length (px/em), percentage, or a keyword (auto, max-content, …).
  2. Set width on the box you want to size.
  3. Decide box-sizing: content-box (default) vs border-box.
  4. Optional: add min-width / max-width so layouts stay readable on small and large screens.
PieceRequired?Role
Target boxYesBlock or replaced element to size
Size valueYesLength, %, or keyword
box-sizing—Whether padding/border count inside width

Live Preview

Change width on the blue box and watch how much horizontal space it takes.

Percentages use the parent track. Intrinsic keywords size from the text itself.

Sample
Sample box

width: 120px

CSS Examples

Each snippet focuses on width. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Fixed Pixel Width

Set an exact horizontal size with a length.

CSS
<style>
  .box {
    width: 200px;
  }
</style>

<div class="box">200px wide</div>
Try It Yourself

How It Works

  1. Pick a length. 200px is a fixed distance on screen.
  2. Apply to the box. The div becomes 200px wide (content area by default).
  3. Independent of parent. Parent size does not change this fixed value.
  4. Try em. 20em would scale with the element’s font size.

Example 2 — Percentage of Parent

Size relative to the containing block’s width.

CSS
<style>
  .box {
    width: 50%;
  }
</style>

<div class="box">Half of parent</div>
Try It Yourself

How It Works

  1. Percent of parent. 50% means half the containing block width.
  2. Responsive. When the parent shrinks, the child shrinks with it.
  3. Needs a defined parent. If the parent’s width is unclear, results can surprise you.
  4. Not inherited. Children do not automatically copy this percentage.

Example 3 — Fit Preferred Content Width

Let the box grow as wide as the text prefers.

CSS
<style>
  .box {
    width: max-content;
  }
</style>

<div class="box">As wide as this sentence prefers.</div>
Try It Yourself

How It Works

  1. Intrinsic size. max-content asks for the preferred content width.
  2. Often one line. Text may stay on a single line if that is preferred.
  3. Compare min-content. min-content shrinks toward the widest unbreakable piece (often a word).
  4. Modern browsers. Safe for current engines; not needed for basic pixel layouts.

Edge Cases & Pitfalls

If a box is wider or narrower than expected, check these first.

box

Padding changes total size

With default content-box, padding and border add outside width. Use border-box to include them.

limits

min/max can win

If width is below min-width or above max-width, those limits override.

inline

Non-replaced inlines ignore width

width does not apply to normal inline text spans. Use block, inline-block, flex items, or replaced elements.

a11y

Fixed widths and zoom

Very small fixed widths can clip text when users zoom. Prefer fluid sizes for body content.

logical

Prefer inline-size for writing modes

inline-size is the logical twin of width and follows writing mode.

Browser Support

width is a browser CSS property for horizontal sizing. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

width

Length, percentage, and auto are supported everywhere. max-content, min-content, and fit-content are widely supported in modern browsers.

98% Global support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 4+
Full support
Samsung Internet 1+
Full support
Internet Explorer 4+
Core values
width Widely supported

Bottom line: Safe for everyday sizing. Treat stretch, anchor-size(), and calc-size() as progressive enhancements. This is not a JavaScript runtime API.

Key Takeaways

  • Role: set how wide a box is (content area by default).
  • Default: auto lets the browser choose.
  • box-sizing decides if padding/border sit inside that width.
  • Not inherited. Use % of the containing block when you need relative sizing.

One line: width sets a box’s horizontal size; pair it with box-sizing and min/max limits when needed.

Frequently Asked Questions

It sets the horizontal size of an element’s box. By default that is the content area; with box-sizing: border-box it includes padding and border.
The initial value is auto. The browser picks a width from content, display type, and available space.
width is the preferred size. max-width caps how large the box can grow, which is useful for responsive layouts.
With the default content-box, no. With box-sizing: border-box, yes — width covers content, padding, and border.
No. Percentage widths are resolved against the containing block’s width.
It is Baseline Widely available. Length, percentage, and auto work everywhere. Intrinsic keywords like max-content need modern browsers. It is a browser CSS property, not a Node.js feature.

Next: widows

Control the minimum number of lines left at the top of a new page or column.

widows tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful