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.
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.
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.
The image below shows the different table styles.
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
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:
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:
The image below shows the different coloring options, with the code for this being in the repository under tableStyleDemo.
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
first col | second col | third col | fourth col |
---|---|---|---|
R2C1 | R2C2 | R2C3 | R2C4 |
R3C1 | R3C2 | R3C3 | R3C4 |
R4C1 | R4C2 | R4C3 | R4C4 |
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.
A cheat sheet covering the basics of CSS.
A brief look at what CSS is and how it came to be.
A look at the three different ways to add style to your document.
Setting the style for different types of elements.
An easy way of having multiple styles on the same element type and across different elements.
The use of named colors, the RGB color model, and creating RGB colors.
Using hexadecimal color codes and other color models.
Controlling what text looks like.
The box model controls how elements are positioned within their layout box.
Anchor element states and turning a link into a button.
A total conversion of Hunt the Webbus with a Christmas Theme.
Lists can have style applied to their layout, placement, and bullet.
Tables have a grid as well as rows and columns that can be controlled.
The project for this chapter is a variation of a classic text game
My solution for the Webbus project
Converting the Webbus game into a Christmas game.