CKTIS / HTML
HTML height attribute
The height attribute specifies the vertical dimension of supported HTML elements. It takes a non-negative integer without a unit. On canvas, it controls bitmap height, not merely the displayed size.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
height="N"height is not a global attribute. It applies to img, iframe, embed, object, video, input with type="image", and source whose parent is picture. Its value is a non-negative integer in CSS pixels: write height="100", not height="100px" or height="50%".
Within picture, the img element displays the image; height does not belong on picture itself. Dimensions on an applicable source describe an image variant. Dimension attributes are not intended to distort a resource's aspect ratio.
The canvas element also supports height, but it sets the bitmap height. A missing attribute or a parsing error defaults to 150; the default width is 300. The display size set through CSS is a separate concern.
For a 2d context, assigning canvas.height clears the bitmap and resets drawing state, even when the value is unchanged. The example shows a drawn pixel's alpha channel changing from 255 to 0 after this assignment.
Examples
<!doctype html>
<meta charset="utf-8">
<title>height</title>
<canvas id="canvas" width="200" height="100"></canvas>
<pre id="output"></pre>
<script>
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
context.fillStyle = "red";
context.fillRect(0, 0, 20, 20);
const before = context.getImageData(0, 0, 1, 1).data[3];
canvas.height = 100;
const after = context.getImageData(0, 0, 1, 1).data[3];
document.getElementById("output").textContent =
`${canvas.width} x ${canvas.height}\n${before} -> ${after}`;
</script>Result
200 x 100 255 -> 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.