Popular Searches
Popular Course Categories
Popular Courses

Tables

28 mins HTML5 & Semantic Markup

Tables in React JS

Course: React JS Training

Topic: Tables

Course Link: React JS Training

Demo Link: Register for a Course Demo


1. Introduction: Why Tables Matter

Some information is naturally two-dimensional. A price list has items running down one side and prices down the other. An exam report has students on the left and subjects across the top. When you meet data like this, paragraphs and bullet lists feel clumsy, because the reader has to guess which value belongs to what. The <table> element exists exactly for this situation: it tells the browser that the content has a grid shape, and it lets you label the rows and columns so that every value keeps its meaning.

Tables also matter later in this course. When you start building interfaces with React, a component that shows products, orders or users is very often a table rendered from data held in an array. If you are comfortable with <table>, <tr>, <th> and <td> in plain HTML, you will recognise the same structure instantly when JavaScript starts generating it for you.

One warning before we begin: a table is for data, not for arranging the overall page. In the early days of the web people laid out entire pages with tables, and that habit still survives in old tutorials. Modern pages use CSS for layout and reserve tables for what they were designed to do — presenting tabular information. We return to this point near the end of the page.

2. What Is an HTML Table?

In simple words:

An HTML table is a set of rows placed one under another, where each row is divided into cells, so that related values line up in columns and can be compared at a glance.

Think of a school attendance register or a spreadsheet. The horizontal strips are rows. The vertical strips are columns. The small box where a row meets a column is a cell, and each cell holds one value — a name, a number, a date. The first row usually holds the headings that explain what each column means.

The browser does the lining up for you. You only declare what each piece of content is — "this is a table, this is a row, this is a heading cell, this is a normal cell" — and the browser arranges everything into a grid. That is why tables are a semantic tool: you describe the meaning, and the presentation follows.

3. Key Terms of Table Markup

Before writing examples, get comfortable with the small family of tags involved. There are only a handful, and each has one job.

TagShort forWhat it holds
<table>tableWraps the entire grid
<caption>captionA title describing the whole table
<thead>table headGroups the heading rows
<tbody>table bodyGroups the main data rows
<tfoot>table footGroups summary rows such as totals
<tr>table rowOne horizontal row of cells
<th>table headerA heading cell, shown bold and centred by default
<td>table dataA normal data cell

Alongside the tags, three attributes do the structural fine-tuning:

AttributeUsed onPurpose
colspan<th> or <td>Stretches one cell across several columns
rowspan<th> or <td>Stretches one cell down several rows
scope<th>Declares whether a heading applies to a column or to a row

4. Syntax and Structure of a Table

A table is built strictly from the tags above, nested in a fixed order. Here is the skeleton that almost every well-written table follows:

<table>
  <caption>Weekly study plan</caption>
  <thead>
    <tr>
      <th>Day</th>
      <th>Topic</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Monday</td>
      <td>HTML tables</td>
    </tr>
  </tbody>
</table>

The nesting rules are worth memorising, because the browser is strict about them:

  • Inside <table>, the only allowed children are <caption>, column-related tags such as <colgroup>, the row-section tags <thead>, <tbody>, <tfoot>, and bare <tr> rows. Never place plain text or other elements directly inside <table>.
  • Inside a row-section tag, or directly inside <table>, rows are always <tr>.
  • Inside a <tr>, cells are always <th> or <td>. Anything you want to show must live inside a cell.
  • <caption>, if you use one, is written as the first child of <table>.
  • Use <th> for headings and <td> for values; the difference is meaning, not styling.

5. How a Table Is Built, Step by Step

When you face any new table, follow the same routine every time. The routine keeps rows aligned and prevents the most common mistakes.

  1. Open <table> and add a <caption> that states what the data shows.
  2. Decide the columns first. Each column gets exactly one <th> in the heading row inside <thead>.
  3. Add one <tr> inside <tbody> for every record — one student, one product, one day.
  4. Inside each record row, put the values in <td> cells, always in the same order as the headings.
  5. If the table needs totals or a summary, add them in <tfoot>, written after <tbody>.
  6. Close all tags in reverse order and check that every row accounts for the same number of columns.

