Adding the Boilerplate HTML and CSS
The main boilerplate for an HTML5 file is minimal. You get a valid HTML file with a <canvas> element inside of a container centered on the page, as shown in Listing 1-1.
Listing 1-1: Boilerplate game HTML
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Alien Invasion</title>
<link rel="stylesheet" href="base.css" type="text/css" />
</head>
<body>
<div id='container'>
<canvas id='game' width='320' height='480'></canvas>
</div>
<script src='game.js'></script>
</body>
</html>
The only external files so far are the base.css file, an external style sheet, and a nonexistent game.js file that will contain the JavaScript code for the game. Drop the HTML from Listing 1-1 into a new directory, and call it index.html.
In base.css you need two separate sections. The first is a CSS reset. CSS resets make sure all elements look the same in all browsers and any per-element styling and padding is removed. To do this, the reset sets the size of all elements to 100% (16 pixels for fonts) and removes all padding, borders, and margins. The reset used is the well-known Eric Meyer reset: http://meyerweb.com/eric/tools/css/reset/.
You can simply copy the CSS code verbatim to the top of base.css.
Next, you need to add two additional styles to the CSS file, as shown in Listing 1-2.
Listing 1-2: Base canvas and container styles
/* Center the container */ #container { padding-top:50px; margin:0 auto; width:480px; } /* Give canvas a background */ ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access