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:
table
element specifies the entire table. In the
above table, we've set its border
attribute to be 1 (try
changing it to 10 to see what it looks like). The border
is deprecated in HTML5, and we are only using it as a
quick and dirty way to see the individual cells of the table. You
should instead use CSS to set the visual design of a table.tr
element specifies a row of a table. tr
element, you can put th
and
td
elements. The td
element specifies a cell
within a row, and the th
element specifies a group of
cells to be a header.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:
td
element of each row.td
element to see what
happens. The rows will have a different number of cells, and so the right
edge will look "ragged". td
element. If you
replace those with the th
element then that first row will
stand out: it will be bold and centered within the cell.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 colspan
elements 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.
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.
The caption
element assigns a caption for table, e.g.:
Best Picture | Year |
---|---|
Moonlight | 2016 |
Spotlight | 2015 |
Birdman | 2014 |
12 Years a Slave | 2013 |
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 1 | 5 |
Question 2 | 8 |
Question 3 | 9 |
Total Score | 22 |
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.
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:
|
|
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.
Modern programming editors, such as a
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
.
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
HTML-CSS-JS-prettify
package from the list.
A quick way to do this is to type prettify
. After selecting the
package, it should be automatically downloaded and installed.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
.
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.