How To Create HTML Tables (Very Simple Examples)

Welcome to a quick tutorial and examples on how to create HTML tables, and I suppose that everyone has seen tables everywhere online. But what is actually happening behind the scenes? How are tables created in HTML?

  • A raw basic HTML table only consists of 3 elements.
    • <table> The entire table itself.
    • <tr> Table row.
    • <td> Table cell.
  • Thus, we can create a simple HTML table as such:
    • <table>
    • <tr> <td>Cell A</td> <td>CellB</td> </tr>
    • </table>

That covers the quick basics, but there are more to HTML tables – Read on for more examples!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

[web_stories_embed url=”https://code-boxx.com/web-stories/create-html-tables/” title=”How To Create HTML Tables (Simple Examples)” poster=”https://code-boxx.com/wp-content/uploads/2022/03/STORY-HTML-20230505.webp” width=”360″ height=”600″ align=”center”]

 

TABLE OF CONTENTS

Download & Notes HTML Tables Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

HTML TABLE EXAMPLES

All right, let us now get into the HTML table examples in this section.

 

1) CREATING A BASIC HTML TABLE

1-basic.html
<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
  </tr>
  <tr>
    <td>Jon Doe</td>
    <td>jon@doe.com</td>
  </tr>
  <tr>
    <td>Joy Doe</td>
    <td>joy@doe.com</td>
  </tr>
</table>
Name Email
Jon Doe jon@doe.com
Joy Doe joy@doe.com
  • <table> The table itself.
  • <tr> represents a table row.
  • <th> represents a header cell.
  • <td> is a normal table cell.

Yes, these 4 tags are all you need to create a basic HTML table… Although <th> is kind of optional.

 

2) TABLE SECTIONING – HEAD, BODY, FOOT

2-section.html
<style>
thead { background-color:lightsalmon }
tbody { background-color:lightyellow }
tfoot { background-color:lightblue }
</style>

<table>
  <!-- (A) HEAD SECTION -->
  <thead>
    <tr> <th>Head 1</th> <th>Head 2</th> </tr>
  </thead>
 
  <!-- (B) BODY SECTION -->
  <tbody>
    <tr> <td>Cell 1</td> <td>Cell 2</td> </tr>
    <tr> <td>Cell 3</td> <td>Cell 4</td> </tr>
  </tbody>
 
  <!-- (C) FOOT SECTION -->
  <tfoot>
    <tr> <td>Cell 5</td> <td>Cell 6</td> </tr>
    <tr> <td>Cell 7</td> <td>Cell 8</td> </tr>
  </tfoot>
</table>
Head 1 Head 2
Cell 1 Cell 2
Cell 3 Cell 4
Cell 5 Cell 6
Cell 7 Cell 8

This one should be pretty self-explanatory as well, we are simply grouping the table rows <tr> into sections with <thead>, <tbody> and <tfoot>. Although this is completely optional, it is pretty useful for styling and organization purposes.

 

 

3) COLUMN GROUPING

3-col-group.html
<table>
  <!-- (A) COLUMN GROUPING -->
  <colgroup>
    <col span="2" style="background-color:lightsalmon">
    <col style="background-color:lightyellow">
    <col style="background-color:lightblue">
  </colgroup>

  <!-- (B) ROWS AS USUAL -->
  <tr>
    <th>First Column</th>
    <th>Second Column</th>
    <th>Third Column</th>
    <th>Fourth Column</th>
  </tr>
  <tr>
    <td>First Cell</td>
    <td>Second Cell</td>
    <td>Third Cell</td>
    <td>Fourth Cell</td>
  </tr>
</table>
First Column Second Column Third Column Fourth Column
First Cell Second Cell Third Cell Fourth Cell

The previous example demonstrated how to group rows together, and yes, we can group columns too.

  • Define a column group <colgroup> right after the opening <table> tag.
  • Accompany it with placing the corresponding column <col> tags inside – How many columns to span and the styles.

 

4) SPANNING ACROSS ROWS & COLUMNS

4-span.html
<table>
  <tr>
    <th>First Column</th>
    <th colspan="2">Span 2 columns</th>
  </tr>
  <tr>
    <td rowspan="2">Span 2 rows</td>
    <td>A</td>
    <td>B</td>
  </tr>
  <tr>
    <td>C</td>
    <td>D</td>
  </tr>
</table>
First Column Span 2 columns
Span 2 rows A B
C D
  • To span a cell across multiple columns, we add a colspan attribute.
  • To span a cell across multiple rows, we add a rowspan attribute.

Easy?

 

 

5) TABLE CAPTION

5-table-caption.html
<table>
  <caption>Animals</caption>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>Doge</td>
    <td>Good boy.</td>
  </tr>
  <tr>
    <td>Cate</td>
    <td>Evil one.</td>
  </tr>
</table>
Animals
Name Description
Doge Good boy.
Cate Evil one.

The <caption> tag adds a description to the table, and it must be right under the main <table> tag. Not very useful though… Most people will just use a header <h1> tag.

 

6) TABLE-IN-TABLE

6-table-in-table.html
<table>
  <tr>
    <td>First Cell</td>
    <td>Second Cell</td>
  </tr>
  <tr>
    <td>Third Cell</td>
    <td>
      <table>
        <tr><td>TABLE-IN-TABLE</td></tr>
        <tr><td>ANOTHER ROW</td></tr>
      </table>
    </td>
  </tr>
</table>
First Cell Second Cell
Third Cell

TABLE-IN-TABLE
ANOTHER ROW

Finally, for you guys who are curious – Yes, we can put a table inside a table.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

SUMMARY – TABLE TAGS

Tag Description
<table> The “root” table tag.
<tr> Table row.
<th> Header cell.
<td> Table cell.
<thead> Head section.
<tbody> Body section.
<tfoot> Foot section.
<colgroup> Column group, accompanied by <col>.
<col> A column group definition.
<caption> Table caption. Must be the first child after the <table> tag.

 

SUMMARY – ATTRIBUTES

Attribute Description
style Defines the style. Can actually be added to almost any HTML tag.
colspan How many columns a cell <th> or <td> spans across.
rowspan How many rows a cell <th> or <td> spans across.
span For <col> tags. How many columns the group spans across.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

HTML Table Cheat Sheet (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!