Spelchan.com Logo

From Scratch Web Games: A Beginners Guide to Game Development using HTML, CSS, and JavaScript

Chapter 6.6: Grid templates

It is common to create a website by first sketching out a rough layout of that design. A simple website might look something like this:

Design of sample site

This could then be broken into a rough grid like so:

Sample site with grid

If we were to assign a short code for each section of the site we may have a list like the following:

  • bg – logo bug
  • hd – main heading
  • sb – Sidebar
  • nv – menu bar (NaVigation)
  • ar – article (content)
  • ft – page footer with copyright

Combining the grid with the short codes we would have this:

bghdhdhdsbsb
nvnvnvnvsbsb
ararararsbsb
ftftftftftft

We can then use this table to put together a simple template for our grid. This makes the layout of the separate divisions a task that is handled by the grid layout. We just need to add the appropriate division tags that make up the parts of the page. This requires that we make sure the divs being added have the appropriate information in them so CSS knows where to place the items.

To set up the grid, we would use the following CSS:

.gridPage {
display: grid;
grid-template:
"bg hd hd hd sb sb"
"nv nv nv nv sb sb"
"ar ar ar ar sb sb"
"ft ft ft ft ft ft";
grid-template-columns: 5em repeat(5, 1fr);
}

Notice that we are using grid-template-columns to also control the size of the columns. This is only needed if you have some elements that need to be a specific size. The template should be specified before the columns are specified.

For this section’s demo, we are taking advantage of semantic tags, which work great for this type of page. We will cover these tags in Chapter 8 but the demo does explain it’s use of these tags in the article.

Each element either needs a tag that can be altered uniquely to fit into place or a class that lets you apply the grid-area information to the div you are placing. The grid-area attribute simply tells the section which short code we used in the template so the grid knows where to place them. This has the additional advantage that you don’t need to specify the divs in a particular order as the grid-area lets grid know where to put things. Here is the CSS for a couple of the page elements:

nav {
grid-area: nv;
background: #003;
color: silver;
height: 2em;
align-content: center;
}
.sidebar {
grid-area: sb;
background: #030;
font-size: larger;
color: #00BB00;
}

To then use the layout in your page you would simply have the grid template div and then each of the elements for the page inside of it.

<div class="gridPage">
<div class="bug">
<a href="http://spelchan.com/index.php">HOME</a>
</div>
<aside class="sidebar">
<h2>Sidebar</h2>
<p> this is your typical sidebar, with information or maybe links</p>
<ul>
<li>sidebar item 1</li>
<li>sidebar item 2</li>
<li>sidebar item 3</li>
<li>sidebar item 4</li>
<li>sidebar item 5</li>
</ul>
</aside>
<!-- additional components would go here -->
</div>

Here is the sample website fit into our page

Semantic web

Even though we are not covering this topic until chapter 8, I have decided that this demo would be using some of the semantic web tags. These are simply tags that are div tags but with names that are related to the type of content that is supposed to go in them. For example, this main area of the page is using a <article> tag as it represents an article portion of a web page.

The sidebar beside this article, would use something called an <aside> tag as it is material that goes along with the main article. Note that I did not redefine the aside tag but instead created a more specific sidebar class which is used in the aside.

The header for this page uses a modified version of the <header> tag. If you look at the source code, you will notice that I accidentally placed the header after this section, yet when the page is generated it is in the correct location. This is because the grid-area attribute is used to place the item in the appropriate area of the grid.

The menu bar uses the <nav> tag which is short for navigation. Sadly, the links are not real links but the point is that you can easily define an area for your navigation menus.

Grid using Templates Demo
Copyright ©2025 Billy D. Spelchan. All rights reserved.

Chapter contents

Chapter 6 Contents

6.1 Layout Cheat Sheets

This chapter's summary cheat sheet.

6.2 The Layout problem

Why layout is complicated for web pages.

6.3 Display blocks

The basic display types.

6.4 Flexible Boxes

A box that contains other components and sizes them to fit the available space.

6.5 Grid

A flexible grid where you can lay out your design.

6.6 Grid Templates

Using templates to make laying out components in a grid easy.

6.7 Multiple columns

Using the columns attributes to easily allow for multiple columns.

6.8 Floating

Having images (or other blocks) have text wrap around them.

6.9 Position

There are other ways of placing elements by using the position attribute.

6.10 Media Queries

Altering CSS based on the properties of the device the page is being rendered on.

6.11 Project: Tic-Tac-Toe

The project for this chapter.

6.12 Project Solution

Solution for this chapter's project

← previous section
Grid
next section →
Multiple Columns
Table of Contents