Table of Content
An HTML table is a way to organize information into rows and columns, like a spreadsheet. It's used to display data in a clear and easy-to-read format.
A table consists of the following parts:
This table displays two rows and two columns, representing the names and ages of John and Mary.
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Mary</td>
<td>30</td>
</tr>
</tbody>
</table>
Use the <table>
tag to create the main table structure.
For the table headers, use the <th>
tag within the <tr>
(table row) tag. These will be the column names or headings.
To add content to the table cells, use the <td>
tag within the <tr>
tag. These will be the values for each cell.
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Mary</td>
<td>30</td>
</tr>
</table>
Imagine a table as a rectangular grid. You can control its width and alignment like this:
<table width="500px">
).<table align="center">
).Cell padding and spacing control the empty space around and between table cells.
<table cellpadding="10">
).<table cellspacing="5">
).CSS (Cascading Style Sheets) allows you to style tables visually. Here are some examples:
table { border: 1px solid black; }
).td { background-color: #f5f5f5; }
).table { font-family: Arial, sans-serif; }
).Imagine you have a table representing students' names and marks in different subjects. You want to combine some cells to create a bigger cell that spans across multiple rows or columns.
Sometimes, you want to group related columns in a table. For example, in the student table, you may want to group all the subject columns together. To do this, you can use the <colgroup>
tag:
<colgroup>
<col class="subjects" span="2">
</colgroup>
This groups the next two columns (Math and Science) into a "subjects" group.
A caption provides a title or description for a table, making it easier to understand. To add a caption, use the <caption>
tag:
<table>
<caption>Student Marks</caption>
</table>
To create more complex table layouts, you can combine all these techniques:
colspan
and rowspan
to merge cells.<colgroup>
to group related columns.For example, here's a more complex table layout, this layout combines a title caption, merged cells for student names, and grouped subject columns:
<table>
<caption>Student Marks</caption>
<thead>
<tr>
<th colspan="2">Student</th>
<th colspan="3">Subjects</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">John Smith</td>
<td>First name</td>
<td class="subjects">Math</td>
<td class="subjects">Science</td>
<td class="subjects">History</td>
</tr>
<tr>
<td>Last name</td>
<td>90</td>
<td>85</td>
<td>75</td>
</tr>
</tbody>
</table>
Screen readers help people with visual impairments access information on computers. To make tables accessible:
Tables can slow down page load times. To optimize performance:
Clean and readable code makes it easier to work with and maintain. To improve readability:
Imagine you have three small tables that fit inside each other, like Russian nesting dolls. You place the smallest table inside the medium table and then the medium table inside the largest table. This is called nesting tables.
Using nested tables can be confusing and make your webpage difficult to read. It's like trying to reach a toy hidden deep inside a set of boxes.
A table has three main parts: rows, columns, and cells. There are also some older elements that are no longer recommended to use, like <tr>
, <td>
, and <th>
.
Using these deprecated elements can make your webpage outdated and less accessible. It's like trying to use an old-fashioned typewriter instead of a modern computer.
Tables were originally designed to organize data, not to create page layouts. Using tables to control the appearance of your webpage can be messy and hard to maintain.
It's like using a hammer to build a house. Sure, it would technically work, but there are better tools that are designed specifically for that purpose.
To make your webpage easier to read, especially for younger students, you should:
<div>
and <span>
instead of <tr>
and <td>
)| Car A | Car B | |---|---| | Model | Sedan | SUV | | Color | Red | Blue | | Fuel Type | Gasoline | Electric |
| Home | About Us | Contact Us | |---|---|---|
| Column 1 | Column 2 | |---|---| | Image | Text |
| Name | Age | City | |---|---|---| | John | 18 | London | | Mary | 19 | Paris |
CSS (Cascading Style Sheets) is a language that defines the visual appearance of web pages. It can be used to control the layout, fonts, colors, and other aspects of a webpage.
To create a dynamic table using CSS, you can specify the width and height of the table, as well as the borders and padding of the table cells. You can also specify the background color and text color of the table cells.
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 5px;
}
th {
text-align: center;
}
This code will create a table with a width of 100% of the available space. The borders between the cells will collapse, meaning that adjacent cell borders will overlap each other. The table cells will have a border of 1 pixel and a padding of 5 pixels. The table headers will be aligned in the center.
Responsive tables are tables that can adapt their layout to fit different screen sizes. This is important for mobile devices, which have smaller screens than desktop computers.
To create a responsive table, you can use the @media
rule. The @media
rule allows you to specify CSS rules that will only be applied when certain conditions are met. For example, you can use the max-width
condition to specify that certain CSS rules will only be applied when the screen is less than a certain width.
@media (max-width: 768px) {
table {
width: 100%;
}
th, td {
border: 1px solid black;
padding: 5px;
}
th {
text-align: center;
}
}
This code will create a responsive table that will adjust its layout based on the size of the screen. When the screen is less than 768 pixels wide, the table will be 100% of the available space. The table cells will have a border of 1 pixel and a padding of 5 pixels. The table headers will be aligned in the center.