×

HTML <table> tag

The Table element

If we want to show the data and information in a two-dimensional table with rows and columns, then we can achieve this by using the HTML element <table>.

Syntax:

<body>
<table>
<thead>
<tr>
<thcolspan="#">………….</th>
</tr>
</thead>
<tbody>
<tr>
<td>……….</td>
<td>……….</td>
</tr>
</tbody>
</table>
</body>

Example:

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<table>
<thead>
<tr>
<thcolspan="2">The table header</th>
</tr>
</thead>
<tbody>
<tr>
<td>The table body</td>
<td>with two columns</td>
</tr>
</tbody>
</table>
</body>
</html>

CSS:

table,
td {
border: 1px solid #333;
}


thead,
tfoot {
background-color: #333;
color: #fff;
}

Output:

HTML Table Tag

Attributes

Align

The values hold by align attribute are right, left, center, justify and char. This attribute is used to determine the visual alignment.

Left:    

We use the left property within the align tag to show the table on the left side of the web page.

<table align="left">

center:

We utilize the center property within the align tag to show the table at the center on the web page.

< table align="center">

right:

We utilize the right property within the align tag to show the table on the right side of the web page.

<table align="right">


<table style="margin-left: 50px;">
<table style="margin-right: 50px;">

Bgcolor

The background colour of the table can be achieved by bgcolor. It is a hexadecimal RGB code with six digits and a "#" prefix. You can also write the name for a color.

You can also use the following CSS background-color property to get the same output:

<tbody>
<tr>
<td bgcolor="red">The table body</td>
<td>with two columns</td>
</tr>
</tbody>

border

This numeric parameter in pixels specifies the size of the table's frame. If it setsas 0, the frame property is set to void.

You can also use the following CSS border shorthand element to get the same output:

<table border="2">

cellpadding

This element defines the distance between the information and the corresponding border. If we specify the length of cellpading in pixels, then the pixel will be given to each corner of the cell. If the length is defined using a percentage figure, the content will be centred and the entire vertical space (top and bottom) will be used this value. The whole horizontal space remains constant (left and right).

Similar results may be obtained by giving the border-collapse property to the table tag and the padding attribute to the td elements.

cellspacing

You can specify cellspacing feature either in percentage or pixel for defining the space between the two cells.

The region between the top of the table and the boxes of the first row, the left of the table and the first column, the right of the table and the final column, and the bottom of the table and the last row is also included in the tag's horizontal and vertical coverage.

You can also use the border-spacing property on the table> tag to get the same result. Border-spacing has no effect if border-collapse is set to collapse.

frame

This property specifies which side of the frame to be shown around the table. You can also use the border-style and border-width properties to get the same output.

rules

This enumerated attribute describes the rules to be placed in the table. This attribute contains the following values:

None:

None value of rules attribute denotes the absence of any rules.

Groups:

The rules are only displayed between thead, tbody, and tfoot tags as well as between column groups. It is provided by the col> and colgroup> attributes.

Rows:

The rules will be shown between the rows as a consequence of the rows.

Columns:

The rules will appear between the columns as a consequence.

Bgcolor:-

This attribute of the <table> tag determines the background color of the table.

summary

This attribute specifies the alternative text for table’s content. You can also use the caption tag instead of this attribute.

width

This parameter specifies the width of the table. You can also use the following CSS width property instead of width attribute:

<table style="width: 100px;">

Examples

Simple table

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<table>
<tr>
<td>deepanshu</td>
<td>lather</td>
</tr>
<tr>
<td>deepanshu</td>
<td>lather</td>
</tr>
</table>
</body>
</html>

Output:

HTML Table Tag

Example:

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<p>Simple table with header</p>
<table>
<tr>
<th>First name</th>
<th>middle name</th>
</tr>
<tr>
<td>deepanshu</td>
<td>lather</td>
</tr>
<tr>
<td>jaideep</td>
<td>lather</td>
</tr>
</table>
</body>
</html>

Output:

HTML table tag

Example:

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<p>Table havingthead, tfoot, and tbody tags in it</p>
<table>
<thead>
<tr>
<th>sakshi</th>
<th>lather</th>
</tr>
</thead>
<tbody>
<tr>
<td>navdeep</td>
<td>dhanda</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>family members</td>
<td>childrens</td>
</tr>
</tfoot>
</table>
<p>Table with colgroup</p>
<table>
<colgroup span="4"></colgroup>
<tr>
<th>player</th>
<th>user name</th>
<th>annual income</th>
<th>server</th>
</tr>
<tr>
<td>almighty 01</td>
<td> almighty 999 yt</td>
<td>656 million</td>
<td>india</td>
</tr>
<tr>
<td>jai</td>
<td>devil hunt 01</td>
<td>915 million</td>
<td>india</td>
</tr>
</table>
</body>
</html>

Output:

HTML Table Tag

Example:

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<p>Table with colgroup and col</p>
<table>
<colgroup>
<col style="background-color: #0f0">
<col span="2">
</colgroup>
<tr>
<th>apple</th>
<th>banana</th>
<th>mango</th>
</tr>
<tr>
<td>red</td>
<td>blue</td>
<td>white</td>
</tr>
</table>
</body>
</html>

Output:

HTML Table Tag

Example:

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<p>Simple table with caption</p>
<table>
<caption>Awesome caption</caption>
<tr>
<td>Awesome data</td>
</tr>
</table>
</body>
</html>

Output:

HTML Table Tag

Table sorting

The rows of an HTML table cannot be sorted automatically. However, you may construct your own sort() function to order an HTMLCollection of <tr> components by utilising the following functions:

1. Array.prototype.slice(),

2. Array.prototype.sort(),

3. Node.removeChild(), and

4. Node.appendChild().

You can understand by using the below example:

Example:

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<table>
<tbody>
<tr>
<td>3</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>1</td>
</tr>
</tbody>
</table>
</body>
</html>

Output:

HTML Table Tag

JAVASCRIPT:

HTMLTableSectionElement.prototype.sort = function(cb){
Array
.prototype
.slice
.call(this.rows)
.sort(cb)
.forEach((e,i,a)=>{
this.appendChild(this.removeChild(e));
},this);
}


document.querySelector('table').tBodies[0].sort(function(a, b){
return a.textContent.localeCompare(b.textContent);
});

Sorting rows with a click on the th element

The below example organises all of the rows in the <tbody> based on the <td> cells present in the rows and adds an event handler to each <th> element of each table in the document.

HTML:

<!DOCTYPE html>
<html>
<head>
<title>
Html table tag
</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Numbers</th>
<th>Letters</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>A</td>
</tr>
<tr>
<td>2</td>
<td>B</td>
</tr>
<tr>
<td>1</td>
<td>C</td>
</tr>
</tbody>
</table>
</body>
</html>

Output:

HTML Table Tag

JAVASCRIPT:

const allTables  = document .query Selector All (' table ' );


for (let table of all Tables) {
const tBody = table.tBodies[0];
const rows = Array.from(tBody.rows);
const headerCells =  table.tHead.rows [0]. cells;


for (let th of headerCells) {
const cellIndex = th.cellIndex;


th.add EventListener ('click', () => {
rows.sort((tr1, tr2) => {
const tr1 Text = tr1. cells[cellIndex].textContent;
const tr2 Text = tr2. cells[cellIndex].textContent;
return  tr1Text.localeCompare(tr2Text);
});


tBody. Append (...rows);
});
}
}

CSS:

table,
th,
td {
border: 1px solid;
}


table {
width : 100 % ;
max - width: 400px ;
height : 240 px ;
margin : 0 auto ;
display: block ;
overflow -x: auto ;
border - spacing : 0 
}


tbody {
white-space: nowrap;
}


th,
td {
padding : 5px 10px;
border - top-width : 0;
border - left - width : 0 ;
}


th {
position: sticky;
top: 0;
background: #fff;
vertical - align: bottom;
}


