How to Set the height and width of the Canvas in HTML5?
Last Updated :
06 Mar, 2024
The canvas element is a part of HTML5 which allows us for dynamic, scriptable rendering of 2D shapes and bitmap images, facilitating the creation of games, graphs, animations, and compositions.
To set the height and width of the canvas in HTML5, we can utilize the height and width attributes within the <canvas> tag, specifying pixel values to define the canvas dimensions.
Different Approaches to Set the height and width of Canvas in HTML5
1. Using HTML Attributes:
Using HTML attributes, specify the canvas dimensions by adding width and height attributes within the <canvas> tag, defining pixel values to establish the canvas size for rendering graphics in HTML5.
Syntax:
<canvas width="300" height="300"> HTML Contents... </canvas>
Example: In this example, we use the canvas element with width and height attributes set to 300 pixels each.
HTML
<!DOCTYPE html>
<html>
<body>
<h1 style="color:green">
Welcome To GeeksForGeeks
</h1>
<h1>
Canvas Height and Width Attributes
</h1>
<canvas id="Canvas"
width="300"
height="300"
style="border:3px solid">
</canvas>
<script>
let c = document.getElementById("Canvas");
let ctx = c.getContext("2d");
ctx.fillStyle = "Green";
ctx.fillRect(100, 100, 100, 100);
</script>
</body>
</html>
Output:

2. Using CSS:
To set the height and width of a canvas in HTML5 using CSS, target the canvas element in CSS and apply the width and height properties, specifying pixel values to define the canvas dimensions.
Syntax:
#myCanvas {
margin: 20px;
padding: 20px;
background: #ffffff;
border: 3px solid green;
width: 300px;
height: 190px;
}
Example: In this example we applies CSS styles to the canvas element with the ID "myCanvas," setting its width and height to 300px and 190px respectively, along with margin, padding, background, and border properties.
HTML
<!DOCTYPE html>
<html>
<style>
#myCanvas {
margin: 20px;
padding: 20px;
background: #ffffff;
border: 3px solid green;
width: 300px;
height: 190px;
}
</style>
<body>
<h1 style="color:green">
Welcome To GeeksForGeeks
</h1>
<h1>
Canvas Height and Width
</h1>
<canvas id="myCanvas"></canvas>
</body>
</html>
Output:

Similar Reads
How to specify the width and height of the canvas using HTML ? The <canvas> tag in HTML is used to draw graphics on web page using JavaScript. It can be used to draw paths, boxes, texts, gradient, and adding images. By default, it does not contain borders and text. Syntax: <canvas id="canvasID"> HTML Contents... <canvas> Attributes: The canvas
1 min read
How to specify the width and height of the object in HTML5? The <object> tag is used to display multimedia like audio, videos, images, PDFs, and Flash in web pages. It can also be used for displaying another webpage inside the HTML page. The <param> tag is also used along with this tag to define various parameters. Any text that is written within
1 min read
How to set the height and width of the video player in HTML5 ? In this article, we will learn to set the width and height of the video player in HTML5. The following are the two scenarios The width and height of the video are fixed.Make the video respond to your screen width. Approach: The HTML5 width and height attributes of the video tag are used to set the h
2 min read
How to Set Width and Height of an Image using HTML? When adding images to a webpage, it's important to set their dimensions using the height and width attributes in HTML. Without these dimensions, the browser won't know how much space to reserve for the image, which can cause layout shifts that disrupt the viewing experience. Note: Always specify the
2 min read
How to Set Height and Width of a Chart in Chart.js ? Chart.js is a popular JavaScript library that allows developers to create interactive and visually appealing charts for web applications. One of the key aspects of chart customization is controlling its dimensions. In this article, we'll explore how to set the height and width of a Chart.js chart.Ch
4 min read
How to find the height of a text in HTML canvas using JavaScript ? In this article, we will find the height of the text canvas using JavaScript. We have two approaches to find the height of a text in HTML canvas using JavaScript which are described below: Approach 1: In the following example, the height attribute of the HTML canvas is used. First set the font in pt
2 min read