HTML height Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Media & Layout

What You’ll Learn

The height attribute sets the vertical size of certain HTML elements in CSS pixels. It is most commonly used on <img>, <iframe>, <canvas>, and <video>. For general page layout, CSS height is the modern approach—but the HTML attribute still matters for media defaults and embedded content.

01

Pixel Values

Integer CSS pixels only.

02

img & Media

Images, canvas, video.

03

iframe Height

Pixels or percentages.

04

CSS Alternative

Prefer CSS for layout.

05

No px Suffix

height="200" not "200px".

06

JavaScript

img.height vs style.height.

Purpose of height Attribute

The height attribute defines how tall an element appears on the page. On <img>, <canvas>, and <video>, the value is an integer representing CSS pixels—for example, height="200" means 200 pixels tall. Do not include a px suffix in the HTML attribute value.

On <iframe>, the attribute accepts either an integer pixel value or a percentage string such as height="50%". The percentage is relative to the containing block’s height. Units like vh, rem, and em are not valid in the HTML height attribute—use the CSS height property for those.

💡
CSS is the modern choice for layout

For <div>, sections, and responsive layouts, use CSS height with any valid unit (px, rem, vh, %). The HTML attribute remains useful as a default on <img> and <iframe>, especially before CSS loads or in simple embeds.

📝 Syntax

Add height to a supported element with an integer pixel value (no unit suffix):

height.html
<!-- Image: integer CSS pixels -->
<img src="photo.jpg" alt="Landscape" width="400" height="300">
<!-- iframe: pixels or percentage -->
<iframe src="page.html" width="100%" height="400" title="Embedded page"></iframe>
<!-- CSS alternative for any element -->
<style>
  .hero { height: 50vh; }
</style>

Syntax Rules

  • On <img>, <canvas>, and <video>: integer CSS pixels only—use height="200", not height="200px".
  • On <iframe>: integer pixels or a percentage string such as height="50%".
  • vh, rem, em, and other CSS units are not valid in the HTML attribute—use CSS height instead.
  • Always pair height with width on images to prevent layout shift while loading.
  • height on <td> and <th> is deprecated—use CSS for table cell sizing.
  • For responsive layouts, prefer CSS height, max-height, or aspect-ratio.

💎 Values

The height attribute accepts different value formats depending on the element:

  • height="128" — Integer CSS pixels on <img>, <canvas>, or <video>. No px suffix.
  • height="400" — Pixel height on <iframe> (same integer format).
  • height="50%" — Percentage of the containing block’s height. Valid on <iframe> only.
  • height: 50vhCSS only. Viewport units are not valid in the HTML attribute.
height-values.html
<!-- Correct: integer pixels -->
<img src="logo.png" alt="Logo" width="128" height="128">
<!-- Correct: iframe with percentage -->
<iframe src="chart.html" height="50%" title="Sales chart"></iframe>
<!-- Wrong: px suffix in HTML attribute -->
<img src="photo.jpg" height="200px"> <!-- Invalid -->
<!-- Use CSS for vh, rem, em -->
<style>
  .panel { height: 20rem; }
  .banner { height: 40vh; }
</style>

⚡ Quick Reference

ConceptDetailsNotes
Applies to<img>, <iframe>, <canvas>, <video>Media and embed elements
Pixel formatInteger only (no px)e.g. height="200"
iframe percentageheight="50%"Relative to container
CSS units (vh, rem)CSS height propertyNot valid in HTML attribute
JS on imgimg.height = 200Number, not string
JS on divel.style.height = "200px"CSS string with unit

Applicable Elements

ElementSupported?Notes
<img>YesInteger CSS pixels—always include width too
<iframe>YesPixels or percentage string
<canvas>YesInteger CSS pixels for display size
<video>YesInteger CSS pixels
<td>, <th>DeprecatedUse CSS height on cells instead
<div>, <section>NoUse CSS height property

HTML height vs CSS height

ApproachExampleWhen to Use
HTML attribute<img height="128">Default size on img, iframe, canvas, video
CSS property.box { height: 200px; }Layout on any element; supports all units
CSS viewport unitsheight: 50vh;Full-height sections—CSS only
CSS relative unitsheight: 20rem;Scalable typography-based sizing—CSS only
iframe percentageheight="50%"HTML attribute on iframes only
Modern best practiceCSS + aspect-ratioResponsive layouts; HTML attr as fallback

Examples Gallery

Image height in pixels, iframe embed sizing, and dynamic height changes with JavaScript.

👀 Live Preview

An image sized with height="128" (integer pixels, no px suffix):

JavaScript logo

Example — Image Height in Pixels

Set image height with an integer value. Always include width, height, and descriptive alt text:

image-height.html
<img
  src="/images/javascript.png"
  alt="JavaScript logo"
  width="128"
  height="128">
Try It Yourself

How It Works

The browser reads height="128" as 128 CSS pixels and renders the image at that vertical size. Pairing width and height reserves space before the image loads, reducing layout shift.

