Topic 07 / 09

Tables: Rows, Columns & Data Grids

1. Simple Definition

Jab hume structured data (rows aur columns me) dikhana hota hai, tab hum <table> element ka use karte hain. Isme <tr> row banata hai, <th> header cell banata hai, aur <td> data cell banata hai.

2. Real-Life Analogy

📊 School Time-Table Ya Cricket Match Scorecard

Cricket scorecard me upar column headers hote hain (Player Name, Runs, Balls, 4s, 6s). Niche har batsman ke liye ek horizontal row hoti hai. Table bhi bilkul aise hi data ko rows aur columns ke grid me organise karta hai.

3. Why Do We Need It?

Financial reports, product specifications, pricing comparison tables, aur train schedules bina table ke padhna impossible hota.

4. Syntax

<table>
  <caption>Table Ka Title</caption>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
    </tr>
  </tbody>
</table>

5. Basic Example (With Colspan & Rowspan)

<div class="table-wrapper">
  <table>
    <caption>Student Exam Report</caption>
    <thead>
      <tr>
        <th>Roll No</th>
        <th>Student Name</th>
        <th>Subject</th>
        <th>Marks</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>101</td>
        <td rowspan="2">Rahul Kumar</td>
        <td>Maths</td>
        <td>95</td>
      </tr>
      <tr>
        <td>101</td>
        <td>Science</td>
        <td>88</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td colspan="3" style="font-weight: bold;">Total Marks</td>
        <td style="font-weight: bold;">183</td>
      </tr>
    </tfoot>
  </table>
</div>

6. Output / Expected Result

Student Exam Report
Roll No Student Name Subject Marks
101 Rahul Kumar Maths 95
101 Science 88
Total Marks 183

7. Code Explanation

  • <caption>: Table ka accessible title.
  • <thead>, <tbody>, <tfoot>: Table ko header, main data aur summary footer me divide karte hain.
  • colspan="3": Ek cell ko 3 columns ki width me merge karta hai.
  • rowspan="2": Ek cell ko 2 vertical rows me merge karta hai.

8. Real-World Example

Flight booking sites (MakeMyTrip, Indigo) par flight timings, fare options aur baggage allowances tables me show kiye jaate hain.

9. Common Mistakes

⚠️ Galti 1: Webpage ka layout banane ke liye table use karna

1990s me log poor layout table se banate the. Aaj ke modern web me table sirf aur sirf tabular data ke liye use hota hai. Layout ke liye Flexbox aur Grid use karein!

⚠️ Galti 2: <th> ke badle <td> use karna headers me

Header row me hamesha <th> lagayein taaki screen readers column names announce kar sakein.

10. Best Practices

  • Mobile screen par table cut na ho, isliye table ko <div class="table-wrapper"> me wrap karke overflow-x: auto; lagayein.
  • Hamesha <th scope="col"> ya <th scope="row"> attribute use karein accessibility ke liye.

11. Try It Yourself

Playground me apne college subjects aur marks ki ek 3-row table banayein.

12. Challenge

Ek Gym Workout Plan table banayein jisme Day, Exercise, Sets, aur Reps hon, aur Saturday/Sunday ke liye colspan="3" me "REST DAY" likha ho.

13. Interview Questions

💼 Q: colspan aur rowspan me difference kya hai?

Answer: colspan horizontally do ya do se zyada columns ko merge karta hai. rowspan vertically do ya do se zyada rows ko merge karta hai.

14. Quick Revision

  • table -> thead/tbody/tfoot -> tr -> th/td.
  • Tabular data ke liye use karein, page layout ke liye nahi.
  • colspan horizontally merge karta hai, rowspan vertically.

15. FAQ

Q1. Table borders CSS ke bina kaise dikhte hain?

Pehle border="1" attribute chalta tha, par modern standard me CSS border: 1px solid #ccc; use hota hai.

Q2. border-collapse property kya karti hai?

CSS me border-collapse: collapse; likhne par double borders single clean line me jud jaate hain.

Q3. Table mobile par kaise responsive banta hai?

Table ke parent div par overflow-x: auto; lagakar horizontal scrolling di jaati hai.

Q4. thead aur tbody likhna kyun zaroori hai?

Print karte waqt ya lambi tables me browser har naye page par table header ko automatically repeat kar sakta hai.

Q5. th element default me kaisa dikhta hai?

Bold font aur center-aligned text ke sath render hota hai.

Topic 07 / 09

Tables & Data Grids (Rows, Columns, Headers)

1. Simple Definition

HTML tables (<table>) structure complex, multi-dimensional relational data into horizontal rows and vertical columns. Semantic tables are composed of <thead> (table header), <tbody> (table data rows), <tr> (table row), <th> (header cell), and <td> (data cell).

2. Real-Life Analogy

📊 School Report Cards & Cricket Scoreboards

Think of an examination marksheet: Column headers represent subjects (Math, English, Science). Each horizontal row corresponds to an individual student with their specific scores aligned underneath.

3. Why Do We Need It?

  • Tabular Data Representation: Financial statements, sports scoreboards, price comparisons, and schedule timetables.
  • Cell Spanning: Merging cells across multiple rows or columns using rowspan and colspan.
  • Data Accessibility: Associating headers with data cells via scope="col" and scope="row".

4. Syntax

<table>
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Role</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Rahul</td>
      <td>Developer</td>
    </tr>
  </tbody>
</table>

5. Basic Example

<table style="width:100%; border-collapse: collapse;">
  <caption>Quarterly Employee Performance Matrix</caption>
  <thead>
    <tr style="background: var(--bg-secondary);">
      <th style="padding: 8px; border: 1px solid var(--border-color);">Employee</th>
      <th style="padding: 8px; border: 1px solid var(--border-color);">Department</th>
      <th style="padding: 8px; border: 1px solid var(--border-color);">Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="padding: 8px; border: 1px solid var(--border-color);">Aarav Patel</td>
      <td style="padding: 8px; border: 1px solid var(--border-color);">Engineering</td>
      <td style="padding: 8px; border: 1px solid var(--border-color);">Active</td>
    </tr>
    <tr>
      <td style="padding: 8px; border: 1px solid var(--border-color);">Priya Roy</td>
      <td style="padding: 8px; border: 1px solid var(--border-color);">Product Design</td>
      <td style="padding: 8px; border: 1px solid var(--border-color);">Active</td>
    </tr>
  </tbody>
</table>

6. Output / Expected Result

Employee Department Status
Aarav Patel Engineering Active
Priya Roy Product Design Active

7. Code Explanation

  • <caption>: Provides an accessible programmatic title for the table.
  • <thead>, <tbody>, <tfoot>: Group header, body, and summary footer rows.
  • <th> vs <td>: <th> designates bold, centered header cells; <td> holds standard data.
  • colspan & rowspan: Merge cells across multiple horizontal columns or vertical rows.

8. Real-World Example

SaaS pricing tables (comparing Free, Pro, and Enterprise tiers across 20+ feature checks) rely on tables enhanced with colspan and responsive overflow wrappers.

9. Common Mistakes (Beginners Traps)

⚠️ Trap: Using <table> for page layouts

In the late 1990s, developers used tables to layout pages. Never do this today! Use CSS Grid or Flexbox for layout; reserve <table> exclusively for tabular data.

10. Best Practices

  • Always wrap tables in a container with overflow-x: auto; for mobile responsiveness.
  • Use scope="col" and scope="row" on <th> elements for screen reader navigation.
  • Always declare CSS border-collapse: collapse; to remove double borders.

11. Try It Yourself

Open the Live Playground and create a student grade table using colspan for a merged semester heading!

Open Live Playground 🛠️

12. Mini Challenge

Create a weekly class schedule table with Monday through Friday headers and a merged 2-hour lab session using colspan="2".

13. Interview Questions

💼 Q1: What is the purpose of the scope attribute on <th>?

Answer: scope="col" or scope="row" explicitly tells assistive technologies whether a header cell categorizes an entire column or row, making navigation comprehensible for screen readers.

14. Quick Revision

  • <table> is strictly for tabular datasets.
  • Structure tables with <thead> and <tbody>.
  • Use colspan and rowspan to span cell dimensions.

15. FAQ (Frequently Asked Questions)

Q1. How do I make large data tables responsive on mobile screens?

Wrap the table in a <div style="overflow-x: auto;"> container, allowing smooth horizontal scrolling without breaking outer layouts.