Simple methods for creating tables with HTML

Here are some Simple methods for creating tables with HTML: To begin, first refer –creating-tables-in-html

Creating a Simple Table:

HTML’s <table>, <tr>, <th>, and <td> components can be used to make a basic table. Here is an example of a simple table with two rows and two columns:

In this example, <table> specifies the table, <tr> defines a row, defines a header cell, and <td> defines a data cell. The border=”1″ feature adds a border to the table for easier visualization, but it is optional.

Figure 1 – Displaying a Table

Adding a Title to a Column:

To include a title for a column in an HTML table, use the <th> (table header) element within the <tr> (table row) that specifies the header row. Here’s an example:

In this case, the first and second columns’ titles are “Title 1” and “Title 2”. The <th> element automatically displays certain titles in bold.

Figure 2 – Displaying a Table with the Column Title

Adding a Caption to a Table

To include a caption in an HTML table, use the <caption> element. Here’s an example:

In this example, the <caption> element is positioned directly after the <table> tag to provide a caption for the table. The text in the <caption> element will be shown centered above the table.

Figure 3 – Displaying a Table with a Column

Specifying the Properties of the Columns

To specify column characteristics in an HTML table, use the <colgroup> and <col> elements. The <colgroup> element groups one or more columns, while the <col> element specifies characteristics for each column.

4.1 – Changing the Properties of Multiple Columns

Change the characteristics of numerous columns in an HTML table using the <colgroup> and <col> elements. Here’s an example of changing the background color of the second and third columns:

In this example, the second and third <col> components within the <colgroup> apply the highlight class to set the background color to light blue. This allows you to change the properties of numerous columns at once.

Figure 4.1 – Showing the Changed Property of Columns

4.2 – Changing the Properties of Single Columns

The <colgroup> and <col> elements can modify a single column’s properties in an HTML table. Here’s an example to change the background color of the second column:

In this example, the second <col> element within the <colgroup> applies the highlight class, which sets the background color to light blue, to the second column alone. No specific styling has been applied to the first and third columns.

Figure 4.2 – Showing the Changed Property of Columns

Spanning Rows and Columns

To span rows and columns in an HTML table, use the rowspan and colspan properties in the <td> (table data) or <th> (table header) elements respectively. Here’s an example with two rows and three columns:

In this example, the first row contains three cells. The second cell spans two columns (colspan=”2″), encompassing both “Title 2” and “Title 3.” The first cell in the second row spans two rows (rowspan=”2″), covering “Row 1, Cell 1” and making room for “Row 2, Cell 1.” This allows you to customize the layout and structure of your table by spanning rows and columns as desired.

Figure 5 – Displaying a Table with Spanned rows and columns

Using Images in a Table

To include images in an HTML table, use <img> elements inside <td> (table data) components. Here’s an example that uses photos in a table:

In this example, each table row has an image in the first column and a description in the second. The <img> element displays the picture, while the src attribute indicates its URL. The alt element offers alternate text for the image, which is displayed when the image cannot be loaded or for accessibility reasons.

Figure 6 – Displaying the output of using an image in the table

Creating Advanced Tables

Advanced tables in HTML are created by combining several elements and attributes to customize the table’s design and behavior. Here’s an example of an advanced table with several features:

This example shows a few advanced features:

  • Custom CSS styling for the table, headers, cells, and alternating row colors.
  • Use the rowspan and colspan characteristics to span rows and columns.
  • To structure the table, use <thead>, <tbody>, and <tfoot> tags.
  • Adding a caption to the table to provide further context.

Figure 7 – Displaying an Advanced Table

Nesting Tables

To nest tables in HTML, insert one <table> element into another <td> or <th> element. Here’s an example of nesting tables:

In this example, the outer table has two columns. The first column of the outer table has a nested table with its own headers and cells. The nested table is placed inside an outer table cell. You can use this technique to create more complicated table structures by stacking tables within tables.

Figure 8 – Displaying Nested Tables

Scroll to Top