Example — iframe Height

Control embedded content height with the height attribute. Iframes accept integer pixels or a percentage:

iframe-height.html
<iframe
  src="https://example.com/widget"
  width="100%"
  height="180"
  title="Embedded widget"></iframe>
<!-- Percentage relative to container -->
<iframe src="panel.html" height="50%" title="Side panel"></iframe>
Try It Yourself

How It Works

The iframe’s height attribute sets the frame’s vertical dimension before CSS applies. Always include a title for accessibility. For responsive embeds, combine with CSS or use percentage values relative to a sized container.

Dynamic Values with JavaScript

Change element height at runtime. Use the height property (number) on media elements, or style.height (CSS string) on any element:

dynamic-height.html
<img id="myImg" src="photo.jpg" alt="Photo" height="100">
<div id="myBox">Resizable box</div>
<script>
  // img, canvas, video: assign a number (CSS pixels)
  var img = document.getElementById("myImg");
  img.height = 200;
  // div and other elements: use style.height with a CSS string
  var box = document.getElementById("myBox");
  box.style.height = "200px";
</script>
Try It Yourself

How It Works

Media elements expose a numeric height property that maps to CSS pixels. General elements like <div> do not have this property—use element.style.height with a full CSS value including the unit.

♿ Accessibility

  • Always include alt on images — Height controls size, not meaning. Descriptive alt text tells screen reader users what the image represents.
  • Set title on iframes — Describes embedded content for assistive technology users.
  • Preserve aspect ratio when scaling — Distorted images from mismatched width/height harm readability.
  • Avoid fixed heights that clip content — Use max-height in CSS when content may overflow.
  • Do not rely on height alone for layout — Responsive designs should use CSS with relative units for better zoom and reflow support.
  • Test at different zoom levels — Fixed pixel heights can cause usability issues when users enlarge text.

🧠 How height Works

1

Author sets height attribute

Integer pixels or iframe percentage.

Markup
2

Browser parses value

Maps to CSS pixels or percentage.

Parse
3

Element renders at size

CSS height may override attribute.

Render
=

Sized media element

Predictable vertical dimensions.

Browser Support

The height attribute is fully supported in all modern browsers on <img>, <iframe>, <canvas>, and <video>. Use integer pixel values without a px suffix for consistent behavior.

HTML4 · Fully supported

Universal element sizing

All major browsers honor the height attribute on media and embed elements.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
height attribute 99% supported

Bottom line: Use height confidently on img and iframe; prefer CSS for responsive layout with vh, rem, and aspect-ratio.

💡 Best Practices

✅ Do

  • Use integer pixels: height="200" on img, canvas, video
  • Pair width and height on images to prevent layout shift
  • Use CSS height for divs, sections, and responsive layouts
  • Set title on iframes and alt on images
  • Use percentage height on iframes when the container has a defined height

❌ Don’t

  • Write height="200px" in HTML attributes
  • Use vh, rem, or em in the HTML attribute
  • Rely on height on deprecated <td>/<th>
  • Stretch images with mismatched width/height ratios
  • Forget that CSS height overrides the HTML attribute when both are set

Conclusion

The height attribute is a straightforward way to set vertical size on images, iframes, canvas, and video elements. Remember: use integer CSS pixels without a px suffix, and reserve CSS units like vh and rem for the CSS height property.

For modern responsive layouts, CSS is the preferred tool. The HTML attribute still earns its place as a reliable default on media elements and simple embeds. Pair images with width, height, and alt; give iframes a descriptive title.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about height

Bookmark these before sizing your next media element.

5
Core concepts
🖼 02

Media elements

img, iframe, canvas.

Scope
💻 03

CSS for layout

vh, rem, em in CSS.

Modern
⚙️ 04

img.height = 200

Number for media.

Script
📝 05

style.height

"200px" for divs.

Pattern

❓ Frequently Asked Questions

It sets the vertical size of an element in CSS pixels. On <img>, <canvas>, and <video>, use an integer without a px suffix, such as height="200".
<img>, <iframe>, <canvas>, <video>, <embed>, and <object>. The attribute on <td> and <th> is deprecated. For <div> and other elements, use CSS height.
No. On img, canvas, and video, write height="200" (integer CSS pixels). The px suffix belongs in CSS: style="height: 200px" or a stylesheet rule.
The HTML attribute sets a default size on specific media elements using integer pixels (or percentages on iframes). CSS height works on any element and accepts all units including vh, rem, and %. CSS overrides the HTML attribute when both are set.
For <img>, <canvas>, and <video>, assign a number: img.height = 200. For <div> and other elements, use element.style.height = "200px" with a CSS string including the unit.
Yes. All modern browsers support the height attribute on img, iframe, canvas, and video. Use correct integer pixel values without a px suffix for reliable cross-browser behavior.

Size your media elements correctly

Practice the height attribute with image, iframe, and JavaScript examples in the Try It editor.

Try image height example →

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.

4 people found this page helpful