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 6348761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:29:06+00:00 2026-05-24T21:29:06+00:00

The HTML5 <canvas> element does not accept relative sizes (percent) for its width and

  • 0

The HTML5 <canvas> element does not accept relative sizes (percent) for its width and height properties.

What I’m trying to accomplish is to have my canvas sized relative to the window. This is what I’ve come up with so far, but I’m wondering if there is a better way that is:

  1. Simpler
  2. Does not require wrapping the <canvas> in a <div>.
  3. Not dependent on jQuery (I use it to get the width/height of the parent div)
  4. Ideally, doesn’t redraw on browser resize (but I think that might be a requirement)

See below for the code, which draws a circle in the middle of the screen, 40% wide up to a maximum of 400px.

Live demo: http://jsbin.com/elosil/2

Code:

<!DOCTYPE html>
<html>
<head>
    <title>Canvas of relative width</title>
    <style>
        body { margin: 0; padding: 0; background-color: #ccc; }
        #relative { width: 40%; margin: 100px auto; height: 400px; border: solid 4px #999; background-color: White; }
    </style>
    <script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script>
        function draw() {
            // draw a circle in the center of the canvas
            var canvas = document.getElementById('canvas');
            var relative = document.getElementById('relative');
            canvas.width = $(relative).width();
            canvas.height = $(relative).height();
            var w = canvas.width;
            var h = canvas.height;
            var size = (w > h) ? h : w; // set the radius of the circle to be the lesser of the width or height;
            var ctx = canvas.getContext('2d');
            ctx.beginPath();
            ctx.arc(w / 2, h / 2, size/2, 0, Math.PI * 2, false);
            ctx.closePath();
            ctx.fill();
        }

        $(function () {
            $(window).resize(draw);
        });
    </script>
</head>
<body onload="draw()">
    <div id="relative">
        <canvas id="canvas"></canvas>
    </div>
</body>
</html>
  • 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-24T21:29:09+00:00Added an answer on May 24, 2026 at 9:29 pm

    The canvas width and height attributes are separate from the same canvas’s width and height styles. The width and height attributes are the size of the canvas’s rendering surface, in pixels, whereas its styles choose a location in the document where the browser should draw the content from the rendering surface. It just so happens that the default value for the width and height styles, if they’re not specified, is the rendering surface’s width and height. So you’re right about #1: there’s no reason to wrap it in a div. You can set percentage values for all of the styles on your canvas element, just like any other element.

    For #3, it’s pretty easy (and cross-browser) to get the size of things with clientWidth and clientHeight, as long as you’re not using padding on your canvas element.

    I coded up the slightly simplified version here.

    For #4, you’re right about being out of luck. It’s possible to check before setting width and height and leave the canvas alone if it doesn’t need resizing, which would eliminate some of the redraws, but you can’t get rid of all of them.

    EDIT: Portman pointed out I messed up the centering style. Updated version here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the HTML5 canvas element to draw some arcs and
How do you rotate an image with the canvas html5 element from the bottom
I keep reading about how great this new Canvas element for HTML5 is and
I am looking at using HTML5 Canvas element for my upcoming project. I want
Does the Canvas element have the equivalent of img's map , so you can
Why do we need the html5 canvas element, when the same can be achieved
I am currently developing a little sketching application based on HTML5 Canvas element. There
I've been messing around with the canvas element in html5, and this is what
Does IE6 support any HTML5 elements? <!DOCTYPE HTML> <header> <section> <header> <nav> <canvas> <video>
I'm working with the HTML5 canvas element. Let's say I have 2 ImageData objects

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.