The mental model to keep in mind: one row of data equals one <tr>. If your data has twelve records, you will write twelve <tr> elements. When React later loops over an array, it will produce exactly this — one <tr> per item — so the habit you build here transfers directly.

6. Worked Example: A Two-Column Price List

Let us build the simplest useful table from scratch: a stationery price list with two columns.

<table>
  <caption>Stationery price list</caption>
  <thead>
    <tr>
      <th>Item</th>
      <th>Price in rupees</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Notebook</td>
      <td>40</td>
    </tr>
    <tr>
      <td>Pen pack</td>
      <td>60</td>
    </tr>
    <tr>
      <td>Geometry box</td>
      <td>120</td>
    </tr>
  </tbody>
</table>

The browser renders this roughly as follows:

ItemPrice in rupees
Notebook40
Pen pack60
Geometry box120

Notice what happened without any CSS at all. The caption appears above the grid, the <th> cells are bold and centred, the <td> cells are plain and left-aligned, and every value has landed in its correct column. You declared meaning; the browser supplied the arrangement.

7. Grouping Rows with thead, tbody and tfoot

Nothing forces you to use the three row-section tags — bare <tr> rows also render. But grouping rows adds meaning, and meaning pays off in three practical ways:

  • Styling in one place. CSS can target <thead> or <tfoot> as a whole, so you style all heading rows at once instead of cell by cell. This becomes valuable the moment your CSS module work begins.
  • Smarter printing and scrolling. When a long table is printed and spills over several pages, browsers can repeat the heading section on each page, so the reader never loses track of which column is which.
  • Clearer structure for tools. Screen readers, scripts and crawlers can treat the heading, body and summary regions as separate areas instead of guessing.

Here is a small table that uses all three sections:

<table>
  <caption>Savings at the end of each month</caption>
  <thead>
    <tr>
      <th>Month</th>
      <th>Amount saved</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>3,000</td>
    </tr>
    <tr>
      <td>February</td>
      <td>4,500</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th>Total</th>
      <td>7,500</td>
    </tr>
  </tfoot>
</table>

The <tfoot> row announces "this line is a summary, not another record". Write the sections in the order <thead>, <tbody>, <tfoot>; in modern HTML the foot section belongs after the body, even though older HTML tutorials sometimes showed it before the body.

8. Spanning Cells with colspan and rowspan

Real tables are rarely perfect rectangles. Sometimes one label covers several columns, such as a "Result summary" banner over a "Pass" column and a "Fail" column. Sometimes one label covers several rows, such as a category name beside a group of related items. The colspan and rowspan attributes handle both cases.

The colspan attribute

<tr>
  <th colspan="2">Result summary</th>
</tr>
<tr>
  <td>Pass</td>
  <td>Fail</td>
</tr>

The heading cell spans two columns, so the row below must supply exactly two cells to fill the grid. If you wrote three cells under a two-column span, the table would grow an unwanted third column.

The rowspan attribute

<tr>
  <th rowspan="2">Science</th>
  <td>Physics</td>
  <td>78</td>
</tr>
<tr>
  <td>Chemistry</td>
  <td>84</td>
</tr>

Here "Science" occupies the first column of two rows. In the second row you therefore begin directly with "Chemistry" — you must not write another cell for the first column, or the whole row shifts one place to the right. Visually, "Science" stretches down beside both subject rows.

Counting cells after a span

The one rule that keeps complex tables honest: every row must account for the same total number of columns. A normal cell counts as one; a cell with colspan="3" counts as three; a cell with rowspan="2" keeps occupying its column in the next row as well. If rows start drifting sideways in the browser, count the spans row by row — one miscounted cell is almost always the cause.

9. Captions and Heading Scope

Writing a useful caption

The <caption> answers the question a reader asks before reading anything: what is this data? A good caption names the subject and the period. Compare "Table 1" with "Household expenses for March, in rupees" — only the second one tells you anything on its own. The caption must be the first child of <table>, and the browser displays it above the grid by default.

Declaring scope on headings

Most tables have headings along the top, but many also have headings down the left side — for example, when each row starts with a student name. The scope attribute says which direction a heading governs:

<thead>
  <tr>
    <th scope="col">Subject</th>
    <th scope="col">Marks</th>
  </tr>
