Class 10Computer ScienceFull chapter

HTML Tables

Rows, columns and merged cells, exactly as the CBSE syllabus asks for them. You will learn <table>, <tr>, <th> and <td>, the presentation attributes the textbook lists, and the two attributes that cause the most confusion — colspan and rowspan.

When a Table Is the Right Choice

Quick answer A table organises data into rows and columns so values can be compared. Use it only for genuinely tabular information, not for arranging a page.

HTML stands for HyperText Markup Language, the language used to describe the structure of a web page. A table is the HTML structure that arranges information in rows (going across) and columns (going down). The box where a row and a column meet is called a cell, and a cell is the smallest unit of a table — every piece of data you display sits inside one.

Before you write a single tag, ask one question: is this data naturally tabular? Data is tabular when each row describes one record and each column describes one property that every record shares. If that is true, a reader can do two useful things: read across a row to see one complete record, and read down a column to compare the same property for many records. If neither reading makes sense, the data is not tabular and a table is the wrong tool.

Good candidates from everyday Indian school and web use include a class timetable (rows are periods, columns are days), a marks sheet (rows are students, columns are subjects), a fee structure in rupees (rows are classes, columns are terms), a train departure list on the IRCTC — Indian Railway Catering and Tourism Corporation — website (rows are trains, columns are number, name, departure and arrival), or a price list on a shopping site. In each case a column heading such as Marks or Fee (₹) applies to every value beneath it.

Poor candidates are equally easy to recognise. A block of continuous writing belongs in a <p> element. A set of navigation links or a simple list of points belongs in <ul> or <ol>. A single photograph with a line of text beneath it is not tabular at all.

There is one historical use you should know about because older books and older pages still show it. In the early years of the web, designers used tables to lay out the whole page — one cell for the banner, one for a side menu, one for the main content. This worked, but it made the code long and hard to change, and the markup no longer described what the data meant. Modern practice is to place data in tables and to control the arrangement and appearance of the page with CSS (Cascading Style Sheets), a separate language for presentation. Your syllabus still requires you to know the presentation attributes covered later in this chapter, so learn them for the paper — but state, when the question invites it, that CSS is the preferred modern method.

A last point of vocabulary that examiners like. The word row always means a horizontal line of cells and is created by one <tr> element. A column is vertical, and there is no column tag in the basic syllabus — columns are formed automatically because every row contributes one cell to each column position. This is why a table looks correct only when every row supplies the same number of column positions, a rule you will meet again when you study colspan and rowspan.

Table element &lt;table&gt; ... &lt;/table&gt; Container for the whole table; a paired (container) tag.
Row &lt;tr&gt; ... &lt;/tr&gt; tr = table row; one horizontal line of cells.
Cell &lt;td&gt; ... &lt;/td&gt; td = table data; the smallest unit of a table.
Column count rule cells per row = number of columns Every row must account for the same number of column positions.
Remember
  • A table arranges data in rows (horizontal) and columns (vertical); their meeting point is a cell.
  • Use a table only when each row is a record and each column is a shared property — a marks sheet or a timetable, not a paragraph.
  • Lists of links or points belong in &lt;ul&gt;/&lt;ol&gt;, not in a table.
  • Tables were once used for whole-page layout; modern practice uses CSS (Cascading Style Sheets) for that.
  • Rows are created by &lt;tr&gt; tags; columns are formed automatically, so every row must supply the same number of column positions.

The Structure: table, tr, th, td and caption

Quick answer A table is built from an outer table element containing tr rows, each holding th header cells or td data cells, with an optional caption as the table's title.

Four elements build every HTML table, and all four are container (paired) tags — each needs an opening tag and a closing tag.

The <table> element is the outermost container; everything belonging to the table is written between <table> and </table>. Inside it, <tr> (table row) creates one horizontal row. Inside a row, cells are written: <th> (table heading) for a heading cell and <td> (table data) for an ordinary data cell. Note the strict nesting order — cells go inside rows, and rows go inside the table. A <td> written directly inside <table> without a surrounding <tr> is invalid markup.

Here is a complete, working table you can copy.

<table border="1">
  <caption>Class 10 A — Term 1 Marks</caption>
  <tr>
    <th>Roll No</th>
    <th>Student Name</th>
    <th>Marks</th>
  </tr>
  <tr>
    <td>01</td>
    <td>Ananya Sharma</td>
    <td>89</td>
  </tr>
  <tr>
    <td>02</td>
    <td>Rohit Verma</td>
    <td>81</td>
  </tr>
</table>

Read it as a machine would. The browser meets <table> and starts a table. It meets three <tr> elements, so the table has three rows. Each row contains three cells, so the table has three columns. The result is a 3 × 3 grid with a title above it.

