How to center canvas in HTML5?

To center a canvas element in HTML5, you can use several CSS techniques. The most common approach is to wrap the canvas in a div and apply text-align: center to the container, or use CSS flexbox for more control over positioning.

Syntax

Following is the basic syntax for centering a canvas element −

<div style="text-align: center;">
   <canvas id="myCanvas" width="400" height="200"></canvas>
</div>

Method 1 − Using Text-Align Center

The simplest way to center a canvas is to wrap it in a div container and apply text-align: center to the div. Since canvas is an inline element by default, it responds to text alignment properties.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Center Canvas with Text-Align</title>
</head>
<body style="font-family: Arial, sans-serif; margin: 0; padding: 20px;">
   <h2 style="text-align: center;">Centered Canvas Example</h2>
   <div style="text-align: center;">
      <canvas id="myCanvas" width="300" height="150" style="background-color: lightgreen; border: 2px solid black;">
         Your browser does not support the canvas element.
      </canvas>
   </div>
</body>
</html>

The canvas appears centered horizontally on the page with a light green background −

Centered Canvas Example
[300x150 canvas with light green background, centered horizontally]

Method 2 − Using CSS Flexbox

Flexbox provides more precise control over canvas positioning and can center the canvas both horizontally and vertically within its container.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Center Canvas with Flexbox</title>
</head>
<body style="font-family: Arial, sans-serif; margin: 0; height: 100vh;">
   <div style="display: flex; justify-content: center; align-items: center; height: 100%; background-color: #f0f0f0;">
      <canvas id="flexCanvas" width="250" height="200" style="background-color: lightblue; border: 3px solid navy;">
         Canvas not supported
      </canvas>
   </div>
</body>
</html>

This method centers the canvas both horizontally and vertically within the viewport −

[250x200 canvas with light blue background, perfectly centered in viewport]

Method 3 − Using CSS Margin Auto

You can also center a canvas by setting it as a block element and applying margin: 0 auto. This approach works well when you want the canvas to behave like a block-level element.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Center Canvas with Margin Auto</title>
</head>
<body style="font-family: Arial, sans-serif; margin: 0; padding: 20px;">
   <h2 style="text-align: center;">Canvas Centered with Margin Auto</h2>
   <canvas id="marginCanvas" width="400" height="180" 
           style="display: block; margin: 0 auto; background-color: lightyellow; border: 2px dashed orange;">
      Canvas element is not supported.
   </canvas>
   <p style="text-align: center; margin-top: 20px;">Canvas is centered using margin: 0 auto</p>
</body>
</html>

The canvas is displayed as a block element and centered horizontally −

Canvas Centered with Margin Auto
[400x180 canvas with light yellow background, centered horizontally]
Canvas is centered using margin: 0 auto
Canvas Centering Methods Text-Align ? Wrap in div ? text-align: center ? Horizontal only ? Simple method Flexbox ? display: flex ? justify-content: center ? Both directions ? Modern approach Margin Auto ? display: block ? margin: 0 auto ? Horizontal only ? Block-level styling Choose based on your layout needs and browser support requirements

Comparison of Centering Methods

Following table compares the different methods for centering canvas elements −

Method Horizontal Center Vertical Center Browser Support Best Use Case
Text-Align Center Yes No All browsers Simple horizontal centering
Flexbox Yes Yes Modern browsers Full control over positioning
Margin Auto Yes No All browsers Block-level canvas centering

Adding Drawing to Centered Canvas

Once the canvas is centered, you can draw on it using JavaScript. The centering method does not affect the drawing functionality.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Centered Canvas with Drawing</title>
</head>
<body style="font-family: Arial, sans-serif; margin: 0; padding: 20px;">
   <div style="text-align: center;">
      <canvas id="drawCanvas" width="300" height="200" style="border: 2px solid #333;"></canvas>
   </div>
   <script>
      var canvas = document.getElementById('drawCanvas');
      var ctx = canvas.getContext('2d');
      
      // Draw a centered circle
      ctx.beginPath();
      ctx.arc(150, 100, 50, 0, 2 * Math.PI);
      ctx.fillStyle = '#4CAF50';
      ctx.fill();
      ctx.strokeStyle = '#2E7D32';
      ctx.lineWidth = 3;
      ctx.stroke();
      
      // Add centered text
      ctx.fillStyle = 'white';
      ctx.font = '16px Arial';
      ctx.textAlign = 'center';
      ctx.fillText('Centered!', 150, 105);
   </script>
</body>
</html>

The canvas is centered on the page and contains a green circle with text −

[300x200 canvas with green circle containing "Centered!" text, horizontally centered on page]

Conclusion

Centering a canvas in HTML5 can be achieved through multiple CSS methods. Use text-align: center for simple horizontal centering, flexbox for complete positioning control, or margin: 0 auto with display: block for block-level centering. Choose the method that best fits your layout requirements and browser support needs.

Updated on: 2026-03-16T21:38:53+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements