Spelchan.com Logo

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

Chapter 3.12: Styling Tables

Tables are the final thing that we are going to want to style. Most of the attributes necessary for controlling the look of the table have already been presented, it is just a matter of looking at them from a table perspective. One of the more important parts of a table is the grid. By default, there is no grid on HTML tables. You may have recalled the depreciated border statement was used earlier to demonstrate grids with borders, but you should never use depreciated attributes in new code. More importantly, you should be removing depreciated attributes from old code whenever you have a chance to refactor that code.

We have already covered the border property of the CSS Box Model, which is what would be used to generate borders around the cell and tables. This is how we do the grid. By default, there are no borders around the table and the cells. To add a border around the table, you simply need to add a border attribute the CSS definition for the table.

<table style="border:1px solid black">
    <!-- table data goes here -->
</table>

This, however, does not add borders around the individual cells. To do this, you need to specify the border around the cells. As you don’t want to have to specify a style, or even a class, for each table header or table data element, you will want to have a CSS style rule for this. If you want more than one table in your document and want to have those tables use different styles, like is done with the demo for this chapter, you need to have different classes. This is a good time to remind you that when defining a class, you can also specify how to style an embedded element. Here is the code for setting up a bordered table using the borderedCells class.

.borderedCells, .borderedCells th, .borderedCells td {
    border: 1px solid black;
}

The problem with this is that there are gaps around the borders. Thinking about how the box model works, it is clear this is probably related to the margin attribute. Eliminating margins results in thicker border lines so does not solve our problem unless we want thicker lines. To get around this problem, CSS has a special attribute called border-collapse. This attribute has two potential values collapse or separate. The collapse option will collapse the borders of adjoining blocks into a single border, while separate is the default option.

border-collapse: collapse;

The image below shows the different table styles.

default table
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4
table with border
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4

borders around cells
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4
borders around cells
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4

As with the lists, it is possible to use the state :nth-child to control the state of certain rows in the table. This is done by applying the state to the tr element, such as:

tr:nth-child(2n) {
    background-color: lightblue;
}
<!-- if applying to a class for a table, would be… -->
.tableRColoring tr:nth-child(2n) {
background-color: lightblue;
}

Likewise, if we wanted to set the column of a table to have a different style, we would do so using the td element. If we wanted to count the column header as part of the style changes, th would also need to be included. Here is an example:

td:nth-child(3n), th:nth-child(3n) {
    background-color: lightgreen;
}
And we can apply both. One interesting thing happens when we apply both. The column styling takes priority over the row styling. Why is this? As we mentioned earlier, the style that is applied to an element is based on the most specific set of rules. As the td and th elements are part of a row, they are more specific than the tr element. It is still possible to have rows take priority over columns by creating a specific rule for the row that includes the td/th elements as part of it’s definition, such as the following:
.tableRCColoring, .tableRCColoring td, .tableRCColoring th {
    border: 1px solid black;
    border-collapse: collapse;
}

.tableRCColoring td:nth-child(3n),
.tableRCColoring th:nth-child(3n) {
    background: lightgreen;
}

.tableRCColoring tr:nth-child(2n),
.tableRCColoring tr:nth-child(2n) td {
    background-color: lightblue;
}

The image below shows the different coloring options, with the code for this being in the repository under tableStyleDemo.

row styling
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4
column styling
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4

rows and columns styled
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4
rows and columns styled, prioritize rows
first colsecond colthird colfourth col
R2C1R2C2R2C3R2C4
R3C1R3C2R3C3R3C4
R4C1R4C2R4C3R4C4

This covers the basics for CSS, so it is time to apply your new knowledge in a project. Any projects utilizing the material covered will help cement the knowledge, but for a fun project we are going to create a clone of a classical text game in the next section.

Chapter contents

Chapter 3 Contents

3.1 CSS Basics Cheat Sheet

A cheat sheet covering the basics of CSS.

3.2 What is CSS

A brief look at what CSS is and how it came to be.

3.3 Setting up Style Sheets

A look at the three different ways to add style to your document.

3.4 Blocks, Spans, and Selectors

Setting the style for different types of elements.

3.5 Classes

An easy way of having multiple styles on the same element type and across different elements.

3.6 Colours

The use of named colors, the RGB color model, and creating RGB colors.

3.7 Hexadecimal color codes

Using hexadecimal color codes and other color models.

3.8 Fonts

Controlling what text looks like.

3.9 Box Model

The box model controls how elements are positioned within their layout box.

3.10 Link styles

Anchor element states and turning a link into a button.

Christmas Bonus

Hunt the Santa logo
A total conversion of Hunt the Webbus with a Christmas Theme.

3.11 Styling lists

Lists can have style applied to their layout, placement, and bullet.

3.12 Styling tables

Tables have a grid as well as rows and columns that can be controlled.

3.13 Webbus project

The project for this chapter is a variation of a classic text game

3.14 Webbus solution

My solution for the Webbus project

3.15 Christmas Total Conversion

Converting the Webbus game into a Christmas game.


← previous section
Styling Lists
next section →
Webbus Project
Table of Contents