Working with HTML-tables
Tables are an important part of web design. They allow you to display information in an organized manner. HTML tables can be daunting at first, but they’re actually quite simple once you know the basics. Let’s take a look at how to create HTML tables with code snippet examples.
Creating a Table: A basic HTML table is created using the <table> tag. Within this tag, each row is created using the <tr> tag, and each cell within that row is created using the <td> tag. Here’s an example of a simple table with two rows and two columns:
<table> <tr> <td>Row 1, Column 1</td> <td>Row 1, Column 2</td> </tr> <tr> <td>Row 2, Column 1</td> <td>Row 2, Column 2</td> </tr> </table>
Row 1, Column 1 | Row 1, Column 2 |
Row 2, Column 1 | Row 2, Column 2 |
Adding Table Headers: The <th> tag is used to create headers for the table. It functions just like the <td> tag, but is used for the first row of the table to indicate column headers. Here is an example with headers added to our previous table:
<table> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Row 1, Column 1</td> <td>Row 1, Column 2</td> </tr> <tr> <td>Row 2, Column 1</td> <td>Row 2, Column 2</td> </tr> </table>
Header 1 | Header 2 |
---|---|
Row 1, Column 1 | Row 1, Column 2 |
Row 2, Column 1 | Row 2, Column 2 |
Colspan and Rowspan: The <colspan> and <rowspan> attributes can be used to make a cell span across multiple rows or columns. Here is an example that uses rowspan to create a cell that spans two rows:
<table> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Row 1, Column 1</td> <td>Row 1, Column 2</td> </tr> <tr> <td rowspan="2">Row 2-3, Column 1</td> <td>Row 2, Column 2</td> </tr> <tr> <td>Row 3, Column 2</td> </tr> </table>
Header 1 | Header 2 |
---|---|
Row 1, Column 1 | Row 1, Column 2 |
Row 2-3, Column 1 | Row 2, Column 2 |
Row 3, Column 2 |
Styling a Table: CSS can be used to style a table. We can add borders and change the background color of alternating rows. Here’s a CSS example:
table { border-collapse: collapse; width: 100%; } td, th { border: 1px solid black; padding: 8px; text-align: left; } tr:nth-child(even) { background-color: #f2f2f2; }
And here’s an example table with CSS applied:
Header 1 | Header 2 |
---|---|
Row 1, Column 1 | Row 1, Column 2 |
Row 2, Column 1 | Row 2, Column 2 |
Row 3, Column 1 | Row 3, Column 2 |
Conclusion: HTML tables can be easily created using the <table>, <tr>, and <td>/<th> tags. The <colspan> and <rowspan> attributes can be used to make cells span multiple rows or columns. And CSS can be used to style the table and its contents. For more information, check out this resource from W3Schools.
Written by: Steven Iversen 24-05-2023 Written in: HTML tutorials
Great tutorial! I found the explanation of working with HTML tables quite clear and easy to follow . The step-by-step instructions were helpful in understanding the process. However, it would have been even better if there were some interactive examples or coding exercises to practice what was taught. Overall, a good resource for beginners like me who are just starting out with HTML tables. Keep up the good work!
This article is amazing! It provided such clear and thorough explanations on working with HTML tables . I’ve always struggled with understanding the structure and styling of tables, but this tutorial made it so much easier for me to grasp. The step-by-step instructions, along with the code examples, were incredibly helpful. Now I feel more confident in incorporating tables into my web projects. Thank you for sharing this valuable resource!
I think the tutorial could use more detailed examples and explanations on how to style HTML tables . I found that the instructions were a bit vague and left me feeling confused. It would be helpful to include more visuals to make it easier to follow along.
I found this tutorial really helpful in understanding how to work with HTML tables . The explanation was clear and easy to follow, and the examples provided were great for practice. Thank you for sharing this helpful resource!