HTML width Attribute

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

Definition and Usage

The width attribute sets an element’s horizontal size in CSS pixels. It is commonly used on <img>, <iframe>, <canvas>, <video>, <embed>, and <object>.

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.

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).

Attribute Values

How width is interpreted depends on the element:

Value / elementDescriptionTry it
integer on <img>Intrinsic width in CSS pixels; helps reserve layout spaceTry it
integer on <iframe>Frame width in CSS pixels (default 300)Try it
integer on <canvas>Drawing buffer width in CSS pixels (default 300)Try it
integer on <video>Display area width in CSS pixels (absolute values only)Try it
width="200px"Incorrect on img/canvas/video — omit pxTry it

Live Preview

Change the width value on a real image. Height stays matched so the square logo stays clear.

A real <img>. Pick a width, then Apply (or use a chip).

Image
HTML logo used in the width live preview

width="96" height="96"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Image Width and Height

Set both dimensions so the browser can reserve space before the image loads.

HTML
<img
  src="/images/html.png"
  alt="HTML logo"
  width="128"
  height="128">
Try It Yourself

How It Works

  1. Use integers. width="128" means 128 CSS pixels — do not write 128px.
  2. Match height. Setting both width and height lets the browser compute an aspect ratio early.
  3. Add alt text. alt keeps the image accessible if it fails to load.
  4. Reduce layout shift. Reserved space stops nearby text from jumping when the image arrives.

Example 2 — iframe Display Width

Control how wide an embedded frame appears with a pixel width.

HTML
<iframe
  srcdoc="<p>Hello from the iframe</p>"
  title="Demo panel"
  width="320"
  height="160">
</iframe>
Try It Yourself

How It Works

  1. Pixel width. width="320" sets the frame box to 320 CSS pixels (default is 300).
  2. Always set title. A descriptive title helps screen reader users understand the frame.
  3. srcdoc for demos. Inline HTML is handy for practice; real pages usually use src.
  4. CSS can override. A CSS width rule wins over the HTML attribute when both apply.

Example 3 — Canvas Drawing Buffer Width

On <canvas>, width sizes the bitmap used for drawing — not only the CSS box.

HTML
<canvas id="box" width="200" height="100">
  Canvas not supported
</canvas>

<script>
  var canvas = document.getElementById("box");
  var ctx = canvas.getContext("2d");
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(20, 20, 160, 60);
</script>
Try It Yourself

How It Works

  1. Buffer size. width="200" creates a 200-pixel-wide drawing surface (default is 300).
  2. Prefer attributes over CSS. CSS scaling stretches the bitmap and can blur graphics.
  3. Fallback text. Content inside <canvas> shows when canvas is unavailable.
  4. 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.

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 Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
width Widely 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.

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.

Next: wrap

Control how text wraps in textarea elements.

wrap 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