Comprehensive Guide to CSS Table Cell Spacing

A laptop screen shows a webpage wireframe next to notes

An HTML table comprises rows and columns, where each intersection of a row and column is referred to as a table cell. Table cells can accommodate various elements such as text, images, links, buttons, and more. Considering the theme of CSS table cell spacing is crucial when exploring examples of the CSS box model, as it directly impacts the spacing and arrangement of elements within a web page.

This tutorial delves into the diverse aspects of HTML table cells, discussing their formatting.

Adding a Cell in HTML Table

Adding a cell to an HTML table involves using the pair of <td> tags. The <td> tag, representing table data, is employed to generate regular cells within HTML tables.

Syntax for creating a cell in the HTML table:

<td> This is a Table Cell </td>

Enclosed within a pair of <tr> tags, each <td> tag is utilized to define a table row. The <tr> tag denotes a table row, serving the purpose of creating rows in HTML tables.

<tr>
   <td> Cell 1 </td>
   <td> Cell 2 </td>
   <td> Cell 3 </td>
</tr>

You have the flexibility to include any quantity of cells in a table row. In the provided code, three cells are added to a single row.

The following is the fundamental structure of an HTML table. This code will generate a table featuring 3 rows, 3 columns, and a total of 9 table cells.

<table>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
    </tr>
    <tr>
        <td>Cell 4</td>
        <td>Cell 5</td>
        <td>Cell 6</td>
    </tr>
    <tr>
        <td>Cell 7</td>
        <td>Cell 8</td>
        <td>Cell 9</td>
    </tr>
</table>

Code Explanation:

  • The <table> tags are mandatory for indicating to the browser that the following markup constitutes a table in HTML. All content intended to be part of the table must be enclosed within these tags;
  • Rows within the table are specified by <tr> tags, while <td> tags are used to declare individual table cells within these rows;
  • Cells are arranged sequentially within a row. By assembling a series of cells across multiple rows, a grid-like structure of rows and columns is formed.

The displayed output of the provided code exhibits straightforward table cells devoid of any formatting, such as borders or padding. It lacks the typical table appearance as it solely relies on HTML code. To enhance its visual appeal, we’ll employ CSS styles, a topic we’ll cover in this tutorial.

Various techniques exist for formatting an HTML table cell, including adjusting cell spacing, padding, borders, background color, width, height, merging cells, and more. Let’s examine each aspect individually through code examples.

Note: Modifications will be applied to the original HTML table code mentioned earlier.

Styling HTML Table Cells with Borders

For HTML table cell borders, the CSS shorthand border property proves effective. The following CSS snippet applies a 2-pixel solid black border to each cell within the table.

<style>
 
table, tr, td{
    border: 2px solid black;
}
 
</style>

In the displayed result, the application of CSS to <table>, <tr>, and <td> elements results in the cell border being distinct from the table border. To address this, the following snippet introduces the use of the border-collapse property.

<style>
 
table, tr, td{
    border: 2px solid black;
    border-collapse: collapse;
}
 
</style>

Another noticeable factor is the cell spacing, where the cells seem closely packed, suggesting that the content is cramped. This problem can be addressed by adding some space around the text within the cells.

HTML Table Cell Spacing

A programmer codes on a laptop with HTML and CSS on screen

To achieve this, the CSS padding property comes into play. Padding allows you to assign numerical values to create spacing within cells. Let’s delve into the process.

<style>
 
table, tr, td{
    border: 2px solid black;
    border-collapse: collapse;
    padding: 16px;
}
 
</style>

Now, each HTML table cell exhibits a notable improvement compared to its previous appearance.

HTML Table Heading Definition

The <th> element is employed to designate header cells within a table, encapsulating content that will be automatically emboldened compared to standard cell text.

For incorporating headers into an HTML table, substitute <td> tags with <th> tags in the initial row. The following HTML code snippet demonstrates this approach. Adjustments are made to the original table code for clarity.

