HTML Tags – Table Tag


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.

SnoAttributesValueDescription
1alignleft
right
center
Deprecated. Specifies the alignment of the table according to parent element.
2bgcolorcolorname
#xxxxxx
rgb(x,x,x)
Deprecated. Specifies the background color of the table.
3border1
0
Deprecated. Specifies the border thickness around table.
“0” value means no border.
4cellpaddingpixels
%
Deprecated. Specifies the space between the cell borders and the cell contents.
5cellspacingpixels
%
Deprecated. Specifies the space between cells.
6frameabove
below
box
border
hsides
lhs
rhs
void
vsides
Deprecated. Specifies which side of the outside borders should be visible.
7rulesall
cols
groups
none
rows
Deprecated. Specifies which parts of the inside borders should be visible.
8summarytextDeprecated. Specifies the table summary of the content.
9widthpixels
%
Deprecated. Specifies the width of the table.

Global Attributes

HTML <table> tag support following global attributes.

SnoAttributesValueDescription
1idunique_nameDeclared unique id for an element.
2classclass_nameDeclared one or more classnames for an element.
3stylestylesCSS inline styles specify an element.
4titletitleSpecify extra details of element contain, this will display as a “tooltip” for an elements.

Event Attributes

HTML <table> tag support following event attributes.

SnoAttributesValueDescription
1onfocusscriptelement gets focus on object when script tobe run.
2onblurscriptelement lose the focus on object when scrip tobe run.
3onabortscriptelement gets aborted on object when script tobe run.
4onchangescriptelement gets anytime change on object when script tobe run.
5onbeforeunloadscriptelement gets unloaded on object when scrip tobe run.
6onclickscriptclicked on object when script tobe run.
7ondblclickscriptdouble click on object when script tobe run.
8onkeydownscriptkey is pressed when script tobe run.
9onkeypressscriptkey is pressed over element then released when script tobe run.
10onkeyupscriptkey is released over element when script tobe run.
11onmousedownscriptmouse button was pressed over an element when script tobe run.
12onmouseoutscriptmouse pointer release over an element when script tobe run.
13onmousemovescriptrun mouse pointer moved when script tobe run.
14onmouseoverscriptrun mouse pointer move over when script tobe run.
15onmouseupscriptmouse button is released when script tobe run.
16onresetscriptform has been reset when script tobe run.
17onselectscriptSelect some content when script tobe run.
18onsubmitscriptform has been submitted when script tobe run.
19onloadscriptobject has load when script tobe run.
20onchangescriptallow to change the object when script tobe run.
21onunloadscriptunload to the browser window when script tobe run.
22ondragscriptelement being dragged when script tobe run.
23ondragendscriptelement being stop dragged when script tobe run.
24ondragenterscriptelement being go target dragged when script tobe run.
25ondragleavescriptelement being leave to target dragged when script tobe run.
26ondragoverscriptelement being over to target dragged when script tobe run.
27ondragstartscriptelement being start dragged when script tobe run.
28ondropscriptelement being dropped when script tobe run.
29onerrorscriptelement error occurs when script tobe run.
30onmessagescriptelement message display when script tobe run.
31onerrorscriptelement error occurs when script tobe run.
32onmousewheelscriptmouse wheel will be rotate when script tobe run.
33onscrollscriptscrollbar is scroll when script tobe run.
34onresizescriptelement should be resize when script tobe run.
35onselectscriptall element content selected when script tobe run.
36onstoragescriptelement should be store in target when script tobe run.

Browser Compatibility

SnoBrowserSupport
1ChromeYes
2FirefoxYes
3EdgeYes
4OperaYes
5SafariYes
6Internet ExplorerYes