Contents
Tables
HTML tables let you organize data in neat rows and columns, kind of like a spreadsheet. They’re great for displaying structured information, and you can style them to look pretty awesome too. In this guide, we’ll go over the basics of HTML tables, important attributes, and how to make them look better with some CSS.
Basic Table Structure ( < table >, < tr >, < th >, < td >)
To create a table in HTML, you use a few key elements:
<table>
: This holds the entire table.<tr>
: Each row in the table.<th>
: Header cells (usually bold and centered).<td>
: Regular data cells.
Basic Example:
First Name
Last Name
Age
John
Doe
28
Jane
Smith
34
This will show up like this:
First Name | Last Name | Age |
---|---|---|
John | Doe | 28 |
Jane | Smith | 34 |
Table Attributes (border, cellpadding, cellspacing)
There are a few attributes you can use to adjust the spacing and look of your table.
Border Attribute
The border
attribute adds a border around your table. It’s better to use CSS for this, but for quick styling, it works.
Name
Age
John
28
This creates a table with borders around the cells.
Cellpadding and Cellspacing
cellpadding
: Adds space inside each cell, between the content and the border.cellspacing
: Adds space between the cells themselves.
Name
Age
John
28
Here, cellpadding="10"
adds space inside the cells, and cellspacing="5"
adds space between the cells.
Table Headers and Footers (, , )
For bigger tables, it helps to organize your content into sections using <thead>
, <tbody>
, and <tfoot>
.
Basic Example:
Name
Age
John
28
Jane
34
End of Data
Here’s how it works:
<thead>
: Holds the table headers.<tbody>
: Holds the actual data rows.<tfoot>
: Holds any footer info, like summaries or notes.
Colspan and Rowspan Attributes
Need to merge cells? Use colspan
to make a cell span across multiple columns, and rowspan
to span across rows.
Colspan Example:
Name
Age
John
Doe
28
This merges two header cells into one:
Name Age John Doe 28
Rowspan Example:
Name
Age
John
Jane
34
This merges the “Age” cell across two rows:
Name Age John 28 Jane 34
Styling Tables with CSS
You can style tables using CSS to make them look cleaner and more polished.
Basic Table Styling:
Name
Age
John
28
Jane
34
border-collapse
: Merges the borders into one.- Padding: Adds space inside the cells.
- Hover effect: Highlights a row when you hover over it.
Zebra Striped Table:
Zebra striping alternates the row colors to make the table easier to read.
This creates alternating row colors for better readability.
Summary
Tables in HTML are super handy for organizing data into rows and columns. The basic structure includes tags like <table>
, <tr>
, <th>
, and <td>
. You can use attributes like border
, cellpadding
, and cellspacing
to adjust the table’s spacing and appearance. More advanced tags like <thead>
, <tbody>
, and <tfoot>
help with larger tables, and colspan
and rowspan
allow you to merge cells. Finally, with CSS, you can make your tables look polished with features like hover effects and zebra stripes.
Related Chapters
GET IN TOUCH
+91 9999990000
contact@eduzan.com
For bigger tables, it helps to organize your content into sections using <thead>
, <tbody>
, and <tfoot>
.
Basic Example:
Name
Age
John
28
Jane
34
End of Data
Here’s how it works:
<thead>
: Holds the table headers.<tbody>
: Holds the actual data rows.<tfoot>
: Holds any footer info, like summaries or notes.
Colspan and Rowspan Attributes
Need to merge cells? Use colspan
to make a cell span across multiple columns, and rowspan
to span across rows.
Colspan Example:
Name
Age
John
Doe
28
This merges two header cells into one:
Name | Age | |
---|---|---|
John | Doe | 28 |
Rowspan Example:
Name
Age
John
Jane
34
This merges the “Age” cell across two rows:
Name | Age |
---|---|
John | 28 |
Jane | 34 |
Styling Tables with CSS
You can style tables using CSS to make them look cleaner and more polished.
Basic Table Styling:
Name
Age
John
28
Jane
34
border-collapse
: Merges the borders into one.- Padding: Adds space inside the cells.
- Hover effect: Highlights a row when you hover over it.
Zebra Striped Table:
Zebra striping alternates the row colors to make the table easier to read.
This creates alternating row colors for better readability.
Summary
Tables in HTML are super handy for organizing data into rows and columns. The basic structure includes tags like <table>
, <tr>
, <th>
, and <td>
. You can use attributes like border
, cellpadding
, and cellspacing
to adjust the table’s spacing and appearance. More advanced tags like <thead>
, <tbody>
, and <tfoot>
help with larger tables, and colspan
and rowspan
allow you to merge cells. Finally, with CSS, you can make your tables look polished with features like hover effects and zebra stripes.
Related Chapters
GET IN TOUCH
contact@eduzan.com