Learn HTML

"Are you looking to learn HTML from scratch? Look no further! Our HTML course is designed for both beginners and advanced learners who want to master the fundamental concepts of HTML. Our comprehensive course covers everything from the basic concepts of HTML, including tags, attributes, and elements, to the more advanced concepts such as multimedia, responsive design, and accessibility."

HTML Tables

HTML tables are used to display data in rows and columns. They are created using the <table> element and contain one or more rows, which are created using the <tr> element, and one or more cells, which are created using the <td> element. 

Now, you might be wondering, “Why do I need tables?” Tables are crucial for displaying data in a structured format. They're often used for schedules, pricing lists, or any data that needs to be neatly organized.

 

HTML Table Syntax

The basic syntax for creating an HTML table is as follows:

<table>
   <tr>
     <td>Row 1, Cell 1</td>
     <td>Row 1, Cell 2</td>
   </tr>
   <tr>
     <td>Row 2, Cell 1</td>
     <td>Row 2, Cell 2</td>
   </tr>
</table>


In this example, the <table> element is used to create the table, and each row is created using the <tr> element. Within each row, one or more cells are created using the <td> element. This table has two rows and two columns, with the text "Row 1, Cell 1" in the first cell of the first row, "Row 1, Cell 2" in the second cell of the first row, "Row 2, Cell 1" in the first cell of the second row, and "Row 2, Cell 2" in the second cell of the second row.

 

HTML Table Attributes

HTML tables have a number of attributes that can be used to specify things like the width of the table, the alignment of the text within cells, and the border style of the table. Some common attributes include:

  • border: specifies the width of the border around the table (useful for creating tables with visible borders)
  • width: specifies the width of the table
  • cellpadding: specifies the amount of space between the content of a cell and its border
  • cellspacing: specifies the amount of space between cells
  • align: specifies the horizontal alignment of the table within its container (left, center, or right)
  • valign: specifies the vertical alignment of the table within its container (top, middle, or bottom)

These attributes can be added to the <table> element to customize the appearance of the table.

 

HTML Table Example

HTML tables allow you to display data in a tabular format. Here's an example of how to create a simple HTML table using various attributes:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Example Table</title>
</head>
<body>
    <table border="1" cellpadding="5" cellspacing="0">
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
        <tr>
            <td>Row 1, Cell 1</td>
            <td>Row 1, Cell 2</td>
            <td>Row 1, Cell 3</td>
        </tr>
        <tr>
            <td>Row 2, Cell 1</td>
            <td>Row 2, Cell 2</td>
            <td>Row 2, Cell 3</td>
        </tr>
    </table>
</body>
</html>

In this example, we've created a basic HTML table with 3 columns and 2 rows. Let's break down the various attributes used:

  • border: This sets the thickness of the border around the table cells. The value of 1 creates a solid border.
  • cellpadding: This sets the amount of space between the cell content and the cell border. The value of 5 creates 5 pixels of padding.
  • cellspacing: This sets the amount of space between cells. The value of 0 creates no spacing between cells.
  • <th>: This is a table header cell, used to specify column headers. By default, <th> cells are bold and centered.
  • <td>: This is a standard table cell, used to contain data.

 

HTML Table Benefits

  • HTML tables are a useful way to display data in a structured format. 
  • They can be used to create everything from simple two-column layouts to complex spreadsheets with multiple rows and columns. 
  • By using the various attributes available, you can customize the appearance of your tables to suit your needs.

 

Watch this Chapter Video on YouTube: