Quick Reach
- 1 What is HTML Table tag?
- 2 HTML table online examples
- 3 HTML table tag
- 4 The table <tr> tag
- 5 The table <td> tag
- 6 Creating a Table with <th> tag
- 7 HTML table border
- 8 HTML table border color
- 9 The table style
- 10 HTML table padding
- 11 HTML colspan
- 12 HTML rowspan
- 13 The table background
- 14 Table background image
- 15 CSS based Tables
- 16 Related
What is HTML Table tag?
The HTML table is a way to present information in a tabular format in your HTML documents. The data is presented in the form of rows and columns in tables, generally.
You can apply styles to the table rows and columns by using style attribute with CSS properties. With CSS3 you can also apply shades, round edged tables as well.
You can place text, images, links, forms and even tables inside HTML tables.
In this tutorial, we will show you how to use <table> tag of HTML along with useful attributes to style tables with examples.
HTML table online examples
A few simple examples that you can see online with code are:
Example of using HTML <th> tag
Example of HTML table with no border
Example of HTML border with value = 2
Table border with color example
Table background color example with bgcolor attribute
HTML table tag
The tag used to define a table is:
<table> </table>
For example, a basic table structure could be:
1
2
3
4
5
6
7
8
9
10
11
|
<table border=“1”>
<tr>
<td>Some table data</td>
<td>Some table data</td>
</tr>
</table>
|
The table <tr> tag
TR stands for the table row. As such table in HTML is the combination of rows and columns, you have to define table rows and columns after starting a table. The <tr> tag is used to define table row as follows:
<table>
<tr>
……
…..
</tr>
</table>
So after opening a table tag, you have to use the <tr> tag to define a row in a table. This will create a row in HTML table. You can create as many rows as required by using <tr> tag inside the <table> tag.
Example of using <tr> tag
1
2
3
4
5
6
7
8
9
10
11
|
<table border=“1”>
<tr>
<td>Table row example</td>
<td> Table row example </td>
</tr>
</table>
|
The table <td> tag
TD stands for table data. After creating rows in a table, you have to present data for what you have created a table. The <td> tag is used to create columns inside the table row <tr>. A <td> is also called as table cell. You can create as many columns as required depending on the task requirement and space of table.
You can create as many columns as required depending on the task requirement and space of table.
This is how you can define table columns by using the <td> tag and placing table data.
<table>
<tr>
<td>
</td>
</tr>
</table>
So after the table tag, it tells HTML to create a table. The <tr> tag will create a table row. After <tr>, the <td> tag is used to create a column in the table.
The above structure creates one column inside a row of HTML table. You can place more <td> tags to place more columns to show data. After each opening <td>, you have to close it by using closing tag </td> for each column.
Example of using HTML <td> tag
The example below shows a table with employee names, age, and salaries.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1”>
<tr>
<td>Employee Name</td>
<td>Employee Age</td>
<td>Employee Salary</td>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
<tr>
<td>Adams</td>
<td>31</td>
<td>$4000.00</td>
</tr>
</table>
</body>
</html>
|
First of all, a table is created with the <table> tag. Then a row is created that contains three column by using <td> tags. This constitutes the first row.
As we are using only three columns or <td>, </tr> will tell this is the end of row. The column in first row contains Employee name, the second column contains the Employee age, and the third column shows employee salary.
Then we started the second row by again using <tr> and created three columns again with an employee’s data.
Creating a Table with <th> tag
The table contains rows and columns. Each <td> also represents a cell in HTML table. The other type of tag that represents a cell in a table is the <th>.
The <th> tag stands for Table header. As the name shows, this tag is used to define Headers in a table. The <th> tag is also placed inside the <tr> tag.
As such <th> defines the table header, the data inside <th> cells is by default bold and center aligned. Whereas <td> data is left aligned with the normal text.
An example of using HTML <th> tag
The example below shows a table with employee names, age, and salaries. The headers are placed in the <th> tags.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
<tr>
<td>Adams</td>
<td>31</td>
<td>$4000.00</td>
</tr>
</table>
</body>
</html>
|
As you can see, the employee name, Employee age, and Employee Salary are placed inside <tr> tags. All these are showing centered and bold by itself.
HTML table border
Generally, tables are shown with borders to distinguish information. However, by default, no border is shown for HTML tables. i.e. if you do not specify or use border attribute it will be taken as “0”.
The table attribute border is used to define the border of a table.
The syntax of border attribute is:
<table border=”1”>
The more value is given the border line will be thicker.
An example of HTML border with 0 value
The example below uses “0” value for the border. See the code and output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“0”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
</table>
</body>
</html>
|
So no border line will be shown.
Example of HTML border with value = 2
The example below uses “2” value for the border attribute of the table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“2”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
</table>
</body>
</html>
|
Increase or decrease the value of border as it suits to your webpage.
HTML table border color
You can also define the color for HTML border. The default color is black. However, to blend it to the other parts of the webpage, you may need to set some other color of the border of a table.
The border color of a table can be set by using CSS. See example below to give color to a table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<html>
<head>
<title>HTML table demo</title>
<style>
.table, th, td
{
border:1px solid red;
}
</style>
</head>
<body>
<table>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
</table>
</body>
</html>
|
The table style
You can use the Style attribute in table tag to define properties of the table or cell data. You can set properties like table border, height, and width of the table, the font size of cell text, color of text etc.
See example below for how to use style attribute in table tag:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1” style=“width: 300px;height: 400;font-size:15px;color: red;”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
</table>
</body>
</html>
|
Similarly you can place Style attribute inside <tr> or <td> tags to define the <td> width, color etc.
HTML table padding
By default, the cell data is aligned left and very close to the border of a cell. To increase the distance between content and border of a cell you can use the CSS padding property.
The example below shows how you may use the table padding.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<html>
<head>
<title>HTML table demo</title>
<style>
td
{
padding:20px;
}
</style>
</head>
<body>
<table border=“1” style=“width: 300px;height: 400;font-size:15px;color:red;”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
</table>
</body>
</html>
|
The above example will apply to all <td>s of the table. In order to apply padding to single column <td>, you can use style at <td> level. See example below
The table padding at single column level example
The example below shows applying padding to a single <td> cell.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1” style=“width: 300px;height: 400;font-size:15px;color:red;”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td style=“padding:20px;”>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
</table>
</body>
</html>
|
You can see only “Mike” <td> is distant 20px from the border line.
HTML colspan
The colspan in HTML table is used to merge two cells into a single column. For example, if you have three columns and want to merge second and third column into one column then in the second column you can place colspan=”2”. And if want to merge three cells into one then use colspan=”3” and so on.
See example below of using the table colspan:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1” style=“width: 300px;height: 400;font-size:15px;color:red;”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
</tr>
<tr>
<td colspan=“3” style=“padding:20px;text-align:center;”>N/A</td>
</tr>
</table>
</body>
</html>
|
As you can see, the second row’s three columns are merged into the single column.
HTML rowspan
Similarly, you can use rowspan to merge two or more rows into the single row. You can place rowspan inside <td> tag to merge two or more cells into one row.
See example below to see the usage of table rowspan.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1”>
<tr>
<th></th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td rowspan=“3”>Employee’s data</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
<tr>
<td>Adams</td>
<td>31</td>
<td>$4000.00</td>
</tr>
</table>
</body>
</html>
|
The table background
You can set the background color of whole table or whole row or even a single cell. In order to set table background, you can use table attribute bgcolor. Alternatively, you can use background attribute if you want to set an image as background.
Table background color Example with bg color attribute
The example below shows how to set the background color of a table with bgcolor attribute.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1” bgcolor=“lightgray”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
<tr>
<td>Adams</td>
<td>31</td>
<td>$4000.00</td>
</tr>
</table>
</body>
</html>
|
The whole table background color is set to light gray color.
The table background color for single row example
Sometimes, you need to have different colors of different rows in order to stand out specific rows.
The example below shows how to set the background color of only single row by using bgcolor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1” >
<tr bgcolor=“lightgray”>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
<tr>
<td>Adams</td>
<td>31</td>
<td>$4000.00</td>
</tr>
</table>
</body>
</html>
|
As you can see, the whole table header color is set to light gray color.
Table background image
You can set the background of the whole table, specific rows or a specific cell by using the background attribute.
See examples below how to set the background image of a table.
Setting whole table background image
The example below shows how to set table background of the whole table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1” background=“TC_logo.png”>
<tr>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
<td>$5000.00</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
<tr>
<td>Adams</td>
<td>31</td>
<td>$4000.00</td>
</tr>
</table>
</body>
</html>
|
The table background image for cell
The example below sets the background image of a cell.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<html>
<head>
<title>HTML table demo</title>
</head>
<body>
<table border=“1”>
<tr>
<th></th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
</tr>
<tr>
<td rowspan=“3” background=“TC_logo.png”>Employee’s data</td>
</tr>
<tr>
<td>Tina</td>
<td>24</td>
<td>$3500.00</td>
</tr>
<tr>
<td>Adams</td>
<td>31</td>
<td>$4000.00</td>
</tr>
</table>
</body>
</html>
|
CSS based Tables
You can also draw table and set all properties of the table, table headings, table rows and cells by using CSS.
These days, the preferred way of styling the table is by CSS. Due to its importance, we have written a comprehensive guide in CSS table chapter that describes working with CSS and tables.
Leave A Comment?