CSS Portal

CSS width Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The width property controls the horizontal size an element takes in layout — it determines the size of the element’s content box (subject to the box model) and therefore affects how much inline space the element occupies and how its children are laid out. In normal flow block-level elements the declared horizontal size is a primary driver of reflow: it influences line wrapping of text, the calculation of percentage-based sizes for descendants, and how neighboring elements flow around or alongside the box. Because an element’s used width is one of the fundamental layout inputs, changes to it typically trigger reflow and repaint of the document.

How that horizontal value is interpreted depends on the box model and surrounding properties. The interplay with the box-sizing model determines whether padding and border are counted inside the specified box or added outside it; the visual outer size is then influenced by padding, border, and outer spacing such as margin. Percentages and relative measurements are resolved against the containing block, so changing an ancestor’s size cascades into new used widths for descendants. Because margins can collapse or shift in block formatting contexts, the way outer spacing interacts with available width can be non‑trivial.

Width never acts in isolation — constraints and overflow rules can alter the final rendered result. Specified constraints from min-width and max-width will clamp the computed width, and when content does not fit the established width the element’s overflow handling (controlled by overflow) decides whether content is clipped, scrollable, or allowed to expand other layout factors. Intrinsic sizing and aspect relationships (for replaced elements and elements with intrinsic ratios) can also affect the used width; the aspect-ratio concept is often used to preserve proportions when only one dimension is constrained.

Different layout contexts compute and use width differently. Inline-level layout, floats and absolutely positioned elements follow different sizing rules: floats use a shrink-to-fit calculation and are affected by the element’s float behavior, absolutely positioned elements are taken out of normal flow and interact with offsets when position is non‑static, and flex or grid items participate in their container’s distribution algorithms (with flex items often influenced by flex-basis). For replaced elements like images and videos, intrinsic dimensions and available space (including the element’s declared width and computed height) combine to determine the final painted size. Understanding how width interacts with these contexts is essential for predictable, robust layout.

Definition

Initial value
auto
Applies to
All elements except non-replaced inline elements, table rows, and row groups
Inherited
No
Computed value
Percentage or absolute length
Animatable
Yes
JavaScript syntax
object.style.width

Interactive Demo

Example of the width css property

Syntax

width: <length> | <percentage> | auto

Values

  • <length>Specifies the width of the content area using a length unit.
  • <percentage>Integer, followed by a %. The value is a percentage of the width of the parent object, whether or not it is specified explicitly. Negative values are not allowed.
  • autoThe width depends on the values of other properties. See the sections below.
  • border-boxIf border-box is used, the length or percentage defined will be applied to the element's border box.
  • content-boxIf content-box is used, the length or percentage defined will be applied to the element's content-box.
  • autoIf auto is set for the elements width, the browser will determine the width for the element.
  • max-contentThe intrinsic preferred width
  • min-contentThe intrinsic minimum width
  • availableThe containing block width minus horizontal margin, border, and padding
  • fit-contentThis will be either the large of the minimum width or the smaller of the preferred width and the available width

Example

<div class="container">
<h1>CSS width property - examples</h1>
<div class="box fixed">Fixed width (200px)</div>
<div class="box percent">Percentage width (50%)</div>
<div class="box auto">Auto width (size to content)</div>
<div class="box minmax">Min-width 150px, max-width 300px (responsive)</div>
</div>
/* Basic page styling */
body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
    background: #f5f7fa;
    padding: 24px;
    color: #222;
}

/* Container to demonstrate percentage-based width */
.container {
    max-width: 900px;
    margin: 0 auto;
    background: #ffffff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

/* Common box styles */
.box {
    display: block;
    padding: 12px 16px;
    margin: 12px 0;
    background: #eef6ff;
    border: 1px solid #d0e6ff;
    border-radius: 6px;
}

/* Examples of width property */
.fixed {
    width: 200px;
}

.percent {
    width: 50%;
}

.auto {
    display: inline-block; /* width:auto on block-level would fill the container; inline-block lets it size to content */
    width: auto;
}

.minmax {
    width: 60%;
    min-width: 150px;
    max-width: 300px;
}

Browser Support

The following information will show you the current browser support for the CSS width property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!