Open In App

How to specify the width and height of the canvas using HTML ?

Improve
Improve
Like Article
Like
Save
Share
Report

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 tag has two attributes which are listed below.

  • height: This attribute is used to set the height of canvas.
  • width: This attribute is used to set the width of canvas.

Example 1:

HTML




<!-- HTML code to illustrate height and
     width attribute of canvas tag -->
<!DOCTYPE html>
<html>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>
        How to specify the width
        and height of the canvas?
    </h3>
  
    <canvas id="canvasID" height="200" width="200" 
        style="border:1px solid black">
    </canvas>
</body>
</html>


Output:

Example 2:

HTML




<!-- HTML code to illustrate height and
    width attribute of canvas tag -->
<!DOCTYPE html>
<html>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>
        How to specify the width
        and height of the canvas?
    </h3>
  
    <canvas id="canvasID" height="200" width="200" 
        style="border:1px solid black">
    </canvas>
  
    <script>
        var canvas = document.getElementById("canvasID");
        // Getting a drawing context in the canvas
        var context = canvas.getContext("2d");
        context.beginPath();
        context.arc(100, 100, 90, 0, 2 * Math.PI);
        context.stroke(); 
    </script>
</body>
  
</html>


Output:



Last Updated : 30 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads