Table in HTML: tags, height/width, caption, cell padding/spacing, row/column span

HTML tables organize chaotic webpage data into perfectly aligned rows and columns, turning messy lists into professional layouts.

8 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Gridlock in Your Portfolio

You want to showcase your top three college projects on your portfolio website. You want to display the project titles, the semesters you built them, and your final grades side by side. But when you type them out using ordinary text tags, they line up haphazardly, looking unorganized and confusing. A recruiter visiting your site cannot align the titles with the grades. How do we neatly snap this structured information into clean horizontal rows and vertical columns that look completely professional?

Theory

The Graph Paper Notebook

Think back to your school days using a graph paper notebook for math. When you draw a chart, you utilize pre-printed grid boxes. Each individual box holds one number, and every horizontal line represents a full record. You can even combine adjacent boxes if you need a wider title row. An HTML table operates exactly like graph paper. It creates a rigid structural mesh where content cannot slide out of place, keeping your grid impeccably aligned.

Theory

Anatomy of an HTML Table

In web engineering, an HTML table is an element used to display data in a grid format. It is created using the outer <table> tag container. Inside this container, every individual row is initialized using the <tr> (table row) tag. Inside a row, you define individual cells using either <th> (table header) for bold centered titles, or <td> (table data) for regular text cells. You can also specify an optional <caption> tag immediately after opening the table to act as a title.

At a glance

Core HTML elements used to assemble data grids.

TagPurposeDefault Visual Behavior
<table>Main outer wrapper for the entire grid layoutInvisible boundaries without borders
<caption>Provides an accessible title above the gridCentered text at the very top
<tr>Declares a horizontal container for cellsActs like a block level line breaker
<th>Creates prominent column headings inside a rowBold text and automatically centered
<td>Creates a standard data cell holding data valuesRegular left aligned text content

Practical

A Standard Projects Grid Layout

<table width="100%" height="150">
  <caption>My Semester Projects</caption>
  <tr>
    <th>Project Name</th>
    <th>Semester</th>
    <th>Grade</th>
  </tr>
  <tr>
    <td>Portfolio Site</td>
    <td>Semester 2</td>
    <td>A+</td>
  </tr>
  <tr>
    <td>Library App</td>
    <td>Semester 1</td>
    <td>A</td>
  </tr>
</table>

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

Spacing Controls and Cell Merging

To refine your grid presentation, you must master spacing and merging. The cellpadding attribute controls internal spacing: the buffer distance between a cell border and the text inside it. The cellspacing attribute dictates external spacing: the clear gap between adjacent cell borders. When you need to merge cells, use colspan to stretch a single cell horizontally across multiple columns, or use rowspan to stretch a cell vertically downwards across multiple rows.

Quiz

You are creating a financial report table for a lab assignment and want a single 'Total Expenses' label cell to span across exactly three columns in the bottom row. Which attribute combination is valid?

  1. <td rowspan="3">
  2. <td colspan="3">
  3. <td size="3">
  4. <td width="300%">
Show the answer

<td colspan="3">

The colspan attribute expands a cell horizontally across columns, making colspan="3" the correct choice for combining three columns. rowspan merges cells vertically down across rows, which would ruin the row flow here.

Think first

Predicting Spacing and Padding Behavior

Mentally examine a table where the text is sticking tightly to the cell grid borders, but the grid boxes themselves have large white gaps between them. How do you fix this? Think before tapping.

Show the answer

You need to increase the value of the cellpadding attribute to push the cell walls away from the text inside, creating internal breathing room. At the same time, you must decrease the value of the cellspacing attribute to reduce the gaps between the cell borders and pull the grid blocks closer together.

Watch out

The Broken Floating Cell Trap

A critical mistake in university semester practical exams is mismatching cell counts across rows. If your header row contains three <th> tags, but your data row only contains two <td> tags, the browser renders an ugly empty cavity at the end of the line. Remember: if you use colspan="2" on a single cell, that cell counts as 2 towards that row's total cell allotment. Always ensure every single row adds up to the identical cell sum!

Formula

Exam Answer Recipe: Secure Table Construction

When a theory question asks you to write code for a complex grid layout, execute these programmatic steps for full marks:

  • Open the main <table> wrapper and insert a <caption> immediately.
  • Construct your first row using <tr> with <th> tags for columns headers.
  • Populate data rows sequentially using matching <tr> and <td> containers.
  • Embed explicit rowspan or colspan modifiers inside the tags if cells merge.
  • Close elements cleanly in structural order: close the data cell, close the row, then close the table.

Summary

Key takeaways

  • The <table> tag acts as the parent container, while <caption> sets an overhead description.
  • Rows are built with <tr>, containing <th> for headers and <td> for structural data cells.
  • Cellpadding sets internal padding room, while cellspacing controls external border spacing.
  • Colspan merges adjacent cells across columns, while rowspan extends a cell down across rows.
  • Every row must explicitly account for the same total cell count to maintain structural integrity.
  • Memory Hook: Rows go wide, columns go deep, count every cell or your grid structure will weep.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Basics of Web and HTML

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Table in HTML: tags, height/width, caption, cell padding/spacing, row/column span · Designing and Publishing Website (SEC-02 option B) · Gri-Learn