The th versus td distinction is one you must be able to state precisely. Both create a cell, and both are written inside a <tr>. The difference is meaning, and appearance follows from meaning: <th> marks a cell as a heading for the row or column it introduces, while <td> marks a cell as ordinary data. By default browsers render <th> text in bold and horizontally centred, and <td> text in normal weight, left-aligned. Those defaults come from the browser's own style rules, so the exact shade of boldness or spacing can differ slightly between browsers; the bold-and-centred behaviour itself is what your textbook states and what an examiner expects. The important part of a full-mark answer is that <th> conveys that the cell is a header — the bold appearance is a consequence, not the definition.

Heading cells are not restricted to the first row. A table can have row headings too, placed as the first cell of each row.

<tr>
  <th>Monday</th>
  <td>Maths</td>
  <td>Science</td>
</tr>

The <caption> element supplies a title for the whole table. Two rules matter. First, it must be written immediately after the opening <table> tag, before any <tr>. Second, a table may have only one caption. Browsers normally display the caption centred above the table. A caption is not the same as a heading cell: the caption names the table as a whole ("Term 1 Marks"), whereas a <th> names one row or one column ("Marks").

Heading cell &lt;th&gt; ... &lt;/th&gt; Bold and centred by default; marks a row/column header.
Data cell &lt;td&gt; ... &lt;/td&gt; Normal weight, left-aligned by default.
Caption &lt;caption&gt; ... &lt;/caption&gt; One per table, written just after &lt;table&gt;.
Nesting order table &gt; tr &gt; th | td A &lt;td&gt; outside a &lt;tr&gt; is invalid.
Remember
  • Nesting order is fixed: cells inside &lt;tr&gt;, rows inside &lt;table&gt;.
  • &lt;th&gt; = table heading cell, &lt;td&gt; = table data cell; both are cells, the difference is meaning.
  • Browsers render &lt;th&gt; bold and centred by default and &lt;td&gt; normal and left-aligned.
  • &lt;caption&gt; gives the whole table a title and must come immediately after the opening &lt;table&gt; tag.
  • A table may have only one caption; it is usually displayed centred above the table.
  • Heading cells may also start each row (row headings), not only the first row.

Presentation Attributes: border, cellpadding, cellspacing, width, align, bgcolor

Quick answer The syllabus lists six attributes that control how a table looks. Learn what each one does and where it is written, and note that modern pages use CSS for the same effects.

An attribute is extra information written inside an opening tag as name="value". The attributes below are the ones the CBSE syllabus presents for tables. Write the value in double quotes; leaving the quotes out is a common careless mistake.

border is written on <table> and gives the thickness of the table's border in pixels. border="1" draws a thin line around the table and around every cell. border="0" — or omitting the attribute altogether — draws no visible border, which is why a table you have just written can appear as loose text on the screen. Increasing the number thickens the outer frame.

cellpadding is written on <table> and sets the space in pixels between the content of a cell and that cell's own border — the breathing room inside a cell. With cellpadding="0" text touches the cell edge and looks cramped.

cellspacing is also written on <table> and sets the space in pixels between adjacent cells — the gap outside the cells, between one cell's border and the next. This pair is the classic distinction to keep straight: padding is inside a cell, spacing is between cells. Setting cellspacing="0" makes the borders of neighbouring cells sit together as a single line.

width may be written on <table>, on <th> or on <td>. It takes either a number of pixels, as in width="600", or a percentage of the available space, as in width="80%". A percentage width lets the table grow and shrink with the browser window, which is usually the friendlier choice.

align behaves differently depending on where it is written, and examiners exploit that. On <table>, align positions the whole table on the page — left, center or right. On <tr>, <th> or <td>, it sets the horizontal alignment of the text inside those cells. So <td align="right"> pushes a rupee amount to the right edge of its cell, which is how number columns are normally set.

bgcolor sets a background colour. It may be written on <table> for the whole table, on <tr> for one row, or on <th>/<td> for a single cell. The value is a colour name such as "yellow" or a hexadecimal code such as "#f0f0f0". A value written on a cell overrides one written on its row, which in turn overrides one written on the table — the more specific setting wins.

Here they are together in one table.

<table border="1" cellpadding="8" cellspacing="2"
       width="80%" align="center" bgcolor="#f0f0f0">
  <caption>Annual Fee Structure</caption>
  <tr bgcolor="#cfe8e4">
    <th>Class</th>
    <th>Fee (Rs.)</th>
  </tr>
  <tr>
    <td>Class 9</td>
    <td align="right">18000</td>
  </tr>
  <tr>
    <td>Class 10</td>
    <td align="right">22000</td>
  </tr>
</table>

