--- title: HTMLCanvasElement.height slug: Web/API/HTMLCanvasElement/height translation_of: Web/API/HTMLCanvasElement/height ---
{{APIRef("Canvas API")}}

HTMLCanvasElement.height 属性是一个正整数 ,使用了{{HTMLElement("canvas")}} 元素的HTML 属性{{htmlattrxref("height", "canvas")}}来反映该元素高度的 CSS 像素值。当该属性没有被定义,或被定义为一个无效值(如负值)时, 将使用150作为它的默认值。

控制canvas元素大小的属性有两个,这是其中一个,还有一个是{{domxref("HTMLCanvasElement.width")}}。

语法

var pxl = canvas.height;
canvas.height = pxl;

示例

给出这样一个{{HTMLElement("canvas")}} 元素:

<canvas id="canvas" width="300" height="300"></canvas>

可以通过以下代码获得这个canvas元素的高度:

var canvas = document.getElementById('canvas');
console.log(canvas.height); // 300

规范

规范 状态 备注
{{SpecName('HTML WHATWG', "scripting.html#attr-canvas-height", "HTMLCanvasElement.height")}} {{Spec2('HTML WHATWG')}} No change since the latest snapshot, {{SpecName('HTML5 W3C')}}
{{SpecName('HTML5.1', "scripting-1.html#attr-canvas-height", "HTMLCanvasElement.height")}} {{Spec2('HTML5.1')}}  
{{SpecName('HTML5 W3C', "scripting-1.html#attr-canvas-height", "HTMLCanvasElement.height")}} {{Spec2('HTML5 W3C')}} Snapshot of the {{SpecName('HTML WHATWG')}} containing the initial definition.

浏览器兼容性

{{ CompatibilityTable() }}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatChrome(4) }} {{CompatVersionUnknown}} {{ CompatGeckoDesktop("1.9.2") }} {{ CompatIE(9) }} {{ CompatOpera(9) }} {{ CompatSafari(3.1) }}
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{CompatVersionUnknown}} {{ CompatGeckoMobile("1.9.2") }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

其他