Pair it with height on images so the browser can reserve space before the file loads. For general page layout, prefer the CSS width property (and max-width for responsive images).
Baseline · Widely available.
Not experimental. Stable on modern media and embed elements. Always write a bare integer for pixels on img, canvas, and video — no px suffix.
Obsolete on tables.
width on <table>, <td>, and <th> is obsolete. Size tables and cells with CSS instead.
Syntax
HTML Syntax
syntax.html
<img width="pixels" height="pixels">
Replace img with iframe, canvas, or video as needed. pixels is a non-negative integer (CSS pixels).
Values
Attribute Values
How width is interpreted depends on the element:
Value / element
Description
Try it
integer on <img>
Intrinsic width in CSS pixels; helps reserve layout space
Buffer size.width="200" creates a 200-pixel-wide drawing surface (default is 300).
Prefer attributes over CSS. CSS scaling stretches the bitmap and can blur graphics.
Fallback text. Content inside <canvas> shows when canvas is unavailable.
Draw after the element exists. The short script fills a rectangle so you can see the sized canvas.
Edge Cases & Pitfalls
Most mistakes come from treating HTML width like CSS or using it on the wrong elements.
units
No px in the attribute
Write width="200", not width="200px", on img, canvas, and video.
layout
Not for every element
Do not use the HTML attribute to size div, section, or text blocks. Use CSS width instead.
obsolete
Tables and cells
width on <table>/<td>/<th> is obsolete. Use CSS width, max-width, or table layout properties.
canvas
CSS can distort canvas
Changing display size with CSS without matching the attribute stretches the bitmap. Set the buffer with HTML attributes.
css
CSS overrides the attribute
If both apply, the CSS width value wins for the rendered box. Keep them intentional and consistent.
Compatibility
Browser Support
width is an HTML attribute for media and embed elements in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
width
Widely available across Chrome, Firefox, Safari, Edge, and Opera for img, iframe, canvas, video, embed, and object. Avoid obsolete table usage.
97%Global support (approx.)
Google ChromeAll
Full support
Mozilla FirefoxAll
Full support
Apple SafariAll
Full support
Microsoft EdgeAll
Full support
OperaAll
Full support
Samsung InternetAll
Full support
Internet ExplorerAll
Supported
widthWidely supported
Bottom line: Safe to use on media and embeds. Prefer CSS for general layout, and always set image width and height together when you know the intrinsic size.
Remember
Key Takeaways
Role: set horizontal size in CSS pixels on media and embed elements.
Format: integer only on img/canvas/video — no px.
Images: set both width and height to reduce layout shift.
Layout: use CSS width for general page sizing; avoid obsolete table uses.
One line: HTML width sizes media embeds in pixels; CSS width sizes everything else.
Frequently Asked Questions
It sets the horizontal size of supported elements in CSS pixels. On img, it helps the browser reserve space and reduce layout shift. On canvas, it also sets the drawing buffer width.
Common valid uses are img, iframe, canvas, video, embed, and object. Prefer CSS width for layout on divs and other elements. width on td, th, and table is obsolete — use CSS instead.
No for img, canvas, and video. Use an integer only: width="200". The px suffix belongs in CSS, not the HTML attribute.
The HTML attribute works on specific media and embed elements and usually takes a bare pixel number. CSS width works on all elements and accepts units like px, rem, %, and vw.
Yes when you know the intrinsic size. Together they let the browser compute an aspect ratio early and reduce Cumulative Layout Shift (CLS).
No. width on td, th, and table is obsolete in the HTML Standard. Size tables and cells with CSS instead.