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


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.
Integer CSS pixels only.
Images, canvas, video.
Pixels or percentages.
Prefer CSS for layout.
height="200" not "200px".
img.height vs style.height.
height AttributeThe 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.
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.
Add height to a supported element with an integer pixel value (no unit suffix):
<!-- 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><img>, <canvas>, and <video>: integer CSS pixels only—use height="200", not height="200px".<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.height with width on images to prevent layout shift while loading.height on <td> and <th> is deprecated—use CSS for table cell sizing.height, max-height, or aspect-ratio.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: 50vh — CSS only. Viewport units are not valid in the HTML attribute.<!-- 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>| Concept | Details | Notes |
|---|---|---|
| Applies to | <img>, <iframe>, <canvas>, <video> | Media and embed elements |
| Pixel format | Integer only (no px) | e.g. height="200" |
| iframe percentage | height="50%" | Relative to container |
| CSS units (vh, rem) | CSS height property | Not valid in HTML attribute |
| JS on img | img.height = 200 | Number, not string |
| JS on div | el.style.height = "200px" | CSS string with unit |
| Element | Supported? | Notes |
|---|---|---|
<img> | Yes | Integer CSS pixels—always include width too |
<iframe> | Yes | Pixels or percentage string |
<canvas> | Yes | Integer CSS pixels for display size |
<video> | Yes | Integer CSS pixels |
<td>, <th> | Deprecated | Use CSS height on cells instead |
<div>, <section> | No | Use CSS height property |
height vs CSS height| Approach | Example | When 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 units | height: 50vh; | Full-height sections—CSS only |
| CSS relative units | height: 20rem; | Scalable typography-based sizing—CSS only |
| iframe percentage | height="50%" | HTML attribute on iframes only |
| Modern best practice | CSS + aspect-ratio | Responsive layouts; HTML attr as fallback |
Image height in pixels, iframe embed sizing, and dynamic height changes with JavaScript.
An image sized with height="128" (integer pixels, no px suffix):

Set image height with an integer value. Always include width, height, and descriptive alt text:
<img
src="/images/javascript.png"
alt="JavaScript logo"
width="128"
height="128">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.
Control embedded content height with the height attribute. Iframes accept integer pixels or a percentage:
<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>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.
Change element height at runtime. Use the height property (number) on media elements, or style.height (CSS string) on any element:
<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>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.
alt on images — Height controls size, not meaning. Descriptive alt text tells screen reader users what the image represents.title on iframes — Describes embedded content for assistive technology users.max-height in CSS when content may overflow.Integer pixels or iframe percentage.
Maps to CSS pixels or percentage.
CSS height may override attribute.
Predictable vertical dimensions.
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.
All major browsers honor the height attribute on media and embed elements.
Bottom line: Use height confidently on img and iframe; prefer CSS for responsive layout with vh, rem, and aspect-ratio.
height="200" on img, canvas, videowidth and height on images to prevent layout shiftheight for divs, sections, and responsive layoutstitle on iframes and alt on imagesheight="200px" in HTML attributesvh, rem, or em in the HTML attributeheight on deprecated <td>/<th>height overrides the HTML attribute when both are setThe 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.
heightBookmark these before sizing your next media element.
height="200" not "200px".
Syntaximg, iframe, canvas.
Scopevh, rem, em in CSS.
ModernNumber for media.
Script"200px" for divs.
Pattern<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.height="200" (integer CSS pixels). The px suffix belongs in CSS: style="height: 200px" or a stylesheet rule.height works on any element and accepts all units including vh, rem, and %. CSS overrides the HTML attribute when both are set.<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.height attribute on img, iframe, canvas, and video. Use correct integer pixel values without a px suffix for reliable cross-browser behavior.Practice the height attribute with image, iframe, and JavaScript examples in the Try It editor.
4 people found this page helpful