html
/

HTML Tables: Displaying Data in Rows and Columns

Last Sync: Today

On this page

9
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

html

HTML Tables: Displaying Data in Rows and Columns

What are HTML Tables?

HTML tables are used to display data in a structured format using rows and columns.

Basic Table Structure

HTMLRead-only
1
<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>John</td>
    <td>25</td>
  </tr>
</table>

Table Elements

TagDescription
<table>Defines a table
<tr>Table row
<th>Header cell
<td>Data cell
<thead>Table header group
<tbody>Table body
<tfoot>Table footer

Table with Header, Body, Footer

HTMLRead-only
1
<table>
  <thead>
    <tr><th>Name</th><th>Age</th></tr>
  </thead>
  <tbody>
    <tr><td>Alice</td><td>22</td></tr>
    <tr><td>Bob</td><td>28</td></tr>
  </tbody>
  <tfoot>
    <tr><td colspan="2">End of Data</td></tr>
  </tfoot>
</table>

Colspan and Rowspan

HTMLRead-only
1
<table>
  <tr>
    <th colspan="2">Full Name</th>
  </tr>
  <tr>
    <td rowspan="2">John</td>
    <td>Doe</td>
  </tr>
  <tr>
    <td>Smith</td>
  </tr>
</table>

Table Styling with CSS

CSSRead-only
1
table {
  border-collapse: collapse;
  width: 100%;
}
th, td {
  border: 1px solid #ccc;
  padding: 8px;
}

Best Practices

  • Use tables only for tabular data
  • Use <th> for headers
  • Keep tables simple and readable
  • Use CSS for styling
  • Use semantic sections (thead, tbody, tfoot)

Common Mistakes

  • Using tables for layout design
  • Missing table headers
  • Overcomplicating with too many spans
  • Not making tables responsive

Conclusion

HTML tables are powerful for displaying structured data. Proper usage ensures clarity, accessibility, and maintainability.

Try it yourself

<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Alice</td>
    <td>22</td>
  </tr>
</table>

Test Your Knowledge

Q1
of 3

Which tag defines a table row?

A
<td>
B
<tr>
C
<th>
D
<table>
Q2
of 3

Which tag is used for header cells?

A
<td>
B
<th>
C
<tr>
D
<thead>
Q3
of 3

What does colspan do?

A
Merge rows
B
Merge columns
C
Add border
D
Align text

Frequently Asked Questions

What is colspan?

It merges multiple columns into one.

What is rowspan?

It merges multiple rows into one.

Should tables be used for layout?

No, use CSS layouts instead.

Previous

html textarea

Next

html table advanced

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.