Width attribute

HTML

The width attribute is an HTML attribute that specifies the intended display width of an element, most commonly images, videos, iframes and canvases. For images, pairing width with height allows browsers to infer an aspect ratio before the file is downloaded, reserving space and reducing layout shifts. The attribute value is a non-negative integer interpreted as CSS pixels, and it may be overridden by CSS. In modern responsive design, the width attribute is used primarily to establish intrinsic size and aspect ratio, not to hard-code the final rendered width.

Definition and scope

In HTML, the width attribute provides a numeric hint about how wide an element should render in CSS pixels. On img, video, iframe and canvas, it affects the element’s default layout size if no CSS overrides exist. When width and height are both present on an img, the browser derives an intrinsic aspect ratio immediately during layout, even before the image resource is fetched. This early knowledge reduces the risk of reflow and unexpected shifts as assets load.

The attribute’s scope is limited to layout hints. It does not control which resource gets downloaded in a responsive images setup; selection is governed by srcset and sizes. Nor does the attribute change the actual pixel dimensions of the file on the server. CSS can always override the rendered CSS width (for example, width: 100%), while the attribute remains valuable for establishing intrinsic size and aspect ratio.

Definitions

Attribute width vs CSS width

Attribute width is an HTML attribute on elements like img (e.g. width="1600") expressed as an integer in CSS pixels. CSS width is a property applied via stylesheets or inline styles, supporting units, percentages and fluid values. When both are present, CSS width wins for the rendered size, but the attribute still contributes to intrinsic aspect ratio calculations when paired with height.

Intrinsic vs rendered width

Intrinsic width refers to the natural dimensions of an image resource (its pixel width). Rendered width is how many CSS pixels it occupies in the layout after CSS is applied and device pixel ratio is considered. Best practice is to set width and height attributes to the intrinsic dimensions so browsers can calculate the aspect ratio early, while CSS (e.g. max-width: 100%; height: auto) makes the element responsive to its container.

Why it matters: Unexpected layout shifts are a major source of poor user experience measured by the Core Web Vitals metric Cumulative Layout Shift (CLS). “Good” CLS is <0.1. Images without reserved space commonly cause shifts when they load.

When images load without reserved space, text and buttons move, harming perceived stability and increasing bounce risk. CLS quantifies this instability; Google considers a page “Good” when the CLS score is below 0.1. Declaring width and height attributes on img elements (or using CSS aspect-ratio) allows the browser to allocate the correct space before the image is decoded, reducing or eliminating the layout shift contribution from those images.

Even with lazy loading and responsive containers, the attributes remain beneficial. They help ensure that placeholders reserve the correct aspect ratio across breakpoints, improving both perceived stability and the reliability of skeletons or blurred previews. The improvement is most evident for above-the-fold images, banners and hero sections that otherwise cause large shifts as they appear.

Role in responsive images:

In a responsive images setup, the width attribute does not decide which image file is fetched; that role belongs to srcset descriptors (x or w) combined with sizes. The attribute’s main value is to provide a stable aspect ratio. A common pattern is width and height attributes set to the intrinsic dimensions of the default source, while CSS constrains the element with max-width: 100% and height: auto so it scales fluidly to its container.

When using w-descriptors in srcset (e.g. 400w, 800w, 1200w), the browser chooses the most appropriate candidate based on the sizes attribute and device pixel ratio. This selection is independent of the width attribute. However, declaring both width and height ensures that, whichever candidate is selected, the page layout remains stable because the browser already knows the element’s intended aspect ratio before the image data arrives.

Common errors and constraints,

The width attribute accepts only non-negative integers and must not include units (e.g. width="600", not width="600px"). A frequent mistake is omitting height; without both attributes or a CSS aspect-ratio, the browser may not reserve space early enough, risking CLS. Another common pitfall is setting attribute values that do not match the intrinsic dimensions, leading to distortion or cumulative layout adjustment when CSS corrects the display.

