I am having trouble placing the javascript code, the snake game, in the center of the screen. It just jumps down a bit, how can i fix this?
the code was too long to put here,
CSS: https://pastebin.com/6N9H7dJd
JAVASCRIPT: https://pastebin.com/PBENS81s
I want the snake canvas to be place in the middle of the screen, not centered only in the y axel, but also in the x axel.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link href="main.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" type="image/x-icon" href="icon/favicon.ico">
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans:100,100i,400,400i,700,700i" rel="stylesheet">
<title> MOCKO </title>
</head>
<body>
<center>
<div id="yes">
<h2>Score: <span id="score"></span> </h2>
<div id="stage" style="position:relative"></div>
<script src="snake.js"></script>
<script type="text/javascript">
var game = new Game(20, 10, "stage", "score");
</script>
</div>
</center>
</body>
</html>
It is hard for me to test out your code since I do not have access to a demo/minimal example, but theoretically speaking, we could centre the parent div that contains the snake game, as well as the score, using flexbox .
The div with the id yes
is the parent container, whereas the score and the div containing the snake game are the flex children.
<body>
<div id="yes">
<h2>Score: <span id="score"></span> </h2>
<div id="stage" style="position:relative"></div>
<script src="snake.js"></script>
<script type="text/javascript">
var game = new Game(20, 10, "stage", "score");
</script>
</div>
</body>
On your .css,
#yes {
display: flex;
justify-content: center;
align-items: center;
flex-direction:column;
}
The properties justify-content
and align-items
with the value of center
will center the flex children along the line and in the cross-axis respectively, thus placing them on the centre of the page. The property flex-direction
with the value of column
aligns the children from top to bottom.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.