0%
Exam Sidemann Logo Light Mode

HTML Tables

Learn how to structure and style data with HTML for structure and CSS for style.

Why Are We Even Writing Code? The Big Picture

Before we even type a single bracket, let's understand why we're doing this. At its core, code is simply a set of instructions that we give to a computer. In our case, we're giving instructions to a web browser (like Google Chrome, Firefox, or Safari).

Websites are just documents that browsers read. The language we use to create the structure of these documents—the headings, the paragraphs, the images, and, yes, the tables—is called HTML (HyperText Markup Language). Think of HTML as the skeleton of a webpage. It gives the content its basic shape and meaning, but it doesn't make it look pretty.

That's where CSS (Cascading Style Sheets) comes in. CSS is the language we use to style our HTML. It's like the paint, furniture, and decorations for our house. While HTML builds the walls and rooms, CSS adds the color, controls the spacing, and makes everything visually appealing.

So, when we build a table, we use HTML to create the structure of the rows and columns, and we use CSS to add borders, set colors, and align the text to make it readable and nice to look at.

The Core Concepts of HTML Tables: Building Your Grid

Imagine you have a list of your friends and their phone numbers. You could write it in a paragraph, but it would be messy. A much better way to organize this information is in a grid with columns and rows, just like a spreadsheet. In the world of websites, this grid is called a table.

To build this grid, we need a few key HTML "tags." A tag is just a keyword wrapped in angle brackets, like <table>, that tells the browser what kind of content it's looking at. Most tags come in pairs: an opening tag (<table>) and a closing tag (</table>). Everything in between is part of that element.

Here are the four essential building blocks for any table:

  • <table>: This is the main container, the master box for your entire table. When the browser sees this tag, it knows, "Okay, everything between <table> and </table> belongs together as one single table."
  • <tr> (Table Row): Think of this as creating a new horizontal row in your grid. Every time you want to start a new row, you'll use a <tr> tag. All the cells for that row will go inside it.
  • <th> (Table Header): These are the special cells that act as the titles for your columns. They tell you what kind of information is in that column (e.g., "Name," "Email," "Price"). To make them stand out, browsers automatically make the text inside <th> elements bold and centered.
  • <td> (Table Data): These are the regular cells of your table. This is where you put your actual data—the names of your friends, the prices of products, etc. By default, the text in these cells is plain and aligned to the left.

The Trick to Building Tables: Think in Rows!

The most common point of confusion for beginners is how to put these tags together. The secret is to build your table one row at a time.

Before you write any code, just ask yourself: "How many rows do I need?" Let's say you need a header row and two rows of data. That's three rows in total. So, the first thing you would write is the skeleton for three rows:

HTML
<table>
  <tr>
    <!-- Cells for the first row (header) will go here -->
  </tr>
  <tr>
    <!-- Cells for the second row will go here -->
  </tr>
  <tr>
    <!-- Cells for the third row will go here -->
  </tr>
</table>

Once you have this structure, the rest is just about filling in each <tr> with the right cells (<th> for the header row, <td> for the data rows). This "rows first" approach makes building any table much more manageable.

Styling Your Tables: From Plain to Polished with CSS

As we mentioned, HTML on its own is very plain. A default table has no borders and looks like a jumble of text. To make it look like an actual table and customize its appearance, we use CSS. We write our CSS rules inside a <style> tag, which is usually placed at the top of the HTML file.

Let's break down the most common styling techniques for tables.

Border Collapse: Cleaning Up Your Lines

By default, if you add borders to table cells, the browser puts a border around each individual cell, resulting in a clunky, "double border" look. To fix this, we use a simple CSS property:

border-collapse: collapse;: This powerful instruction tells the browser to merge the borders of adjacent cells into a single, clean line. This instantly makes your table look more modern and professional. The other value, separate, is the default where each cell has its own border.

Text Alignment and Bolding: Controlling Your Content

You have precise control over where the text sits inside each cell.

  • Horizontal Alignment: The text-align property controls the alignment from left to right. You can set it to left, right, or center. Remember, <th> elements are centered by default, while <td> elements are left-aligned by default.
  • Vertical Alignment: The vertical-align property controls the alignment from top to bottom. You can set it to top, middle, or bottom. The default for all table cells is middle.
  • Bolding: Header (<th>) text is bold automatically. If you want to make text in a regular data cell (<td>) bold, you can use the CSS rule font-weight: bold;.

Setting Widths: Creating Responsive Layouts

You can control how wide your columns are. It is highly recommended to use percentages for widths. Why? Because a percentage is flexible. A column with width: 50%; will take up half of the table's total width, whether you're viewing it on a giant desktop monitor or a small smartphone screen. This helps create "responsive" tables that adapt to different screen sizes.

Let's Get Practical: Table Examples Deconstructed

The best way to learn is by doing. Let's break down each of the five examples to see these concepts in action.

Example 1: A Basic 3x3 Table

The Goal: Create a simple table with a header row and two data rows to display names and ages.

HTML
<style>
  /* These styles apply to the table with class="example1" */
  table.example1 {
    border-collapse: collapse; /* Merges borders into single lines */
    width: 100%;             /* Makes the table span the full width of the page */
  }
  /* This applies to all header (th) and data (td) cells in the table */
  table.example1 th, table.example1 td {
    border: 1px solid black; /* Adds a thin, solid, black border */
    padding: 8px;            /* Adds some space between the text and the border */
    text-align: left;        /* Aligns all text to the left */
  }
  /* This applies ONLY to the header (th) cells */
  table.example1 th {
    background-color: #f2f2f2; /* Gives the header a light grey background */
    font-weight: bold;         /* Ensures the header text is bold */
  }
