What is a HTML table ?
HTML table allows developers to arrange data into rows and columns in the webpage.
HTML table is a two-dimensional table comprised of rows and columns of cells containing data.
What is the structure of a HTML table ?
1. <table></table> tag defines an HTML table.
2. <caption></caption> tag defines description/purpose of the table.
3. <th></th> tag defines the heading element of the table. By default the elements declared inside will be bold and centered.
4. <tr></tr> tag defines the row of the table.
5. <td></td> tag defines the column of the table.
6. <colgroup></colgroup> tag defines the group of one or more columns in a table for formatting.
7. <col></col> tag defines the element to specify column properties for each column.
What is the use of HTML tables?
For presentation purpose with various text/data information and numerical data in structured format.
Used to compare two or more items in tabular form layout.
In Database tables are used to create database tables..
What are the attributes in table?
Attributes for table listed below,
1. id
2. class
3. rowspan
4. colspan
HTML beginners tutorial !!!
Click to Learn More about – HTML Tutorial for beginners
1. Simple table creation without border
Examples :
<!DOCTYPE html>
<html>
<head>
<title>Table tag</title>
</head>
<body>
<table>
<caption>Exam Mark Details</caption>
<tr>
<th>Sno</th>
<th>Name</th>
<th>English</th>
<th>Tamil</th>
<th>Maths</th>
<th>Botany</th>
<th>Zoology</th>
<th>History</th>
<th>Geography</th>
<th>Total</th>
</tr>
<tr>
<td>1</td>
<td>Gandhi</td>
<td>89</td>
<td>90</td>
<td>90</td>
<td>35</td>
<td>56</td>
<td>80</td>
<td>67</td>
<td>507/700</td>
</tr>
<tr>
<td>2</td>
<td>Abdul</td>
<td>89</td>
<td>90</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>537/700</td>
</tr>
<tr>
<td>3</td>
<td>Tendulkar</td>
<td>89</td>
<td>100</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>547/700</td>
</tr>
</table>
</body>
</html>
Output :
2. Table with border.
Examples :
<!DOCTYPE html>
<html>
<head>
<title>Table tag</title>
</head>
<body>
<table border="1px">
<caption>Exam Mark Details</caption>
<tr>
<th>Sno</th>
<th>Name</th>
<th>English</th>
<th>Tamil</th>
<th>Maths</th>
<th>Botany</th>
<th>Zoology</th>
<th>History</th>
<th>Geography</th>
<th>Total</th>
</tr>
<tr>
<td>1</td>
<td>Gandhi</td>
<td>89</td>
<td>90</td>
<td>90</td>
<td>35</td>
<td>56</td>
<td>80</td>
<td>67</td>
<td>507/700</td>
</tr>
<tr>
<td>2</td>
<td>Abdul</td>
<td>89</td>
<td>90</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>537/700</td>
</tr>
<tr>
<td>3</td>
<td>Tendulkar</td>
<td>89</td>
<td>100</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>547/700</td>
</tr>
</table>
</body>
</html>
Output :
3. Table with id attribute
Examples :
<!DOCTYPE html>
<html>
<head>
<title>Table tag</title>
<style>
#t1{
width:50%;
border:1px solid blue;
}
</style>
</head>
<body>
<table border="" id="t1">
<caption>Exam Mark Details</caption>
<tr>
<th>Sno</th>
<th>Name</th>
<th>English</th>
<th>Tamil</th>
<th>Maths</th>
<th>Botany</th>
<th>Zoology</th>
<th>History</th>
<th>Geography</th>
<th>Total</th>
</tr>
<tr>
<td>1</td>
<td>Gandhi</td>
<td>89</td>
<td>90</td>
<td>90</td>
<td>35</td>
<td>56</td>
<td>80</td>
<td>67</td>
<td>507/700</td>
</tr>
<tr>
<td>2</td>
<td>Abdul</td>
<td>89</td>
<td>90</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>537/700</td>
</tr>
<tr>
<td>3</td>
<td>Tendulkar</td>
<td>89</td>
<td>100</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>547/700</td>
</tr>
</table>
</body>
</html>
Output :
4. Table with class attribute
Examples :
<!DOCTYPE html>
<html>
<head>
<title>Table tag</title>
<style>
.t1{
width:50%;
border:1px solid blue;
}
</style>
</head>
<body>
<table border="" class="t1">
<caption>Exam Mark Details</caption>
<tr>
<th>Sno</th>
<th>Name</th>
<th>English</th>
<th>Tamil</th>
<th>Maths</th>
<th>Botany</th>
<th>Zoology</th>
<th>History</th>
<th>Geography</th>
<th>Total</th>
</tr>
<tr>
<td>1</td>
<td>Gandhi</td>
<td>89</td>
<td>90</td>
<td>90</td>
<td>35</td>
<td>56</td>
<td>80</td>
<td>67</td>
<td>507/700</td>
</tr>
<tr>
<td>2</td>
<td>Abdul</td>
<td>89</td>
<td>90</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>537/700</td>
</tr>
<tr>
<td>3</td>
<td>Tendulkar</td>
<td>89</td>
<td>100</td>
<td>100</td>
<td>35</td>
<td>56</td>
<td>100</td>
<td>67</td>
<td>547/700</td>
</tr>
</table>
</body>
</html>
Output :
5. Table with rowspan
Examples :
<!DOCTYPE html>
<html>
<head>
<title>Table tag</title>
<style>
.t1{
width:30%;
border:1px solid blue;
}
</style>
</head>
<body>
<table class="t1" border="">
<caption>Mobile Details</caption>
<tr>
<th>Name:</th>
<td>Gandhi</td>
</tr>
<tr>
<th rowspan="4">Telephone:</th>
<td>1234567890</td>
</tr>
<tr>
<td>0987654321</td>
</tr>
<tr>
<td>5432167890</td>
</tr>
<tr>
<td>6789012345</td>
</tr>
</table>
</body>
</html>
Output :
6. Table with colspan
Example :
<!DOCTYPE html>
<html>
<head>
<title>Table tag</title>
<style>
.t1{
width:30%;
border:1px solid blue;
}
</style>
</head>
<body>
<table class="t1" border="">
<tr>
<th>Name</th>
<th colspan="4">Telephone</th>
</tr>
<tr>
<td>MKGandhi</td>
<td>1234567890</td>
<td>0987654321</td>
<td>5432167890</td>
<td>6789054321</td>
</tr>
</table>
</body>
</html>
Output :
<table> Tag Attributes
HTML <table>
tag support following specific attributes.
All following attributes has been removed in HTML5.
Sno | Attributes | Value | Description |
---|---|---|---|
1 | align | left right center | Deprecated. Specifies the alignment of the table according to parent element. |
2 | bgcolor | colorname #xxxxxx rgb(x,x,x) | Deprecated. Specifies the background color of the table. |
3 | border | 1 0 | Deprecated. Specifies the border thickness around table. “0” value means no border. |
4 | cellpadding | pixels % | Deprecated. Specifies the space between the cell borders and the cell contents. |
5 | cellspacing | pixels % | Deprecated. Specifies the space between cells. |
6 | frame | above below box border hsides lhs rhs void vsides | Deprecated. Specifies which side of the outside borders should be visible. |
7 | rules | all cols groups none rows | Deprecated. Specifies which parts of the inside borders should be visible. |
8 | summary | text | Deprecated. Specifies the table summary of the content. |
9 | width | pixels % | Deprecated. Specifies the width of the table. |
Global Attributes
HTML <table>
tag support following global attributes.
Sno | Attributes | Value | Description |
---|---|---|---|
1 | id | unique_name | Declared unique id for an element. |
2 | class | class_name | Declared one or more classnames for an element. |
3 | style | styles | CSS inline styles specify an element. |
4 | title | title | Specify extra details of element contain, this will display as a “tooltip” for an elements. |
Event Attributes
HTML <table>
tag support following event attributes.
Sno | Attributes | Value | Description |
---|---|---|---|
1 | onfocus | script | element gets focus on object when script tobe run. |
2 | onblur | script | element lose the focus on object when scrip tobe run. |
3 | onabort | script | element gets aborted on object when script tobe run. |
4 | onchange | script | element gets anytime change on object when script tobe run. |
5 | onbeforeunload | script | element gets unloaded on object when scrip tobe run. |
6 | onclick | script | clicked on object when script tobe run. |
7 | ondblclick | script | double click on object when script tobe run. |
8 | onkeydown | script | key is pressed when script tobe run. |
9 | onkeypress | script | key is pressed over element then released when script tobe run. |
10 | onkeyup | script | key is released over element when script tobe run. |
11 | onmousedown | script | mouse button was pressed over an element when script tobe run. |
12 | onmouseout | script | mouse pointer release over an element when script tobe run. |
13 | onmousemove | script | run mouse pointer moved when script tobe run. |
14 | onmouseover | script | run mouse pointer move over when script tobe run. |
15 | onmouseup | script | mouse button is released when script tobe run. |
16 | onreset | script | form has been reset when script tobe run. |
17 | onselect | script | Select some content when script tobe run. |
18 | onsubmit | script | form has been submitted when script tobe run. |
19 | onload | script | object has load when script tobe run. |
20 | onchange | script | allow to change the object when script tobe run. |
21 | onunload | script | unload to the browser window when script tobe run. |
22 | ondrag | script | element being dragged when script tobe run. |
23 | ondragend | script | element being stop dragged when script tobe run. |
24 | ondragenter | script | element being go target dragged when script tobe run. |
25 | ondragleave | script | element being leave to target dragged when script tobe run. |
26 | ondragover | script | element being over to target dragged when script tobe run. |
27 | ondragstart | script | element being start dragged when script tobe run. |
28 | ondrop | script | element being dropped when script tobe run. |
29 | onerror | script | element error occurs when script tobe run. |
30 | onmessage | script | element message display when script tobe run. |
31 | onerror | script | element error occurs when script tobe run. |
32 | onmousewheel | script | mouse wheel will be rotate when script tobe run. |
33 | onscroll | script | scrollbar is scroll when script tobe run. |
34 | onresize | script | element should be resize when script tobe run. |
35 | onselect | script | all element content selected when script tobe run. |
36 | onstorage | script | element should be store in target when script tobe run. |
Browser Compatibility
Sno | Browser | Support |
1 | Chrome | Yes |
2 | Firefox | Yes |
3 | Edge | Yes |
4 | Opera | Yes |
5 | Safari | Yes |
6 | Internet Explorer | Yes |