</thead>
<tbody>
  <tr>
    <th scope="row">Aditi</th>
    <td>92</td>
  </tr>
  <tr>
    <th scope="row">Rohan</th>
    <td>85</td>
  </tr>
</tbody>

Two details are happening here. First, scope="col" marks the top headings as column labels, and scope="row" marks "Aditi" and "Rohan" as row labels. Second, notice that "Aditi" is a <th>, not a <td> — it is a heading for the row, even though it sits at the start of the row rather than at the top of the table. This pairing is what lets a screen reader announce "Aditi, 92" instead of just "92", so the listener always knows which person a mark belongs to. The later Accessibility page of this module builds further on this idea.

10. Worked Example: A Monthly Expense Report

Now assemble everything into one realistic table: a caption, column headings with scope, row headings for each category, and a summary row in <tfoot>.

<table>
  <caption>Household expenses for March, in rupees</caption>
  <thead>
    <tr>
      <th scope="col">Category</th>
      <th scope="col">Week 1</th>
      <th scope="col">Week 2</th>
      <th scope="col">Week 3</th>
      <th scope="col">Week 4</th>
      <th scope="col">Month total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Groceries</th>
      <td>3,200</td>
      <td>2,800</td>
      <td>3,500</td>
      <td>3,100</td>
      <td>12,600</td>
    </tr>
    <tr>
      <th scope="row">Travel</th>
      <td>1,200</td>
      <td>900</td>
      <td>1,500</td>
      <td>800</td>
      <td>4,400</td>
    </tr>
    <tr>
      <th scope="row">Entertainment</th>
      <td>1,000</td>
      <td>1,500</td>
      <td>800</td>
      <td>1,200</td>
      <td>4,500</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th scope="row">Total</th>
      <td>5,400</td>
      <td>5,200</td>
      <td>5,800</td>
      <td>5,100</td>
      <td>21,500</td>
    </tr>
  </tfoot>
</table>

The rendered result:

CategoryWeek 1Week 2Week 3Week 4Month total
Groceries3,2002,8003,5003,10012,600
Travel1,2009001,5008004,400
Entertainment1,0001,5008001,2004,500

Study the arithmetic of the markup: the heading row has six cells, each body row has six cells, and the foot row has six cells — one <th> plus five <td>. Every row accounts for the same six columns, so the grid stays straight. If you preferred one wide summary cell instead of a broken-down total row, you could write <td colspan="5"> followed by a single grand total cell in the <tfoot> — both patterns are common in real reports.

11. Tables Are for Data, Not Layout

This is the rule that separates current practice from old habits. Use a table when the content itself is a grid; avoid one when you are merely trying to position things on the page.

SituationUse a table?Better approach
Exam marks, price list, timetableYesIt is genuine tabular data
Order items with quantities and amountsYesA grid of values with column headings
Page columns such as menu beside contentNoCSS layout with Flexbox and Grid, covered in the CSS module
A simple sequence of stepsNo<ol> or <ul>, which keep the list meaning intact
A row of picture cardsNoCSS grid or flex layout, so the cards can reflow on small screens

The reasons against layout tables are practical, not just stylistic. A screen reader enters "table mode" when it meets a <table> and starts announcing rows and columns, which becomes nonsense if the table only holds navigation links. Layout tables also resist responsiveness: on a narrow phone screen, a data table can still make sense, but a page skeleton built from table cells cannot reflow into a single column the way a CSS layout can.

12. Common Mistakes with Tables

Most broken tables in beginner code come from a handful of repeating errors. Learn to spot them early:

  • Text placed directly inside <table> or <tr>. Text must live inside <th> or <td>. Stray text confuses the browser, which lifts it out and paints it above the table, wrecking the layout.
  • Rows that cover different numbers of columns. After adding colspan or rowspan, recount the columns in every row; one forgotten span skews all later rows.
  • Using <th> everywhere just to get bold text. Bold styling belongs to CSS. <th> changes the meaning of a cell, so reserve it for real headings.
  • Row sections in the wrong order. Keep the source order <thead>, then <tbody>, then <tfoot>.
  • Wrapping rows in unrelated tags. A <tr> may not sit inside a <div>, and a <div> may not sit directly inside <table> or <tr>. The table tags nest only among themselves.
  • Leaving out the caption. Without <caption>, neither the reader nor a screen reader knows what the numbers refer to.

Here is a small broken example worth recognising on sight:

<table>
  <tr>
    Items and prices listed here   <!-- wrong: text outside a cell -->
  </tr>
</table>

The fix is always the same: wrap the text in a <td> (or <th> if it is a heading) so the browser receives a proper cell.

13. Why Semantic Tables Help Browsers and People

Because you used the real table tags rather than improvised boxes, several capabilities come for free:

  • Screen readers can move cell by cell and speak the matching row and column headings along with each value — the behaviour that scope supports.
  • Users can select the table and paste it into a spreadsheet, and many spreadsheet programs rebuild the grid automatically.
  • CSS can address whole regions at once, styling every heading row through <thead> instead of chasing individual cells.
  • Tools and crawlers can identify the data and its headers directly from the markup, without guessing from visual position.

This is the same philosophy that runs through the whole module: pick elements by meaning, not by appearance. The semantic tags page later in this module generalises the idea to whole-page regions such as articles and navigation; tables apply it to grids of data. When the meaning is in the markup, presentation, accessibility and automation all follow without extra effort.

14. Advanced Usage: Column Groups with colgroup and col

So far we have worked row by row. HTML also lets you describe the table by columns, using <colgroup> and its self-closing partner <col>. A <col> represents one column of the table, and a <colgroup> bundles columns that share something in common. These elements carry no content of their own — they exist so that styles and attributes can be applied to whole columns in one place.

<table>
  <caption>Stationery stock</caption>
  <colgroup>
    <col />
    <col span="2" />
  </colgroup>
  <thead>
    <tr>
      <th>Item</th>
      <th>In stock</th>
      <th>On order</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Notebook</td>
      <td>50</td>
      <td>20</td>
    </tr>
  </tbody>
</table>

The first <col> stands for the "Item" column. The second <col> uses span="2" to stand for the two quantity columns together. Once the columns are declared, CSS can target them — for example, to give the first column a wider width or a background colour — without touching any individual cell. When you reach the CSS module, remember this hook: styling a column through <colgroup> is cleaner than adding a class to every cell in that column by hand.

Two placement rules apply. <colgroup> goes immediately after <caption> and before <thead>. And the number of columns the <col> elements describe must match the real number of columns in the rows, otherwise the mapping drifts.

15. Advanced Usage: Rich Content Inside Cells

A cell is not limited to plain text. Because <th> and <td> are normal containers, they may hold links, images, short lists, form controls and even other nested elements — as long as the flow-level elements sit inside the cell and not directly inside the row. This is what turns a table from a static grid into a realistic interface piece, such as an order list where each row ends with a status badge or an action link.

<table>
  <caption>Recent orders</caption>
  <thead>
    <tr>
      <th>Order</th>
      <th>Items</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>#1042</td>
      <td>
        <ul>
          <li>Keyboard</li>
          <li>Mouse pad</li>
        </ul>
      </td>
      <td>Shipped</td>
    </tr>
    <tr>
      <td>#1043</td>
      <td>
        <ul>
          <li>Monitor stand</li>
        </ul>
      </td>
      <td>Pending</td>
    </tr>
  </tbody>
</table>

Each row still accounts for exactly three columns; the second cell simply happens to contain a list instead of a single word. The same technique works for a link in a cell (<td><a href="details.html">View</a></td>) or a small image preview. Keep cell content short and related to the column heading, though — a cell that swallows half a page of content usually means the table is being pushed beyond its purpose.

16. Real-World Example: A Weekly Class Timetable

Timetables are the classic case where rowspan and colspan work together. Days run across the top, periods run down the side, and one subject may occupy two consecutive periods. Study how the spans keep the grid aligned:

<table>
  <caption>Class timetable, Monday to Wednesday</caption>
  <thead>
    <tr>
      <th scope="col">Period</th>
      <th scope="col">Monday</th>
      <th scope="col">Tuesday</th>
      <th scope="col">Wednesday</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>HTML</td>
      <td rowspan="2">JavaScript lab</td>
      <td>CSS</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>CSS</td>
      <td>HTML</td>
    </tr>
    <tr>
      <th scope="row">Lunch</th>
      <td colspan="3">Lunch break</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>React basics</td>
      <td>React basics</td>
      <td>Project work</td>
    </tr>
  </tbody>
</table>

Check the column arithmetic row by row. Rows one and two have a period cell, two ordinary cells, and one cell that spans downward — four columns accounted for in each. The lunch row has a period cell plus one cell spanning all three day columns — again four. The last row has four ordinary cells. Every row covers the same four columns, so nothing drifts. This counting habit is the single most useful skill for reading and writing real-world tables.

17. Making Tables Work on Small Screens

Tables are naturally wide, and phone screens are naturally narrow, so a little forward planning saves painful rework. The HTML itself stays the same; the thinking changes. Three honest strategies cover most beginner needs:

StrategyHow it worksBest suited for
Let the table scroll horizontallyWrap the table in a container that CSS turns into a horizontally scrollable area on narrow screensWide data tables such as reports and timetables
Keep tables narrow by designLimit each table to a few essential columns; split very wide data into two linked tablesSimple lists such as prices and contacts
Reflow on small screensCSS rearranges each row into a stacked card-like block on phonesLater, once the CSS module's media queries are covered

For now, adopt the simplest discipline: write the semantic markup correctly first, and keep columns to what the data genuinely needs. Semantic tables respond far better to whatever CSS technique you choose later, because the structure — headings, body, foot — is already explicit.

18. More Mistakes and Their Corrections

Part one listed the structural errors beginners make. Here are the mistakes that appear once people start writing realistic tables, each shown wrong and then right.

Using bold text instead of heading cells

<!-- wrong: styling fakes the meaning -->
<td><strong>Item</strong></td>

<!-- right: the meaning is declared -->
<th>Item</th>

A screen reader meeting the first version announces an ordinary cell; meeting the second, it understands a column heading. The bold look comes free with <th> anyway, and CSS can adjust it later.

Forgetting that a rowspan cell occupies the next row too

<!-- wrong: the second row repeats the spanned column -->
<tr>
  <th rowspan="2">Science</th>
  <td>Physics</td>
</tr>
<tr>
  <th>Science</th>
  <td>Chemistry</td>
</tr>

<!-- right: the second row starts at the next column -->
<tr>
  <th rowspan="2">Science</th>
  <td>Physics</td>
</tr>
<tr>
  <td>Chemistry</td>
</tr>

In the wrong version the table grows an extra column and every later row shifts. When a cell spans down, simply omit it from the following row.

Placing the caption anywhere but first

<!-- wrong: caption after the rows -->
<table>
  <thead> ... </thead>
  <caption>Price list</caption>
</table>

<!-- right: caption is the first child -->
<table>
  <caption>Price list</caption>
  <thead> ... </thead>
</table>

Browsers are forgiving here, but validators flag it and some tools ignore a misplaced caption entirely. The safe habit is to type the caption immediately after opening the table.

Building a table with no headings at all

<!-- wrong: values with no labels -->
<tr>
  <td>Notebook</td>
  <td>40</td>
</tr>

<!-- right: a heading row in thead first -->
<thead>
  <tr>
    <th>Item</th>
    <th>Price</th>
  </tr>
</thead>

Numbers without labels are unreadable to everyone — sighted users included. Every table needs at least one heading row, and the earlier "step by step" routine enforces this by putting headings first.

19. Best Practices for Writing Tables

The following checklist condenses everything so far into habits you can apply to every table you ever write:

  • Choose a table only for genuine grid data. If the content reads fine as a list, use a list.
  • Start every table with a meaningful <caption> that names the subject and period of the data.
  • Put headings in <thead> using <th>, values in <tbody> using <td>. Never fake headings with bold text.
  • Add scope="col" or scope="row" to every <th> so the direction of each heading is explicit.
  • Use <tfoot> for totals and summaries, keeping them visually and semantically separate from the records.
  • Recount columns after every span. Each row must account for the same total number of columns.
  • Declare columns with <colgroup> when a whole column needs shared styling, instead of adding classes cell by cell.
  • Keep one record per row. One student, one order, one month — one <tr>. This maps directly onto how React will later render arrays.
  • Do not nest unrelated block structures inside rows. Content belongs inside cells; rows contain only cells.

20. Practical Project: A Student Marks Report Page

Time to combine everything into one small, complete page you can build yourself. The project is a marks report for a small study group: a caption, column headings with scope, row headings per student, a subject column that spans two rows to demonstrate rowspan, and a totals row in <tfoot>.

Step 1: Plan the columns on paper

Decide the grid before typing. Columns: Student, Subject group, Theory marks, Practical marks. The subject group "Science" covers two rows (Physics and Chemistry), so its cell will use rowspan="2". Sketching this first prevents most span errors.

Step 2: Write the skeleton with caption and thead

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Study group marks report</title>
</head>
<body>
  <table>
    <caption>Study group marks, term test</caption>
    <thead>
      <tr>
        <th scope="col">Student</th>
        <th scope="col">Subject</th>
        <th scope="col">Theory</th>
        <th scope="col">Practical</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
    <tfoot>
    </tfoot>
  </table>
</body>
</html>

Notice the empty <tbody> and <tfoot> placeholders — writing the skeleton first means you never forget a section.

Step 3: Fill the body rows, applying rowspan

    <tbody>
      <tr>
        <th scope="row">Aditi</th>
        <td rowspan="2">Science</td>
        <td>78</td>
        <td>19</td>
      </tr>
      <tr>
        <th scope="row">Aditi</th>
        <td>84</td>
        <td>21</td>
      </tr>
      <tr>
        <th scope="row">Rohan</th>
        <td>Mathematics</td>
        <td>91</td>
        <td>25</td>
      </tr>
    </tbody>

The "Science" cell spans down over Aditi's second row, so that row contains only three cells: student, theory, practical. Count them — three cells plus the spanned one equals four columns, matching the heading row.

Step 4: Add the summary row in tfoot

    <tfoot>
      <tr>
        <th scope="row" colspan="2">Group average</th>
        <td>84.3</td>
        <td>21.6</td>
      </tr>
    </tfoot>

The label spans the first two columns, then two value cells follow — four columns again. The arithmetic holds across the whole table.

Step 5: Save, open and verify

  1. Save the file as marks-report.html and open it in the browser.
  2. Check that the caption sits above the grid and the heading row looks bold.
  3. Confirm that "Science" appears once, stretched beside both of Aditi's rows.
  4. Count the columns visually: every row should end at the same right-hand edge.
  5. Resize the browser window narrower and observe how the table behaves — this builds intuition for the responsive work ahead.

If any row looks shifted, the cause is almost always a miscounted span. Return to the paper sketch, recount, and fix the markup — debugging a table is nearly always arithmetic, not mystery.

21. Practice Exercises

  1. Build a two-column table of five books with their authors. Include a caption and a <thead> section, and give each heading cell a scope.
  2. Create a three-column grocery table (item, quantity, price) and add a <tfoot> row showing the bill total.
  3. Write a table where one heading cell spans three columns using colspan, then verify that the row beneath supplies exactly three cells.
  4. Reproduce a small weekly timetable where one subject occupies two consecutive periods with rowspan. Count the columns in every row before opening the browser.
  5. Take any table you have written and add a <colgroup> describing its columns, ready for column styling later in the CSS module.
  6. Deliberately break one of your tables — remove a cell from one row — then diagnose the drift in the browser and repair it. Learning to spot the symptom is as valuable as writing correct markup.

22. Interview Questions and Answers

What is the difference between th and td?

<th> marks a heading cell — a label for a column or a row — while <td> marks a data cell holding a value. The difference is semantic, not visual: browsers render <th> bold and centred by default, but the real value is that screen readers and other tools can tell labels from values. Styling differences should be adjusted with CSS, never by choosing a tag for its look.

Why do we use thead, tbody and tfoot?

They group rows into heading, body and summary regions. This gives CSS a whole region to style at once, lets browsers repeat heading rows when a long table prints across pages, and gives assistive technology and scripts a clear structure instead of an undifferentiated pile of rows. They are optional, but using them is standard practice for any real data table.

Where does the caption element go, and what is it for?

<caption> is written as the first child of <table> and gives the whole table a title, such as "Household expenses for March". It differs from a <th>: a caption describes the entire table, while heading cells label individual columns or rows. A good caption lets any reader — human or machine — understand what the data represents without reading the cells.

What do colspan and rowspan do?

colspan stretches a cell across several columns and rowspan stretches it down several rows. They let a table represent real-world grids that are not perfect rectangles, such as a banner heading over two sub-columns or a category label beside several rows. After using either, every affected row must still account for the same total number of columns.

How do you decide how many cells a row needs after a span?

Count columns, not cells. A normal cell counts as one, a cell with colspan="3" counts as three, and a cell with rowspan="2" continues to occupy its column in the next row, so that row starts one column further along. If rows drift sideways in the browser, recount the spans row by row — a miscounted span is the usual culprit.

What is the scope attribute used for?

scope on a <th> declares which cells the heading governs: scope="col" for a column heading and scope="row" for a row heading. This matters most for screen readers, which use it to announce each value together with its matching label — for example, saying a student's name along with her marks instead of reading isolated numbers.

Can a tr contain a div, or text directly?

No. A <tr> may contain only <th> and <td> cells, and text must live inside a cell. Stray text or elements inside a row confuse the browser, which pulls the content out of the grid and breaks the layout. If a cell needs richer content — a list, a link, an image — place that content inside the <td> or <th>.

Should tables be used for page layout?

No. Tables are for tabular data; page layout belongs to CSS, particularly Flexbox and Grid. Layout tables harm accessibility because screen readers enter table-reading mode and announce meaningless rows and columns, and they resist responsive design because a page skeleton built from cells cannot reflow on small screens the way a CSS layout can.

What is colgroup and when is it useful?

<colgroup>, containing <col> elements, declares the table's columns so that attributes and styles can be applied to a whole column in one place. It is useful when several cells in a column need the same width or background: instead of adding a class to every cell, you style the <col>. It sits after the caption and before the heading section.

How does this HTML table knowledge connect to React?

A React component that displays a list of records typically renders a <table> whose <tbody> is produced by mapping over an array — one <tr> per record, one <td> per field. The semantic habits from this page carry over directly: headings stay in <thead>, each array item becomes exactly one row, and keys and structure follow the same one-record-per-row discipline you practised in plain HTML.

How would you make a wide table usable on a phone?

Common approaches are letting the table scroll horizontally inside a scrollable container on narrow screens, keeping tables narrow by limiting them to essential columns, or reflowing rows into stacked blocks with CSS on small screens. The semantic structure — caption, thead, tbody — makes all of these easier, because CSS can target whole regions rather than individual cells.

Why is a table considered semantic markup?

Because each tag declares meaning rather than appearance: "this is a table", "this is a row", "this is a heading cell", "this is a value". From that meaning, the browser derives the grid layout, screen readers derive spoken structure, and tools can extract the data with its labels intact. This is the same principle behind the semantic page-structure tags covered elsewhere in this module.

23. Quick Revision Checklist

  • A table is for grid-shaped data, never for page layout.
  • Nesting order: <table> → <caption> → <colgroup> → <thead> → <tbody> → <tfoot>; rows are <tr>; cells are <th> or <td>.
  • <caption> is the first child of <table> and names the whole dataset.
  • <th> means heading, <td> means value; add scope="col" or scope="row" to every <th>.
  • colspan stretches across columns; rowspan stretches down rows; the spanned cell is omitted from the rows or columns it covers.
  • Every row must account for the same total number of columns — count spans as multiple columns.
  • One record equals one <tr>; this is exactly how React will render an array later.
  • Cells may contain lists, links and images — but always inside the cell, never directly in the row.
  • <tfoot> holds totals and summaries, written after <tbody>.
  • Debugging a crooked table is arithmetic: recount the spans row by row.

Conclusion

Tables reward precision. The tag family is small — <table>, <caption>, <thead>, <tbody>, <tfoot>, <tr>, <th>, <td> plus the colspan, rowspan and scope attributes — yet together these pieces let you describe any grid of data so that browsers, stylesheets, screen readers and spreadsheets all understand it. The discipline you practised here, one record per row and every row accounting for the same columns, is precisely the discipline a React component applies when it maps an array of data onto table rows. With tables mastered, the module moves on to media elements and then to the semantic tags that give whole pages the same clarity you have now given to grids of data.

Learn more in the React JS Training course, or register for a course demo.

Previous Next
whatsapp