th:last-child,
td:last-child {
border-right-width: 0;
}


tr:last-child td {
border-bottom-width: 0;
}

Use Of Caption element of table tag:

The caption tag of element tag is used just after table tag tag. The text which is written inside the caption tag should determine the nature of the table. We can also use only one caption element in table tag.

Browser support:

List of browsers that support html table tag are mentioned below:

Chrome: Yes, version 1 needed

Microsoft Edge: Yes, version 12 needed

Firefox: Yes, version 1 needed

Safari: Yes

Opera: Yes


Related Topics

HTML Font Color

The HTML <font> tag has an attribute called color, which defines the color of the text. We use the color attribute inside the <font> tag to specify the color of...

6 minutes read.

Caption Tag in HTML

Caption Tag in HTML: The < caption > tag in HTML describes the heading of a table in the HTML text. Browsers typically make the text found above the table...

4 minutes read.

HTML Hide Element

The HTML hide element is used to hide the element. We place the hidden attribute inside those elements which we want to hide. The hidden attribute is a Boolean attribute. The...

3 minutes read.

Most Commonly used Tags in HTML

In this tutorial, we'll look at the most popular HTML tags. We know HTML is a fundamental requirement for creating a website. As a result, it is essential to remember...

9 minutes read.

HTML <sup> Tag

The HTML sup element is used to describe one or more superscript contents. The sup tag shows a text which is presented above the regular line and half the length...

2 minutes read.

HTML Charsets

In this tutorial, we are going to discuss about the charsets in the HTML. The letters, numbers, and several other symbols are shown correctly by the web browser. All of this...

2 minutes read.

HTML Event Attributes

HTML Event Attributes If a browser responds to the user behavior, an event is called. For example, if we click on the submit button, a box of details will appear on...

4 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

Html &lt;datalist >Tag

The autocomplete feature in HTML files is provided by the <datalist> tag. It can be used in conjunction with an input tag to enable users to quickly fill out form...

4 minutes read.

HTML Tutorial

Introduction This HTML tutorial helps beginners and professionals to learn HTML easily. HTML is a widely used web technology for website development. It stands for Hyper Text Markup Language. It is mainly used to...

7 minutes read.

HTML Frames

Frames in Html is used to divide the browser into different sections where each section can load different Html documents. Collection of frame is known as Frameset. Creating frames:We use frameset...

2 minutes read.

How to change font in HTML

To give style to the content of your website, you may change the font of the text and make your website look attractive to the user. There are several methods which...

4 minutes read.

HTML <section> tag

The section element in HTML is used to define sections in a document. Syntax of Section Tag: <body> <h1>……….</h1> <section> <h2>……….</h2> <p> …………………………</p> </section> <section> <h2>………..</h2> <p>…………………………. <p> </section> </body> Examples of Section Tag Example 1: In this example, we use the HTML "section" element...

3 minutes read.

HTML <style> tag

The section of information that we want to style is contained in html style tag. It includes CSS, which is used to design the text and images in the web page. Syntax: <head> <title>…………....

3 minutes read.

Html Wbr Tag

The Line Break Opportunity element A word break opportunity is a place in text from where the browser breaks a line. This opportunity is represented by the element wbr in the...

2 minutes read.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

HTML <u> tag

Underline Element/Tag: The u tag in html is used to specify the important text or phrase by underline. In earlier HTML versions, this element was known as the "Underline" element, and...

5 minutes read.

How to insert spaces/tabs in text using HTML/CSS

What is HTML? HTML or HyperText Markup Language is used to design web pages. It was released in 1993. The file extension name for HTML file is “.html”. An HTML file...

4 minutes read.

HTML5 Button Tag

HTML Button Tag The < button > tag is used on the webpage to construct a clickable button inside the HTML. Within the < button>....</button > tag, we can place content...

2 minutes read.

HTML Code Tag

The code tag in HTML is a phrase tag used to insert a piece of computer programming code. In HTML there is a separate tag named as <code> tag. Sometimes,...

3 minutes read.