One honest note to carry into any long answer. These six attributes are presentation attributes: they describe appearance, not meaning. Current professional practice is to leave the HTML describing the data and to set borders, padding, spacing, width, alignment and colour in CSS instead, because one CSS rule can style every table on a site and can be changed in one place. Support for the older attributes also varies — some browsers ignore or partially honour a few of them, so appearance can differ between browsers. Write the attributes when the question asks for them, and add the CSS remark when the question asks you to comment on modern practice.

border &lt;table border="1"&gt; pixels · Thickness of the table border; 0 = invisible.
cellpadding &lt;table cellpadding="8"&gt; pixels · Gap between cell content and cell border (inside).
cellspacing &lt;table cellspacing="2"&gt; pixels · Gap between two neighbouring cells (outside).
width &lt;table width="80%"&gt; pixels or % · Also valid on &lt;th&gt; and &lt;td&gt;.
bgcolor &lt;td bgcolor="#f0f0f0"&gt; Colour name or hex code; cell beats row beats table.
Remember
  • cellpadding = space inside a cell (content to its own border); cellspacing = space between adjacent cells.
  • border is in pixels; border="0" or no border attribute means no visible lines.
  • width takes pixels (width="600") or a percentage (width="80%").
  • align on &lt;table&gt; moves the whole table; align on &lt;tr&gt;/&lt;th&gt;/&lt;td&gt; aligns the text inside cells.
  • bgcolor may be set on table, row or cell; the most specific setting wins.
  • These are presentation attributes — modern practice sets the same effects with CSS.

Merging Cells: colspan and rowspan

Quick answer colspan makes one cell stretch across several columns and rowspan makes it stretch down several rows. Each spanned position is then omitted from the rows it covers.

Real tables are rarely a plain grid. A marks sheet needs one wide heading sitting over two narrow ones; a fee table needs a class name standing beside two term rows. Two attributes do this, and both are written on a cell — on <th> or on <td>, never on <tr> or <table>.

colspan="n" makes one cell occupy n column positions in its own row — it stretches sideways. rowspan="n" makes one cell occupy n row positions in its own column — it stretches downwards. The default value of each is 1, which is why an ordinary cell fills exactly one position.

The rule students most often miss is what happens to the positions that a merged cell swallows. Every position covered by a span must be removed from the markup. If a cell in row 1 has rowspan="2", then row 2 must not write a cell for that column — the merged cell is already occupying it. Writing one anyway pushes the rest of the row sideways and the table comes out crooked.

From this follows the counting rule an examiner applies when marking your code:

For every row: (sum of the colspan values of the cells written in that row) + (number of positions occupied by rowspans coming down from rows above) = the total number of columns in the table.

Now the fully worked example. Copy it, run it, and check it against the rule.

<table border="1" cellpadding="6" cellspacing="0">
  <caption>Half-Yearly Result — Class 10 A</caption>

  <tr>
    <th rowspan="2">Roll No</th>
    <th rowspan="2">Student Name</th>
    <th colspan="2">Marks Obtained</th>
  </tr>

  <tr>
    <th>Theory</th>
    <th>Practical</th>
  </tr>

  <tr>
    <td>01</td>
    <td>Ananya Sharma</td>
    <td align="right">61</td>
    <td align="right">28</td>
  </tr>

  <tr>
    <td>02</td>
    <td>Rohit Verma</td>
    <td align="right">55</td>
    <td align="right">26</td>
  </tr>

  <tr>
    <td colspan="2">Class average</td>
    <td align="right">58</td>
    <td align="right">27</td>
  </tr>
</table>

The table has four columns. Verify each row.

  • Row 1Roll No has rowspan 2 (1 column), Student Name has rowspan 2 (1 column), Marks Obtained has colspan 2 (2 columns). Total 1 + 1 + 2 = 4. Correct.
  • Row 2 — only two cells are written, Theory and Practical. The first two columns are already occupied by the two rowspans from row 1. So 2 written + 2 inherited = 4. Correct, and this is exactly the row where students wrongly re-write Roll No.
  • Rows 3 and 4 — four ordinary cells each. 4 = 4. Correct.
  • Row 5 — one cell with colspan 2 covering the Roll No and Student Name positions, then two ordinary cells holding the two column averages. 2 + 1 + 1 = 4. Correct. The figures check out: theory (61 + 55) ÷ 2 = 58, practical (28 + 26) ÷ 2 = 27. Span only the label columns — had the label been given colspan 3 it would have swallowed the Theory column, and the one remaining figure would have been printed under the Practical heading, which would say something the table does not mean.

Notice how the two attributes combine to produce a two-level heading: Marks Obtained spans two columns across the top, and beneath it the two sub-headings sit in their own row, while Roll No and Student Name — which have no sub-headings — reach down through both heading rows so the header block stays rectangular.

A small check you can do in the exam without a computer: draw the finished grid on your rough sheet as a set of empty boxes, shade the boxes each merged cell covers, and then read your shading back as rows. Any row whose shaded and written boxes do not add up to the column total tells you the error before you lose the mark.

colspan &lt;th colspan="2"&gt;Marks&lt;/th&gt; columns · Cell stretches across 2 column positions.
rowspan &lt;td rowspan="2"&gt;Class 10&lt;/td&gt; rows · Cell stretches down 2 rows; next row omits that cell.
Counting rule sum(colspan) + inherited rowspans = total columns Apply to every row; the usual source of a broken table.
Default colspan = 1, rowspan = 1 Written only when a cell must span more than one position.
Remember
  • colspan merges cells horizontally (across columns); rowspan merges them vertically (down rows).
  • Both are written on &lt;th&gt; or &lt;td&gt; only — never on &lt;tr&gt; or &lt;table&gt;. Default value is 1.
  • Positions covered by a span must be omitted from the rows they cover.
  • Counting rule: colspans written in a row + positions inherited from rowspans above = total columns.
  • A colspan label is the standard way to write a Total or Average row — span only the label columns so every figure stays under its own heading.
  • Sketch the grid and shade merged cells to check a table before writing the final answer.

Row Groups (thead, tbody, tfoot) and Nested Tables

Quick answer thead, tbody and tfoot divide a table into a header, a body and a footer of rows. A complete table may also be placed inside a td to make a nested table.

A long table reads better if its rows are grouped by purpose. HTML provides three row-group elements, each of which contains whole <tr> rows — never bare cells.

  • <thead> — the header group, holding the row or rows of column headings.
  • <tbody> — the body group, holding the main data rows.
  • <tfoot> — the footer group, holding summary rows such as a total.

Here is a table using all three.

<table border="1" cellpadding="6">
  <caption>Library Fine Collected</caption>

  <thead>
    <tr>
      <th>Month</th>
      <th>Amount (Rs.)</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>June</td>
      <td align="right">450</td>
    </tr>
    <tr>
      <td>July</td>
      <td align="right">380</td>
    </tr>
  </tbody>

  <tfoot>
    <tr>
      <th>Total</th>
      <th align="right">830</th>
    </tr>
  </tfoot>
</table>

Two points about these groups are worth stating in an answer. First, they are optional — a table without them is perfectly valid, and if you omit them the browser treats the rows as one implicit body. Second, they add no visible change by themselves; their purpose is to label the meaning of each block of rows so that a style sheet can target it, and so that software reading the page can tell a heading row from a data row. Some browsers can also keep a <thead> visible or repeat it when a long table is printed across several pages, but this behaviour is not identical everywhere, so describe it as something browsers may do rather than as a guarantee.

A detail that surprises students: older textbooks often show <tfoot> written before <tbody>, because an earlier version of HTML asked for that order. Current HTML expects the natural order — <thead>, then <tbody>, then <tfoot> — as shown above. Browsers display the footer rows at the bottom of the table in either case, but write it last: that is both the natural reading order and what current HTML expects.

Nesting a table means placing a complete table inside a cell of another table. The inner table must sit inside a <td> or a <th> — it can never be written loose inside a <tr> or directly inside the outer <table>. The inner table is a full table in its own right, with its own opening and closing tags and its own attributes.

<table border="1" cellpadding="6">
  <tr>
    <th>Class</th>
    <th>Subjects Offered</th>
  </tr>
  <tr>
    <td>Class 10</td>
    <td>
      <table border="1" cellpadding="3">
        <tr>
          <td>Science</td>
          <td>Mathematics</td>
        </tr>
        <tr>
          <td>Social Science</td>
          <td>Computer Applications</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

The outer table has two columns and two rows. The second cell of the second row contains a whole 2 × 2 table. Because the inner table is closed before its enclosing </td>, the nesting is correct.

Nesting is legal but should be used sparingly. Deeply nested tables become very hard to read and to correct, and in most cases the same result can be achieved more simply with colspan and rowspan in a single table. If a question asks you to compare the two, say that colspan and rowspan merge cells within one table structure, while nesting places an independent table inside a cell — and that the merged single table is usually preferred.

Header group &lt;thead&gt; &lt;tr&gt;...&lt;/tr&gt; &lt;/thead&gt; Holds the column-heading rows.
Body group &lt;tbody&gt; ... &lt;/tbody&gt; Holds the main data rows.
Footer group &lt;tfoot&gt; ... &lt;/tfoot&gt; Summary/total rows; browsers display them at the bottom.
Nesting &lt;td&gt; &lt;table&gt; ... &lt;/table&gt; &lt;/td&gt; A complete table inside one cell of another table.
Remember
  • &lt;thead&gt;, &lt;tbody&gt; and &lt;tfoot&gt; group whole rows into header, body and footer; they contain &lt;tr&gt;, not cells.
  • Row groups are optional and produce no visual change on their own; they label meaning for styling and software.
  • Some browsers may repeat a &lt;thead&gt; when a long table is printed, but this behaviour varies.
  • Older HTML put &lt;tfoot&gt; before &lt;tbody&gt;; current HTML expects it last, and either way the footer rows display at the bottom.
  • A nested table must be placed inside a &lt;td&gt; or &lt;th&gt;, never loose inside &lt;tr&gt; or &lt;table&gt;.
  • Prefer colspan/rowspan in one table over deep nesting; nesting is harder to read and correct.

Common Mistakes and What a Full-Mark Answer Contains

Quick answer Most table marks are lost to a handful of repeatable errors — an unclosed cell, a span count that does not match the column total, or a caption in the wrong place.

Table questions are marked on code that would actually work. The errors below are the ones that appear again and again; learn to spot each in your own answer before you move on.

1. An unclosed <td> or <tr>. Every cell and every row is a container tag and needs its closing tag. If you write <td>Ananya and then start the next cell, the browser has to guess where the first cell ended. Browsers do try to recover, but they do not all recover in the same way — the cell may swallow the next one's text, or the row may collapse. Because the recovery differs, the same page can look different in different browsers, which is exactly why an examiner marks it wrong even if it happens to render on one machine.

2. A colspan count that does not match the column total. If the table has 4 columns and you write <td colspan="3"> followed by two more cells, that row claims 5 positions. The browser will usually widen the whole table to 5 columns, leaving every other row one cell short and the grid ragged. The fix is the counting rule from the previous section — check every row.

3. Forgetting to remove the cells a rowspan covers. After <td rowspan="2">Class 10</td>, the next row must not write a cell for that column. Rewriting it shunts the rest of that row one position to the right.

4. Writing a cell outside a row. A <td> placed directly inside <table>, with no <tr> around it, is invalid; so is a <tr> placed inside a <td> without a fresh <table> around it.

5. A caption in the wrong position, or more than one. The <caption> belongs immediately after the opening <table> tag and there may be only one per table.

6. Missing quotes or a stray attribute. Write border="1", not border=1. Also remember which element each attribute belongs to — cellpadding and cellspacing go on <table>, while colspan and rowspan go on a cell.

7. Confusing padding with spacing. Padding is the space inside a cell; spacing is the gap between cells. Say which is which explicitly rather than describing both as "gap".

8. Using <td> where a heading is meant. If a cell names a column, it should be <th>. Making a <td> look bold by other means does not make it a heading.

What a full-mark answer contains. When you are asked to write the HTML code to produce the given table, an answer that scores fully normally shows all of the following: the complete <table> element with its opening and closing tags; the border attribute so the table is visible; a <caption> if the picture shows a title; <th> for every cell that acts as a heading and <td> for the data; correctly counted colspan and rowspan values with the covered cells omitted; every tag closed; attribute values in double quotes; and neat indentation so the structure can be read. If the question also asks you to comment on styling, add the sentence that these presentation attributes are part of older HTML practice and that CSS is now used for borders, spacing, width, alignment and colour.

When the question is a definition or a difference, name the element by its full expansion once — <tr> is table row, <th> is table heading, <td> is table data — then give the distinguishing property, then a one-line example. Definition, distinction, example: three short parts, and the mark is secure.

Wrong (unclosed tag) &lt;td&gt;Ananya (no closing tag) Unclosed cell — recovery differs between browsers.
Wrong (span count) 4 columns, row writes colspan="3" + 2 cells Claims 5 positions; table widens and the grid breaks.
Right &lt;td colspan="3"&gt;Total&lt;/td&gt;&lt;td&gt;830&lt;/td&gt; 3 + 1 = 4 columns; a valid total row.
Attribute placement table: border, cellpadding, cellspacing | cell: colspan, rowspan width, align and bgcolor may appear on either.
Remember
  • Close every &lt;td&gt; and &lt;tr&gt;; browsers recover differently from unclosed tags, so output is unpredictable.
  • Check every row against the counting rule before submitting a colspan/rowspan answer.
  • After a rowspan, omit the covered cell from the following row(s).
  • Caption goes immediately after &lt;table&gt;, only one per table; attribute values always in double quotes.
  • cellpadding/cellspacing belong on &lt;table&gt;; colspan/rowspan belong on a cell.
  • A full-mark code answer shows border, caption, correct th/td, matched spans, closed tags and clean indentation.

Quick reference

Every term, tag and rule from this chapter in one place — screenshot it before your exam.

