Overview

Hello, budding developers! In today's lesson, we'll delve into creating and arranging HTML tables. HTML tables are a cornerstone tool used to present data in a structured, easily digestible format on web pages. They can bring an intricate spreadsheet, or a neatly arranged menu at a restaurant, to your webpage. This lesson will walk you through the process of creating a simple HTML table, helping you understand its various components, and teaching you how to use different attributes to enhance it.

Building the Foundation: HTML Table Basics

Understanding the structure of HTML tables is as straightforward as focusing on three fundamental building blocks — tables, rows, and cells, each represented as an HTML tag:

  • <table>: This tag commences and concludes the table.
  • <tr>: This tag marks the beginning and end of each "table row" in our table.
  • <th> and <td>: These are designed to denote the "table header" and "table data" cells in a row. The <th> tag is typically used for header cells (column titles), and <td> is used for the more conventional data cells.

For instance, let's construct a two-by-two table:

<table> 
    <tr>
        <th>Header 1</th>
        <th>Header 2</th>
    </tr>
    <tr>
        <td>Data Cell 1 on First Row</td>
        <td>Data Cell 2 on First Row</td>
    </tr>
    <tr>
        <td>Data Cell 1 on Second Row</td>
        <td>Data Cell 2 on Second Row</td>
    </tr>
</table>

This table creates a two-column structure with a header row housing "Header 1" and "Header 2," and a data row that stores "Data Cell 1" and "Data Cell 2." hence the 2 <td> elements in each <tr>.

Enhancing Table Look: Border and Width

Like a gem, tables can be polished and refined. border and width are two key attributes that assist in formatting the table.

The border outlines the thickness of the table's boundary. The border attribute value is a relative size, not a fixed unit, and the actual pixel size may vary depending on the browser's default styling. We define it as follows:

<table border="2"> 
    <!-- Table content -->
</table>

The width attribute is an essential tool to control a table's overall size. Specifically, it dictates the width of the table within a web page. We can assign it an absolute value in pixels (like px) or a relative value in percentage (%). The absolute value assigns a fixed width regardless of window size, while the percentage assigns relative width based on the browser window size.

<table width="500"> <!-- absolute value in pixels -->
    <!-- Table content -->
</table>
<table width="50%"> <!-- relative value (percentage of browser window's width) -->
    <!-- Table content -->
</table>

In the above example, the table's width is fixed at 500 pixels. Irrespective of the size of the web browser window, the table retains its width. In contrast, in the second example, the table's width is 50% of the browser window's width. If you resize the browser window, the table's width adjusts dynamically to maintain its width as half of the window's width. The width attribute gives you control over the layout and display of your HTML tables, helping you build more flexible and responsive designs.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal