HTML height Attribute

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

Definition and Usage

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

Pair it with width on images so the browser can reserve space before the file loads. For general page layout, prefer the CSS height property.

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.

Deprecated on table cells.

height on <td> and <th> is deprecated. Size table cells with CSS. Non-standard height on <table> should also be replaced with CSS.

HTML Syntax

syntax.html
<img height=" pixels" width="pixels">

Replace img with iframe, canvas, or video as needed. pixels is a non-negative integer (CSS pixels).

Attribute Values

How height is interpreted depends on the element:

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

Live Preview

Change the height value on a real image. Width stays fixed so you can see the attribute take effect.

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

Image
HTML logo used in the height live preview

height="96" width="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. height="128" means 128 CSS pixels — do not write 128px.
  2. Match width. 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 Height

Control how tall an embedded frame appears with a pixel height.

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 height. height="160" sets the frame box to 160 CSS pixels (default is 150).
  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 height rule wins over the HTML attribute when both apply.

Example 3 — Canvas Drawing Buffer Height

On <canvas>, height 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. height="100" creates a 100-pixel-tall drawing surface (default is 150).
  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 height like CSS or using it on the wrong elements.

units

No px in the attribute

Write height="200", not height="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 height instead.

deprecated

Table cells

height on <td>/<th> is deprecated. Non-standard table height should become CSS min-height or height.

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 height value wins for the rendered box. Keep them intentional and consistent.

Browser Support

height 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

height

Widely available across Chrome, Firefox, Safari, Edge, and Opera for img, iframe, canvas, video, embed, and object. Avoid deprecated table-cell 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
height 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 vertical 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 height for general page sizing; avoid deprecated table uses.

One line: HTML height sizes media embeds in pixels; CSS height sizes everything else.

Frequently Asked Questions

It sets the vertical 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 height.
Common valid uses are img, iframe, canvas, video, embed, and object. Prefer CSS height for layout on divs and other elements. height on td and th is deprecated.
No for img, canvas, and video. Use an integer only: height="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 height works on all elements and accepts units like px, rem, %, and vh.
Yes when you know the intrinsic size. Together they let the browser compute an aspect ratio early and reduce Cumulative Layout Shift (CLS).
No. height on td and th is deprecated. Size table cells with CSS instead.

Next: hidden

Hide elements from layout and assistive tech with the hidden attribute.

hidden 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