&lt;table&gt; ... &lt;/table&gt;
Table element
&lt;tr&gt; ... &lt;/tr&gt;
Row
&lt;td&gt; ... &lt;/td&gt;
Cell
cells per row = number of columns
Column count rule
&lt;th&gt; ... &lt;/th&gt;
Heading cell
&lt;td&gt; ... &lt;/td&gt;
Data cell
&lt;caption&gt; ... &lt;/caption&gt;
Caption
table &gt; tr &gt; th | td
Nesting order
&lt;table border="1"&gt;
borderpixels
&lt;table cellpadding="8"&gt;
cellpaddingpixels
&lt;table cellspacing="2"&gt;
cellspacingpixels
&lt;table width="80%"&gt;
widthpixels or %
&lt;td bgcolor="#f0f0f0"&gt;
bgcolor
&lt;th colspan="2"&gt;Marks&lt;/th&gt;
colspancolumns
&lt;td rowspan="2"&gt;Class 10&lt;/td&gt;
rowspanrows
sum(colspan) + inherited rowspans = total columns
Counting rule
colspan = 1, rowspan = 1
Default
&lt;thead&gt; &lt;tr&gt;...&lt;/tr&gt; &lt;/thead&gt;
Header group
&lt;tbody&gt; ... &lt;/tbody&gt;
Body group
&lt;tfoot&gt; ... &lt;/tfoot&gt;
Footer group
&lt;td&gt; &lt;table&gt; ... &lt;/table&gt; &lt;/td&gt;
Nesting
&lt;td&gt;Ananya (no closing tag)
Wrong (unclosed tag)
4 columns, row writes colspan="3" + 2 cells
Wrong (span count)
&lt;td colspan="3"&gt;Total&lt;/td&gt;&lt;td&gt;830&lt;/td&gt;
Right
table: border, cellpadding, cellspacing | cell: colspan, rowspan
Attribute placement

Test yourself

Tap an answer to check it instantly — you'll see why it's right, and what to revise if it isn't.

0 correct · 0/12 answered
Q1 Table structure easy

Which tag creates a heading cell in an HTML table?

Q2 Caption easy

Where must the &lt;caption&gt; element be written?

Q3 Attributes easy

What is the difference between cellpadding and cellspacing?

Q4 colspan medium

A table has 5 columns. A row contains one cell with colspan="2" and no rowspans come down from above. How many more ordinary cells must that row contain?

Q5 colspan and rowspan easy

On which element can colspan and rowspan be written?

Q6 rowspan medium

In row 1 a cell is written as &lt;td rowspan="3"&gt;. What must happen in rows 2 and 3?

Q7 Attributes medium

What does align="center" do when written on the &lt;table&gt; tag?

Q8 Row groups medium

Which statement about &lt;thead&gt;, &lt;tbody&gt; and &lt;tfoot&gt; is correct?

Q9 Nested tables medium

Where must a nested table be placed?

Q10 Attributes easy

A table is written with &lt;table border="0"&gt;. What is the visible result?

Q11 colspan counting hard

Which row is INVALID in a table that has exactly 4 columns?

Q12 Common mistakes hard

A student writes &lt;td&gt;Ananya (with no closing tag) and immediately starts the next cell. What is the most accurate description of the result?

NCERT solutions & previous-year questions

Step-by-step model answers — tap a question to reveal the full solution.

NCERT questions 8

1 Define a table in HTML. Name the tags used to create a basic table.Table structure

A table in HTML is a structure that displays data in rows and columns. The point where a row and a column meet is called a cell, and every value shown in a table sits inside a cell.

Four tags create a basic table, and all four are container (paired) tags:

  • <table> — the outermost container for the whole table.
  • <tr>table row; creates one horizontal row.
  • <th>table heading; creates a heading cell (bold and centred by default).
  • <td>table data; creates an ordinary data cell.

The nesting order is fixed: cells are written inside a row, and rows are written inside the table.

2 Differentiate between the &lt;th&gt; and &lt;td&gt; tags.th vs td

Both tags create a cell inside a row, so the difference lies in meaning, with appearance following from it.

  • <th> stands for table heading. It marks a cell as the heading of the column or row it introduces. Browsers display its text in bold and horizontally centred by default.
  • <td> stands for table data. It marks a cell holding ordinary data. Browsers display its text in normal weight, left-aligned by default.

Example:

<tr>
  <th>Subject</th>
  <th>Marks</th>
</tr>
<tr>
  <td>Computer Applications</td>
  <td>89</td>
</tr>

The bold, centred look of <th> comes from the browser's default style rules, so the exact rendering can vary slightly between browsers; what does not vary is that <th> declares the cell to be a header.

3 What is the purpose of the &lt;caption&gt; tag? State two rules for using it.Caption

The <caption> tag gives a title or heading for the whole table, describing what the table as a whole contains. Browsers normally display it centred, above the table.

Two rules:

  1. It must be written immediately after the opening <table> tag, before any <tr>.
  2. A table may contain only one caption.

Example:

<table border="1">
  <caption>Annual Fee Structure</caption>
  <tr>
    <th>Class</th>
    <th>Fee (Rs.)</th>
  </tr>
</table>

A caption is different from a heading cell: the caption names the entire table, while a <th> names a single row or column.

4 Explain the border, cellpadding, cellspacing and width attributes of the &lt;table&gt; tag with an example.Attributes

An attribute is extra information written inside an opening tag as name="value".

  • border — the thickness of the table's border in pixels. border="0", or omitting it, means no visible lines.
  • cellpadding — the space in pixels between a cell's content and that cell's own border (space inside a cell).
  • cellspacing — the space in pixels between two adjacent cells (space outside the cells).
  • width — the width of the table, given either in pixels (width="600") or as a percentage of the available space (width="80%").

Example:

<table border="2" cellpadding="8" cellspacing="4" width="75%">
  <tr>
    <th>Item</th>
    <th>Price (Rs.)</th>
  </tr>
  <tr>
    <td>Notebook</td>
    <td align="right">60</td>
  </tr>
</table>

These are presentation attributes. In modern practice the same effects are produced with CSS (Cascading Style Sheets), which keeps appearance separate from the data.

5 What do the colspan and rowspan attributes do? Give one short example of each.colspan and rowspan

colspan="n" makes a single cell occupy n column positions in its row — the cell stretches horizontally.

rowspan="n" makes a single cell occupy n row positions in its column — the cell stretches vertically downwards.

Both are written on a cell (<th> or <td>) and both have a default value of 1. Every position that a merged cell covers must be omitted from the rows it covers.

colspan example (a 3-column table with a merged title row):

<tr>
  <th colspan="3">Term 1 Report</th>
</tr>
<tr>
  <th>Roll No</th>
  <th>Name</th>
  <th>Marks</th>
</tr>

rowspan example (a 3-column table; note that the second row writes only two cells):

<tr>
  <td rowspan="2">Class 10</td>
  <td>Term 1</td>
  <td>12000</td>
</tr>
<tr>
  <td>Term 2</td>
  <td>10000</td>
</tr>
6 What are &lt;thead&gt;, &lt;tbody&gt; and &lt;tfoot&gt;? Are they compulsory?Row groups

These three elements divide a table's rows into groups according to purpose. Each of them contains complete <tr> rows, never bare cells.

  • <thead> — the header group, containing the column-heading row(s).
  • <tbody> — the body group, containing the main data rows.
  • <tfoot> — the footer group, containing summary rows such as a total.

They are not compulsory. A table written with only <tr> rows is perfectly valid, and the row groups produce no visual change by themselves. Their purpose is to label the meaning of each block of rows so that a style sheet can target it and so that software reading the page can distinguish heading rows from data rows. Some browsers may also repeat a <thead> when a long table is printed over several pages, though this behaviour is not identical in every browser.

Older HTML asked for <tfoot> to be written before <tbody>, which is why some textbooks show it that way; current HTML expects the order <thead>, <tbody>, <tfoot>. In either case the footer rows are displayed at the bottom of the table, so writing <tfoot> last is the safer habit.

7 What is a nested table? Write a short example and state one disadvantage.Nested tables

A nested table is a complete table written inside a cell of another table. The inner table must be placed between <td> and </td> (or inside a <th>); it can never be written loose inside a <tr> or directly inside the outer <table>.

<table border="1">
  <tr>
    <td>Class 10</td>
    <td>
      <table border="1">
        <tr>
          <td>Science</td>
          <td>Mathematics</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

Disadvantage: nested tables quickly become long and hard to read, and a single missing closing tag in the inner table can disturb the whole outer table. In most cases the same layout can be produced more simply inside one table using colspan and rowspan.

8 List four common errors students make while writing table code, and state the correction for each.Common mistakes
  1. Unclosed <td> or <tr>. Both are container tags. Correction: close every cell with </td> and every row with </tr>. Browsers try to repair broken markup but do not all repair it the same way, so the output becomes unreliable.
  2. A colspan value that does not match the column total. Correction: for each row check that (sum of colspan values written) + (positions inherited from rowspans above) equals the number of columns.
  3. Repeating a cell that a rowspan already covers. Correction: omit that cell from the rows the merged cell reaches into.
  4. Caption in the wrong place, or attribute values without quotes. Correction: write <caption> immediately after the opening <table> tag (only one per table), and always quote values, as in border="1".

Previous-year board questions 6