</style>
<table class="example1">
  <!-- First Row: The Header -->
  <tr>
    <th style="width: 40%;">First Name</th>
    <th style="width: 40%;">Last Name</th>
    <th style="width: 20%;">Age</th>
  </tr>
  <!-- Second Row: John's Data -->
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>30</td>
  </tr>
  <!-- Third Row: Jane's Data -->
  <tr>
    <td>Jane</td>
    <td>Smith</td>
    <td>25</td>
  </tr>
</table>

Breakdown:

  • We use the "rows first" method: one <tr> for the header, one for John, and one for Jane.
  • The header row uses <th> to make the titles stand out.
  • The data rows use <td> for the actual information.
  • The CSS collapses the borders, sets the table width to 100%, and adds borders and padding to every cell.
  • We give the header cells a distinct background color to visually separate them.

Example 2: A Table Without Entries

The Goal: Create a table structure with empty cells, which can be useful as a layout tool or a template.

HTML
<style>
  table.example2 {
    width: 100%;
    border-collapse: collapse;
  }
  table.example2 td {
    border: 1px solid #ddd; /* A light grey border */
    height: 50px;           /* Sets a specific height for the cells */
  }
</style>
<table class="example2">
  <tr>
    <td></td> <!-- An empty cell -->
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

Breakdown: This demonstrates that cells don't need to contain anything. We've created a 2x3 grid of empty boxes. The key new piece of CSS here is height: 50px;, which makes each row 50 pixels tall.

Example 3: Introducing colspan (Spanning Columns)

The Goal: Create a header cell that spans across more than one column. Imagine you want a single "Name" header that sits above both the "First Name" and "Last Name" columns. You can't do this with a normal <th>. This is where the colspan attribute comes in. It allows a single cell to stretch horizontally.

HTML
<table class="m3-table">
  <thead>
  <tr>
    <!-- This header cell will span across 2 columns -->
    <th colspan="2">Name</th>
    <th>Age</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <!-- Now we need two cells to fill the space under the "Name" header -->
    <td>John</td>
    <td>Doe</td>
    <td>30</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>Smith</td>
    <td>25</td>
  </tr>
  </tbody>
</table>

Breakdown:

  • colspan="2" tells the "Name" header: "Behave as if you are two cells wide."
  • Because the first row's header cell now takes up two column spots, the subsequent data rows (<tr>) still need two separate <td> cells to correctly align underneath it.

Example 4: Mastering rowspan (Spanning Rows)

The Goal: Create a header cell that spans across more than one row. This is the vertical equivalent of colspan. The rowspan attribute allows a single cell to stretch downwards across multiple rows. This is perfect for when a single category applies to multiple rows of data.

HTML
<table class="m3-table">
  <thead>
  <tr>
    <th>Category</th>
    <th>Details</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>Name</td>
    <td>John Doe</td>
  </tr>
  <tr>
    <!-- This data cell will span down across 2 rows -->
    <td rowspan="2">Contact</td>
    <td>Email: john.doe@example.com</td>
  </tr>
  <tr>
    <!-- This row only needs one cell, because the "Contact"
         cell is still taking up the first column's space -->
    <td>Phone: 123-456-7890</td>
  </tr>
  </tbody>
</table>

Breakdown:

  • rowspan="2" on the "Contact" cell tells it: "Behave as if you are two rows tall."
  • The first data row is normal: a category and a detail cell.
  • The second row starts with the tall "Contact" cell. We only need to add one <td> for the email, because the "Contact" cell is filling the first slot.
  • The third row is "below" the second row, but the "Contact" cell is still occupying that first column space. So, again, we only need to add one <td> for the phone number.

Example 5: Combining It All with Advanced Styling

The Goal: Build a complex, well-styled table that uses both colspan and rowspan for a professional layout.

HTML
<style>
  table.example5 {
    border-collapse: collapse;
    width: 100%;
    text-align: center;
  }
  table.example5 th, table.example5 td {
     border: 1px solid #a0a0a0;
     padding: 10px;
  }
  table.example5 .header {
    background-color: #4CAF50; /* A green background */
    color: white;
  }
  table.example5 .contact-header {
      background-color: #f2f2f2;
      vertical-align: middle;
  }
   table.example5 .notes {
      text-align: left;
      font-style: italic;
      background-color: #ffffdd;
   }
</style>
<table class="example5">
  <!-- Row 1: A main header spanning all 3 columns -->
  <tr>
    <th colspan="3" class="header">User Information</th>
  </tr>
  <!-- Row 2: A side header and two column headers -->
  <tr>
    <th rowspan="2" class="contact-header">Contact Details</th>
    <th style="width: 35%;">Name</th>
    <th style="width: 35%;">Age</th>
  </tr>
  <!-- Row 3: Data cells aligning under the headers from Row 2 -->
  <tr>
    <td>John Doe</td>
    <td>30</td>
  </tr>
  <!-- Row 4: A footer cell spanning all 3 columns -->
  <tr>
    <td colspan="3" class="notes">Additional Notes: This is a sample table.</td>
  </tr>
</table>

Breakdown: This example elegantly combines all the concepts:

  • The top header "User Information" uses colspan="3" to span the entire table width.
  • The "Contact Details" side-header uses rowspan="2" to sit alongside two rows of data ("Name" and "Age" in row 2, and "John Doe" and "30" in row 3).
  • The final notes cell uses colspan="3" again to create a footer.
  • The CSS uses classes like .header and .notes to apply very specific styles to different parts of the table, giving you complete creative control.

By starting with this fundamental understanding of HTML for structure and CSS for style, you now have the tools to create clear, organized, and visually appealing tables for any data you need to display.

Contextual AI Tutor

Hello! Ask me a question about the section you are reading.
Tables Lists Text Elements Input & Forms Images & Videos HTML Intro