CMS pipelines and CDN transforms can also introduce mismatches. If an image is transformed to a new aspect ratio but templates still emit the original width and height, the reserved space will be wrong. Similarly, relying on width alone while allowing CSS to auto-calculate height can work for responsiveness, but it will not establish an aspect ratio unless the attribute pair or CSS aspect-ratio is present.

  • Units are invalid: use width="800", not width="800px".
  • Missing height undermines aspect ratio reservation and can contribute to CLS.
  • Mismatched attribute values vs actual image size cause distortion or layout correction.
  • Do not confuse attribute width with srcset w-descriptors; they serve different purposes.

Implementation notes

For img elements, set width and height attributes to the image’s intrinsic pixel dimensions (e.g. a 1600×900 file uses width="1600" height="900"). Then apply CSS like max-width: 100%; height: auto to make the image fluid. This pattern preserves the intrinsic aspect ratio for layout stability while allowing the element to scale with its container. If the design requires a fixed aspect (e.g. 1:1 thumbnail crops), use CSS aspect-ratio or consistent server-side cropping so attribute values reflect the actual asset.

In lazy-loading scenarios (loading="lazy"), attributes remain essential for reserving space. For responsive images, use srcset with w-descriptors and a sizes rule matching your layout; the width attribute continues to inform intrinsic sizing but does not affect candidate selection. When templates emit dynamic image sizes, ensure the attribute pair updates with the transformed asset’s intrinsic dimensions to avoid CLS and distortion.

  • Prefer accurate width and height attributes or a CSS aspect-ratio for every meaningful img.
  • Keep attribute values in CSS pixels; do not include units or percentages.
  • Audit CMS/CDN pipelines to ensure emitted attributes match transformed assets.

Comparisons

  • Width attribute vs CSS width: The attribute sets a default size and contributes to intrinsic aspect ratio; CSS width determines the final rendered size and supports fluid units. CSS overrides the attribute for layout.
  • Width attribute vs sizes/srcset: sizes and srcset control resource selection and download cost; the attribute does not influence which file is fetched.
  • Width/height attributes vs CSS aspect-ratio: Both can reserve space to prevent CLS. Attributes are HTML-level hints and widely supported; aspect-ratio is a CSS property that can be convenient for uniform ratios across variants.
  • Width attribute vs intrinsic size metadata: Browsers can infer natural dimensions from the image file, but attributes provide the ratio before decode, improving early layout stability.

FAQs

Should width match the image’s intrinsic width or the container width?

Set width and height attributes to the intrinsic pixel dimensions of the image resource. Use CSS to constrain the rendered size to the container (e.g. max-width: 100%; height: auto). This approach yields correct aspect ratio reservation and responsive behaviour without distortion.

Do I still need width and height if I use CSS aspect-ratio?

A CSS aspect-ratio can replace the attribute pair for reserving space, especially when the desired ratio differs from the intrinsic image. However, width and height are simple, broadly supported and travel with the markup, making them reliable defaults. Many teams use either attributes or aspect-ratio consistently across components.

Does the width attribute affect which image variant is downloaded?

No. Image candidate selection is driven by srcset descriptors and the sizes attribute, evaluated against the device pixel ratio and layout constraints. The width attribute does not participate in that algorithm; it primarily contributes to intrinsic sizing and aspect-ratio calculation.

What happens if width and height do not match the actual image file?

Mismatch can cause distortion (stretched or squashed images) or a layout correction when the browser reconciles intrinsic dimensions after decode. This may contribute to CLS. Always emit accurate attribute values that match the served asset or use CSS aspect-ratio to define an intentional crop.

Is the width attribute required for lazy-loaded images?

It is not required by the loading attribute, but it is strongly recommended. Lazy-loaded images without reserved space can cause visible jumps when they enter the viewport. Supplying width and height or a CSS aspect-ratio helps maintain a stable layout while images are fetched on demand.

Synonyms

HTML width attributeimg width attributewidth (HTML)element width attributeimage width