Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3932420
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:27:24+00:00 2026-05-19T23:27:24+00:00

Is it possible to fix the width and height of an HTML5 canvas element?

  • 0

Is it possible to fix the width and height of an HTML5 canvas element?

The usual way is the following :

<canvas id="canvas" width="300" height="300"></canvas>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:27:25+00:00Added an answer on May 19, 2026 at 11:27 pm

    The canvas DOM element has .height and .width properties that correspond to the height="…" and width="…" attributes. Set them to numeric values in JavaScript code to resize your canvas. For example:

    var canvas = document.getElementsByTagName('canvas')[0];
    canvas.width  = 800;
    canvas.height = 600;
    

    Note that this clears the canvas, though you should follow with ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height); to handle those browsers that don’t fully clear the canvas. You’ll need to redraw of any content you wanted displayed after the size change.

    Note further that the height and width are the logical canvas dimensions used for drawing and are different from the style.height and style.width CSS attributes. If you don’t set the CSS attributes, the intrinsic size of the canvas will be used as its display size; if you do set the CSS attributes, and they differ from the canvas dimensions, your content will be scaled in the browser. For example:

    // Make a canvas that has a blurry pixelated zoom-in
    // with each canvas pixel drawn showing as roughly 2x2 on screen
    canvas.width  = 400;
    canvas.height = 300; 
    canvas.style.width  = '800px';
    canvas.style.height = '600px';
    

    See this live example of a canvas that is zoomed in by 4x.

    var c = document.getElementsByTagName('canvas')[0];
    var ctx = c.getContext('2d');
    ctx.lineWidth   = 1;
    ctx.strokeStyle = '#f00';
    ctx.fillStyle   = '#eff';
    
    ctx.fillRect(  10.5, 10.5, 20, 20 );
    ctx.strokeRect( 10.5, 10.5, 20, 20 );
    ctx.fillRect(   40, 10.5, 20, 20 );
    ctx.strokeRect( 40, 10.5, 20, 20 );
    ctx.fillRect(   70, 10, 20, 20 );
    ctx.strokeRect( 70, 10, 20, 20 );
    
    ctx.strokeStyle = '#fff';
    ctx.strokeRect( 10.5, 10.5, 20, 20 );
    ctx.strokeRect( 40, 10.5, 20, 20 );
    ctx.strokeRect( 70, 10, 20, 20 );
    body { background:#eee; margin:1em; text-align:center }
    canvas { background:#fff; border:1px solid #ccc; width:400px; height:160px }
    <canvas width="100" height="40"></canvas>
    <p>Showing that re-drawing the same antialiased lines does not obliterate old antialiased lines.</p>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to fix the height of a row (tr) on a table?
Is there a way to fix the height of a certain window like it
I am trying to fix a bug on an ActiveX control. Is it possible
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Try this: html, body { height: 100%; } #container { float: left; width: 50px;
how to change font-size with respect to its container. <div id=container style=width: 100%; height:
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.