完整的例子如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
canvas {
background-color: aqua;
width: 300px;
height: 500px;
position: absolute; /*重要的一步是设置绝对定位*/
margin:auto; /*将四周边距都设为auto自动*/
left: 0; /*离包含元素的距离均设为零*/
right: 0;
top: 0;
bottom: 0;
}
</style>
</head>
<body>
<canvas></canvas>
</body>
</html>