Q1 Write the HTML code to create the following table: a caption "Half-Yearly Result"; a first heading row in which "Roll No" and "Name" each stretch down two rows and "Marks Obtained" stretches across two columns; a second heading row with "Theory" and "Practical"; two data rows (01, Ananya Sharma, 61, 28 and 02, Rohit Verma, 55, 26); and a last row in which "Class average" stretches across the first two columns and is followed by the theory average and the practical average. 5 marks
<table border="1" cellpadding="6" cellspacing="0">
  <caption>Half-Yearly Result</caption>

  <tr>
    <th rowspan="2">Roll No</th>
    <th rowspan="2">Name</th>
    <th colspan="2">Marks Obtained</th>
  </tr>

  <tr>
    <th>Theory</th>
    <th>Practical</th>
  </tr>

  <tr>
    <td>01</td>
    <td>Ananya Sharma</td>
    <td align="right">61</td>
    <td align="right">28</td>
  </tr>

  <tr>
    <td>02</td>
    <td>Rohit Verma</td>
    <td align="right">55</td>
    <td align="right">26</td>
  </tr>

  <tr>
    <td colspan="2">Class average</td>
    <td align="right">58</td>
    <td align="right">27</td>
  </tr>
</table>

Check (the table has 4 columns):

  • Row 1: 1 + 1 + 2 = 4.
  • Row 2: 2 cells written + 2 positions occupied by the rowspans from row 1 = 4.
  • Rows 3 and 4: 4 ordinary cells each = 4.
  • Row 5: 2 + 1 + 1 = 4.

Note that row 2 does not repeat "Roll No" or "Name" — those positions are already taken by the merged cells above. In the last row the label spans only the two non-numeric columns, so the theory average (61 + 55) ÷ 2 = 58 stays under Theory and the practical average (28 + 26) ÷ 2 = 27 stays under Practical.

Q2 Differentiate between cellpadding and cellspacing. Write one line of code that uses both. 2 marks

cellpadding is the space between the content of a cell and that cell's own border — that is, space inside a cell.

cellspacing is the space between the borders of two adjacent cells — that is, the gap outside the cells. Both are measured in pixels and both are written on the <table> tag.

<table border="1" cellpadding="10" cellspacing="5">
Q3 A student writes a table with 4 columns. In one row the code is &lt;tr&gt;&lt;td colspan="3"&gt;Total&lt;/td&gt;&lt;td&gt;500&lt;/td&gt;&lt;td&gt;600&lt;/td&gt;&lt;/tr&gt;. Identify the error, explain its effect, and write the corrected row. 3 marks

Error: the row claims 3 + 1 + 1 = 5 column positions, but the table has only 4 columns. The colspan count does not match the column total.

Effect: the browser widens the whole table to 5 columns to fit this row. Every other row then supplies only 4 cells, so an empty column appears at the right and the grid looks ragged.

Corrected row (using colspan="2" so that 2 + 1 + 1 = 4):

<tr>
  <td colspan="2">Total</td>
  <td>500</td>
  <td>600</td>
</tr>

Alternatively, if only one value is to follow the label, write <td colspan="3">Total</td><td>1100</td>, which also gives 4 positions.

Q4 Write the HTML code for a table that uses &lt;thead&gt;, &lt;tbody&gt; and &lt;tfoot&gt; to show two months of library fine collection with a total row. 3 marks
<table border="1" cellpadding="6">
  <caption>Library Fine Collected</caption>

  <thead>
    <tr>
      <th>Month</th>
      <th>Amount (Rs.)</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>June</td>
      <td align="right">450</td>
    </tr>
    <tr>
      <td>July</td>
      <td align="right">380</td>
    </tr>
  </tbody>

  <tfoot>
    <tr>
      <th>Total</th>
      <th align="right">830</th>
    </tr>
  </tfoot>
</table>

The three groups contain whole <tr> rows. They are optional and cause no visual change by themselves; they mark which rows are headings, which are data and which are the summary.

Q5 Name the attribute used to merge two cells vertically, state on which tag it is written, and give its default value. 1 mark

rowspan. It is written on a cell tag — <th> or <td> — and its default value is 1.

Q6 State two situations in which a table should NOT be used on a web page, and name the modern method used instead of table presentation attributes. 2 marks

Two situations where a table should not be used:

  1. For a block of continuous writing or a simple list of points or links — these belong in <p>, <ul> or <ol>, because the content is not tabular.
  2. For arranging the overall layout of a page (banner, side menu, main area). This was common in early web pages but makes the code long and hard to maintain, and the markup no longer describes what the data means.

Modern method: CSS (Cascading Style Sheets) is used for layout and for appearance — borders, padding, spacing, width, alignment and colour — instead of the older presentation attributes.

Part of Priodemy for School

Interactive Maths & Science — free with every school on Priodemy EduSuite. Explore more chapters and labs on the Priodemy for School hub.

Ask AI