Image height attribute

Definition and usage

Height Attribute set or return the image's Height attribute Value.

Height attribute Specify the height of the image.

This attribute can also return the height of the image using CSS styles (see the example below).

Tip:Please use Width attribute Set or return the image's Width attribute value.

Example

Example 1

Change the height of the image to 300px:

document.getElementById("myImg").height = "300";

Try it yourself

Example 2

Return the height of the image:

var x = document.getElementById("myImg").height;

Try it yourself

Example 3

Change the height and width of the image to 300px and 450px respectively:

document.getElementById("myImg").height = "300";
document.getElementById("myImg").width = "450";

Try it yourself

Example 4

Return the height of the image using CSS styles:

var x = document.getElementById("myImg").height;

Try it yourself

Syntax

Return height attribute:

imageObject.height

Set height attribute:

imageObject.height = pixels

属性值

描述
pixels 以像素为单位的高度(例如 height="100")。

技术细节

返回值: 数值,表示图像的高度,以像素为单位。

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持

相关页面

HTML 参考手册:HTML <img> height 属性