<table>
    <tr>
        <th>Heading 1</th>
        <th>Heading 2</th>
        <th>Heading 3</th>
    </tr>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
    </tr>
    <tr>
        <td>Cell 4</td>
        <td>Cell 5</td>
        <td>Cell 6</td>
    </tr>
    <tr>
        <td>Cell 7</td>
        <td>Cell 8</td>
        <td>Cell 9</td>
    </tr>
</table>

Moreover, the CSS code now incorporates the th element selector, visible in the accompanying image. This modification ensures consistent styling across the table header. 

Observe the distinction in the following output; the header text now appears bolder compared to the standard HTML table cells.

Customizing HTML Table Header Cell Appearance

I will update the appearance of the header cells by setting their background to black and altering the text color to white for enhanced visibility. This requires adding a few lines of CSS. The th element selector will be used to target and style the background of each table header cell accordingly.

th{
    background-color: #333333;
    color: #ffffff;
}

Utilize the background-color property to assign diverse backgrounds to individual cells within the HTML table.

Hint: You might consider the table being left-aligned in your browser. But what if the intention is to center-align the table?

Adjusting Dimensions of HTML Table Cells

HTML attributes width and height can directly modify the dimensions of table cells. It is important to note that altering the size of a single cell impacts the corresponding cells in its row or column due to the table’s inherent structure.

If the objective is to customize the width of a particular cell, for instance, the second cell in the second row, inline CSS should be applied to that cell to define its width property uniquely. This ensures that only the targeted cell’s dimensions are affected.

<table>
    <tr>
        <th>Heading 1</th>
        <th>Heading 2</th>
        <th>Heading 3</th>
    </tr>
    <tr>
        <td>Cell 1</td>
        <td style="width: 60%;">Cell 2</td>
        <td>Cell 3</td>
    </tr>
    <tr>
        <td>Cell 4</td>
        <td>Cell 5</td>
        <td>Cell 6</td>
    </tr>
    <tr>
        <td>Cell 7</td>
        <td>Cell 8</td>
        <td>Cell 9</td>
    </tr>
</table>

Now, regarding the height aspect. The height functions similarly; it affects the entire row’s height. However, this feature proves exceptionally beneficial in scenarios where you aim to display a paragraph within your table and wish to define specific width or height for that particular row or column.

Table Cell Spanning: Merging Over Rows or Columns

A modern workspace with an iMac displaying code

In HTML, similar to functionalities available in spreadsheet programs such as Excel or Google Sheets, cells within a table can be merged or extended across multiple rows or columns. This is achieved using the colspan and rowspan attributes. 

<table>
    <tr>
        <th>Name</th>
        <th>Favourite Subjects</th>
    </tr>
    <!-- Student 1 -->
    <tr>
        <td rowspan="3">Sara</td>
        <td>History</td>
    </tr>
    <tr>
        <td>Zoology</td>
    </tr>
    <tr>
        <td>Math</td>
    </tr>
    <!-- Student 2 -->
    <tr>
        <td rowspan="3">Zeeshan</td>
        <td>Psychology</td>
    </tr>
    <tr>
        <td>Science</td>
    </tr>
    <tr>
        <td>Programming</td>
    </tr>
</table>

In the provided HTML table code, the table structure remains consistent; the rowspan attribute is employed for the designated cell that requires spanning, specifying the number of rows.

For spanning a cell across multiple columns, the procedure remains unchanged. Utilize the colspan attribute for the intended cell, as illustrated in the following code.

<table width="50%">
    <tr>
        <th>Name</th>
        <th colspan="3">Favourite Subjects</th>
    </tr>
    <!-- Student 1 -->
    <tr>
        <td>Sara</td>
        <td>History</td>
        <td>Zoology</td>
        <td>Math</td>
    </tr>
    <!-- Student 2 -->
    <tr>
        <td>Zeeshan</td>
        <td>Psychology</td>
        <td>Science</td>
        <td>Programming</td>
    </tr>
</table>

Conclusion

By understanding the different aspects of HTML table cells, you can effectively customize and control the appearance of your HTML tables. With this knowledge, you can enhance the readability and functionality of your web pages, ensuring a smooth user experience.