The HTML height attribute
The HTML height attribute sets the intrinsic height of the element in CSS pixels. It is used on the <img>, <video>, <canvas>, <iframe>, <embed>, <object> and <input type="image"> elements.
Overview
The height attribute sets the rendered height of certain elements — <img>, <video>, <iframe>, <canvas>, <embed> and image inputs — as a number of CSS pixels (unitless, with no "px").
Its most important job, together with width, is preventing layout shift on images and iframes: providing both dimensions lets the browser reserve the correct space before the resource loads, so content does not jump as the page fills in. That improves the Cumulative Layout Shift metric and the reading experience.
As with width, CSS overrides the attribute for display purposes. The common responsive recipe is to keep the width/height attributes for their aspect ratio and add max-width: 100%; height: auto in CSS so the element scales down without distortion.
Syntax
<img src="p.jpg" width="640" height="360" alt="">
Values
| Value |
|---|
| A non-negative integer (pixels). |
Best practices
- Set width and
heighttogether on images and iframes to prevent layout shift. - Use a unitless pixel number —
height="480", notheight="480px". - Add CSS
height: autowithmax-width: 100%so responsive images keep their ratio. - On a <canvas>, the attribute is the drawing resolution, separate from the CSS display size.
Frequently asked questions
What does the height attribute do?
Why set both width and height on an image?
Do I use px in the height attribute?
height="480".How do I keep an image responsive after setting height?
max-width: 100%; height: auto so it scales down while preserving its aspect ratio.