HTML Tables: A Comprehensive Guide with Code Examples

0
3كيلو بايت

HTML tables are used to structure data in a tabular format, making it easy to read and understand. They consist of rows and columns, and each cell within the table can contain text, images, or even other HTML elements.

Basic Table Structure

HTML
<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
    <td>Cell 3</td>
  </tr>
  <tr>
    <td>Cell 4</td>
    <td>Cell 5</td>
    <td>Cell 6</td>
  </tr>
</table>

  • <table>: This tag defines the table.  
  • <tr>: This tag defines a table row.
  • <th>: This tag defines a table header cell.
  • <td>: This tag defines a table data cell.

Table Headers and Data Cells

  • Headers: Use <th> tags to create table headers. They are typically styled differently (e.g., bold, centered) to distinguish them from data cells.
  • Data Cells: Use <td> tags to create table data cells. These contain the actual content of the table.

Table Attributes

  • border: Sets the width of the table border.
  • cellspacing: Sets the space between cells.
  • cellpadding: Sets the space between the cell content and the cell border.
  • width: Sets the width of the table.
  • height: Sets the height of the table.

Example with Attributes

HTML
<table border="1" cellspacing="0" cellpadding="5
  

Table Captions and Summary

  • <caption>: Adds a caption to the table.
  • summary: Provides a brief summary of the table's content, which is helpful for screen readers and search engines.

Example with Caption and Summary

HTML
<table>
  <caption>Sales Data for Q3</caption>
  <summary>This table shows the sales figures for each product line during the third quarter.</summary>
  </table>

Colspan and Rowspan

  • colspan: Specifies the number of columns a cell should span.
  • rowspan: Specifies the number of rows a cell should span.

Example with Colspan and Rowspan

HTML
<table>
  <tr>
    <th>Product</th>
    <th colspan="2">Sales</th>
    <th>Profit</th>
  </tr>
  <tr>
    <td>Product A</td>
    <td>100</td>
    <td>2000</td>
    <td>500</td>
  </tr>
  <tr>
    <td>Product B</td>
    <td rowspan="2">150</td>
    <td>3000</td>
    <td>700</td>
  </tr>
  <tr>
    <td>Product C</td>
    <td>2500</td>
    <td>800</td>
  </tr>
</table>

By understanding these concepts and using the provided code examples, you can effectively create and customize HTML tables to present your data in a clear and organized manner.

Like
1
البحث
الأقسام
إقرأ المزيد
Chemistry
NEW CURRICULUM CHEMISTRY PRACTICAL SAMPLES
https://acrobat.adobe.com/id/urn:aaid:sc:EU:4838a935-5393-417a-a9b9-a4c21d6109cb
بواسطة Landus Mumbere Expedito 2024-07-18 19:33:23 0 3كيلو بايت
Computer Programming
Comparing Data Types and Conditional Expressions
Comparing Data Types: In Python, you can compare data types using the type() function. This...
بواسطة Python for Everybody - Full University Python Course Code 2024-07-16 21:47:48 0 3كيلو بايت
التعليم
Black Codes and Segregation in Reconstruction
Black Codes: The Black Codes were a series of laws passed by Southern states after the Civil...
بواسطة Modern American History 2024-07-19 05:43:15 0 3كيلو بايت
Mathematics
UACE MACIDITA PURE MATHS MOCK EXAM 2024
UACE MACIDITA PURE MATHS MOCK EXAM 2024
بواسطة Landus Mumbere Expedito 2024-08-13 19:01:30 0 3كيلو بايت