Tables

HTML provides a number of tags for creating tables of data. For example:

Best Picture Year
Moonlight 2016
Spotlight 2015
Birdman 2014
12 Years a Slave 2013

Here are the basic tags within the table:

Issue to Consider with Tables

Sample Table: Dogs

Here is a copy of a sample table about dogs:

  Knocky Flor Ella Juan
Breed Jack Russell Poodle Streetdog Cocker Spaniel
Age 16 9 10 5
Owner Mother-in-law Me Me Sister-in-law
Eating Habits Eats everyone's leftovers Nibbles at food Hearty eater Will eat till he explodes

A few things to notice:

Spanning Multiple Rows and Columns

In more complex tables, it is common to have rows that span multiple columns, and columns that span multiple rows. This can be down using the rowspan and colspanelements as in this table:

Category
Movie Year Best Picture
Moonlight 2016
Spotlight 2015
Birdman 2014

rowspan and colspan can be used to make some very complex tables, and it can take some trial-and-error to get things just right. Try changing the attributes in the above table to see what can be done.

Styling Columns

Setting styles for individual columns of a table can be tricky because you must set the style for the apporpriate cell of every column. To make this easier, HTML provides the col and colgroup elements. These are put at the top of a table before any rows. For example, the first column of this table is given a yellow background using col and colgroup:

Year Best Picture
Moonlight 2016
Spotlight 2015
Birdman 2014

Change the order of the two col elements if you want the second column to be the yellow one.

Captions

The caption element assigns a caption for table, e.g.:

Academy Award Winners
Best Picture Year
Moonlight 2016
Spotlight 2015
Birdman 2014
12 Years a Slave 2013

More Structure for tables

The thead element is a table element that specifies the head element of a table. The is usually the first row, or the first few rows, of the table. For example:

Academy Award Winners
Best Picture Year
Moonlight 2016
Spotlight 2015
Birdman 2014
12 Years a Slave 2013

In the thead element of this table the td tag was used (and not the th tag). So thead has no default visual style, which means here we are using it to clarify the structure of our table.

In addition to thead, there is also the tbody element and the tfoot element. The tbody element specifies the main body of the table, and tfoot specifies the footer. Here's an example that shows the use of all three elements:

Question Score
Question 15
Question 28
Question 39
Total Score22

Again, visually, the thead, tbody, and tfoot don't do anything. They are, by default, semantic elements that give the table more structure. This structure is useful when designing tables, and may also be useful to web browsers, or special tools like screen readers.

Nested Tables

You can, if need be, nest tables within tables. This quickly gets complicated, and so it is generally not a good idea unless there is a very clear reason why you want to do it.

As an example, here are two of the previous tables nested inside another table:

Two Tables in the Same Row
Academy Award Winners
Best Picture Year
Moonlight 2016
Spotlight 2015
Birdman 2014
12 Years a Slave 2013
Question Score
Question 15
Question 28
Question 39
Total Score22

The outer table consists of 1 row and 2 columns, and so has no border. Thus it makes the two other tables appear side-by-side.

Again, this probably not a wise idea! If you actually want two tables to appear side-by-side like this, then it is probably better to position them with CSS instead of a table (since CSS is far more flexible). Or, in some cases, it might make sense to structure this as one big table.

Editor Support for HTML

Modern programming editors, such as a Sublime 3, can provide a lot of helpful tools and tricks for writing HTML (and CSS, and JavaScript.

Here's one trick that you can use in Sublime 3 in its HTML mode: type an element name like code ( without any angle brackets), and then press TAB. It will expand into a matching opening/closing take for code.

Prettifying HTML (and CSS and JavaScript)

There are also various useful plugins you can install for helping with HTML. For example, the HTML-CSS-JS-prettify package will automatically re-format your HTML (or CSS, or JavaScript) to look "pretty". This usually means that it will change line breaks and indentation into a standard format. To install HTML-CSS-JS-prettify in Sublime 3, do the following:

After installing HTML-CSS-JS-prettify, you can now "prettify your HTML by right-clicking with the mouse, selecting HTML/CSS/JS Prettify, and then selecting Prettify Code.

Emmet: Shortcuts on Steroids

If you are writing a lot of HTML by hand, then you might want to install the Emmet package as follows:

With Emmet installed, there are now many more shortcuts for quickly creating HTML and CSS. For example, if you type "ul" and then press TAB (or ctrl-E), it should automatically expand into an unordered list structure. It is much faster than type all the tags and angle-brackets by hand.

The Emmet cheat sheet lists many other abbreviations and shortcuts you can use to quickly create HTML on the fly. This may seem like a lot to learn --- and it is! But you are writing a lot of HTML by hand, then learning these shortcuts can save you time overall.