HTML tables allow for data to be arranged into rows and columns using the table
tag, which contains the tr
tag for rows and the td
tag for data cells that are not in the header row. For the header row th
tags are used for the data since they will contain header data.
This is a simple table. It has two columns with four rows of data. It also has a header row that names each of the columns to show what the data represents.
Grade | Class |
---|---|
A- | Math |
B | English |
B+ | History |
C+ | Science |
The following represents the code to build the table above (ignore the formatting; we’ll look at that while doing our table formatting). We are only interested in the HTML structure – a row with two columns of header data, and four rows containing two columns of data.
Grade
Class
A-
Math
B
English
B+
History
C+
Science
Table Tag
The first thing we need is the table
tag which defines everything within it to be part of the table.
Table Rows
Then inside the table
tag we create a series of rows using the tr
tag. For this table we need the row that will be the table header and then we need four additional rows. Each row begins with tr
and ends with /tr
.
Table Data in Columns
Inside the first set of tr /tr
tags we will use th
tags for the header data (column descriptors). There are two columns of data, so we need two sets of th /th
tags.
Finally, inside each row we want two columns of data. So we need two sets of td /td
inside each row.
th
or td
tag can contain just about any kind of data your table needs such as numbers, text, bullet lists, images, another table, etc.
This gives us our table with default formatting, meaning minimum table size, minimum cell spacing, no borders, etc. Next, we will look at the table and cell attributes for doing some table formatting to change these defaults and make the table slightly more appealing (like the example table at the top of the page).
Vertical Header
Lastly, if necessary, the header row can instead be a header column. This is called a vertical header. All we need to do is place a th
tag first thing within each tr / tr
set to create the header column.
First Name
Mark
James
Emily
Last Name
Gonzalez
Harbour
Watson
Grade
A-
C
A+
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.
Why 1SMARTchicken?
See More →
Thanks for your support!
HTML Books
All are affiliate links. Thank you for your support!
HTML NOTES:
- In our HTML section the term “tag” and “element” are often used interchangeably to refer to both the tag used to create a page element and the element created by the tag (
p
tag =p
element = paragraph on the page) - HTML5 is not case sensitive; so
P
is the same asp
,ÂH1
is the same ash1
- Global attributes can be used with all HTML tags and are therefore not mentioned on every tag page
- To write clean, readable HTML code, it is best to use indentation whereas elements within elements are indented (tabbed or spaces) to create something that looks like a project outline
- The browser will automatically remove any extra spaces and lines in your HTML code when the page is displayed
- Double quotes or single quotes can be used around HTML attribute values, but when the attribute value itself contains one form of quote, it will be necessary to use the other around the attribute
Feedback
If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.