No history yet

Advanced Tables

Advanced Table Structuring

You already know how to create a basic table with rows and columns. Now, let's move beyond the simple grid. HTML provides several attributes to control the structure, spacing, and appearance of your tables, which are crucial for organising complex data clearly. These are older, presentation-focused attributes, but they are essential for the CBSE curriculum.

Controlling Space and Borders

Four key attributes of the <table> tag allow you to fine-tune its overall layout. The border attribute sets the thickness of the outer border and the lines between cells. A value of "0" hides them completely, while a higher number makes them thicker.

The width attribute controls the table's size, which can be a fixed number of pixels (like width="400") or a percentage of the available screen space (like width="80%"). Using percentages helps your table adapt to different screen sizes.

Next are cellpadding and cellspacing. They both manage space, but in different ways. cellpadding controls the margin inside each cell, pushing the content away from the cell's border. Think of it as padding within a box.

cellspacing controls the distance between the cells themselves. Increasing this value creates wider gaps in your table's grid. Think of it as the thickness of the grid lines separating the boxes.

Here is how these attributes look together in a single <table> tag.

<table border="2" width="100%" cellpadding="10" cellspacing="5">
  <tr>
    <td>Row 1, Cell 1</td>
    <td>Row 1, Cell 2</td>
  </tr>
  <tr>
    <td>Row 2, Cell 1</td>
    <td>Row 2, Cell 2</td>
  </tr>
</table>

Merging Cells

Standard tables have a regular grid structure, but sometimes you need a single cell to span across several columns or rows. This is common for creating headers or grouping related data. HTML uses two attributes for this: colspan and rowspan.

Use colspan to merge cells horizontally (across columns). Use rowspan to merge cells vertically (down rows).

The colspan attribute, short for 'column span', is added to a <td> or <th> tag. The value you give it determines how many columns that cell should occupy. For example, colspan="3" makes a single cell take up the space of three normal cells in that row.

When you use colspan, remember to reduce the number of <td> or <th> tags in that specific <tr>. If one cell now spans three columns, you need two fewer cells in that row to complete the grid.

<table border="1" width="100%">
  <tr>
    <th colspan="2">Student Details</th>
  </tr>
  <tr>
    <td>Aarav</td>
    <td>Class 10A</td>
  </tr>
  <tr>
    <td>Priya</td>
    <td>Class 10B</td>
  </tr>
</table>

Similarly, the rowspan attribute lets a cell extend downwards across multiple rows. If you set rowspan="2" on a cell in the first row, that cell will occupy its own space plus the space of the cell directly below it in the second row.

When using rowspan, you must omit the corresponding <td> or <th> tag from the subsequent row(s) that the spanning cell covers. This prevents the table layout from breaking.

<table border="1" width="100%">
  <tr>
    <th>Subject</th>
    <th>Marks</th>
  </tr>
  <tr>
    <td rowspan="2">Science</td>
    <td>Theory: 75</td>
  </tr>
  <tr>
    <td>Practical: 22</td>
  </tr>
    <tr>
    <td>Maths</td>
    <td>98</td>
  </tr>
</table>

Alignment and Styling

You can also control where the content appears within a cell. The align attribute handles horizontal alignment and can be set to left, center, or right.

The valign attribute manages vertical alignment. Its common values are top, middle, and bottom. These attributes can be applied to an entire row (<tr>) or to individual cells (<td>, <th>).

To change the background colour, you can use the bgcolor attribute. This can be applied to the whole <table>, a single <tr>, or an individual <td> or <th> to highlight specific data points.

<table border="1" width="100%" cellpadding="10">
  <tr bgcolor="#f2f2f2">
    <th>Item</th>
    <th>Quantity</th>
    <th>Status</th>
  </tr>
  <tr>
    <td>Apples</td>
    <td align="center">50</td>
    <td rowspan="2" valign="middle" align="center" bgcolor="#e8f5e9">In Stock</td>
  </tr>
  <tr>
    <td>Oranges</td>
    <td align="center">30</td>
  </tr>
</table>

These advanced table attributes give you precise control over data presentation, a key skill for your board examinations. Now, let's test your understanding.

Quiz Questions 1/6

What is the primary function of the cellpadding attribute in an HTML table?

Quiz Questions 2/6

A table is designed to have four columns. If you apply colspan="3" to the first cell (<td>) in a row, how many more <td> tags do you need in that same row (<tr>)?