HTML 5: How to keep a canvas at the center of the browser?

Hi Experts,

I am working on a HTML web page and want to keep a canvas element at the center of the browser.  Below is the code I tried
<!DOCTYPE html>
<html style="height: 99%; width: 99%; padding: 0; margin:0">
<head>
    <title>Knockwave</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="js/jquery-1.11.3.js"></script>
    <script type="text/javascript" src="js/phaser.js"></script>
</head>
<body style="width: 100%; height: 100%; margin: 0; padding: 0; border: 5px blue solid; text-align: center; vertical-align: central; vert-align: middle; mso-vertical-page-align:center">
<canvas style="background-color: red; width: 50%; height: 50%; position: relative; vert-align: middle; -webkit-align-self:center"></canvas>
</body>
</html>

Open in new window


However, it doesn't work. The horizontal alignment is ok, but not vertical.
Can you help me to find a way to do that?
Thank you!
huangs3Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Here is one way of doing it with absolute positioning.

<!DOCTYPE html>
<html>
<head>
    <title>Knockwave</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="js/jquery-1.11.3.js"></script>
    <script type="text/javascript" src="js/phaser.js"></script>
<style type="text/css">
* {
  box-sizing: border-box;
}
html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}
body {
  border: 5px solid blue;
}
canvas {
  position: absolute;
  width: 50%;
  height: 50%;
  top: 25%;
  left: 25%;
  background-color: red;
}
</style>
</head>
<body>
  <canvas></canvas>
</body>
</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.