HTML Table


In this tutorial, we will learn about the HTML Table, several ways to build it, and how to use it through examples. HTML Table is a data organization in rows and columns, or in a more complicated form. Tables are common in communication, research, and data analysis. Tables may be used to convey written information and numerical data, among other things. In the tabular form arrangement, it may be used to compare two or more objects. Databases are built using tables.

Defining tables in HTML

The "table" element is used to define an HTML table. The "tr" tag is used to specify each table row. The "th" tag is used to specify a table header. Table heads are bold and centered by default. The "td" tag specifies a table data/cell.

Example

Preview

Adding a border to an HTML table

The CSS border property is used to define a border. If no border is specified for the table, it will be presented without one.

Syntax


Adding collapsed borders in an HTML table

Add the CSS border-collapse attribute to make borders merge into one.

Syntax


Adding cell padding in an HTML table 

The gap between the cell content and its edges is defined by cell padding. If no padding is specified, the table cells will be presented without padding.

Syntax


Adding left align headings in an HTML table

Table heads are bold and centered by default. To align the table heads to the left, we must utilize the CSS text-align property.

Syntax


Adding border spacing in an HTML table

The gap between the cells is defined by the border spacing. We must utilize the CSS border-spacing property to specify the border-spacing for a table.  

Syntax


Adding cells that span many columns in HTML tables

The colspan property must be used to have a cell of more than one column. 

Example

Preview

Adding cells that span many rows in HTML tables

The rowspan property must be used to have a cell of more than one row. 

Example

Preview

Adding a caption in an HTML table

We must utilize the "caption" tag to add a caption to a table.

Example

Preview

Creating nested tables

Nesting tables simply implies putting one table within another. Nesting tables can result in sophisticated table layouts that are visually appealing but have the potential to introduce problems.

Example

Preview

FAQs

To make an HTML table responsive, you can use CSS media queries to adjust the table's layout based on the screen size. One way to make a table responsive is to set its width property to 100% and add the overflow-x: auto; property to make it horizontally scrollable on smaller screens. Additionally, you can set a max-width for the table to prevent it from overflowing on larger screens. Another approach is to use the display: block; property on the table and each table cell to stack them vertically on smaller screens.

To sort data in an HTML table, you can use JavaScript or a jQuery plugin like tablesorter. With JavaScript, you can write a function that sorts the table rows based on the values in a column. For example, you can use the sort() method to sort an array of table rows, and then append the sorted rows back to the table.

With tablesorter, you can add sorting functionality to a table by including the plugin script and initializing it on the table element. The plugin allows sorting by clicking on column headers and supports multiple sort criteria. You’ll learn about this later in the JavaScript or jQuery tutorial.

HTML tables have various applications, including organizing and presenting data in a structured way, displaying product comparisons and pricing tables, creating calendars and schedules, and formatting web page layouts.

Tables are commonly used in e-commerce websites to compare product features and prices or to display product reviews. They are also used in financial websites to display stock prices and other financial data. HTML tables can be styled with CSS to make them visually appealing and responsive, and JavaScript can be used to add sorting, filtering, and other interactive features.