Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot center align canvas

I Have been googling for solutions to no avail. I just want to center-align my canvas. I have tried float="center", text-align="center", margin-top: 100px but my stage didnt align center in the browser. Any help is appreciated.

<body onLoad="Main();">
<canvas id="Shooter" width="320" height="480" align="center">
Your browser does not support the HTML5 canvas tag.
</canvas>

like image 978
herblackcat Avatar asked Dec 14 '22 07:12

herblackcat


2 Answers

You could center align it by:

#Shooter {
  margin: auto;
  display: block;
}

Make sure the parent container have 100% width.

Here's a pen. http://codepen.io/asim-coder/pen/aNpWoB

like image 113
Asim K T Avatar answered Dec 28 '22 17:12

Asim K T


I suggest you remove the width, height and align tags from canvas. Instead add a style region to your html and define a css-style for canvas:

<style>
    canvas {
        padding-left: 0;
        padding-right: 0;
        margin-left: auto;
        margin-right: auto;
        display: block;
        width: 320px;
        height: 480px;
    }
</style>

Your canvas would look like this:

<canvas id="Shooter">
like image 37
チーズパン Avatar answered Dec 28 '22 17:12

チーズパン