Day 9
Day 9
HTML 5
HTML Table Borders
HTML tables can have borders of different styles and shapes.
Example
table, th, td {
border: 1px solid black;
}
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
Example
table, th, td {
border: 1px solid white;
border-collapse: collapse;
}
th, td {
background-color: #96D4D4;
}
Example
table, th, td {
border: 1px solid black;
border-radius: 10px;
}
Skip the border around the table by leaving out table from the css selector:
Example
th, td {
border: 1px solid black;
border-radius: 10px;
}
• dotted
• dashed
• solid
• double
• groove
• ridge
• inset
• outset
• none
• hidden
Example
th, td {
border-style: dotted;
}
Border Color
With the border-color property, you can set the color of the border.
Example
th, td {
border-color: #96D4D4;
}
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border-collapse: collapse;
</style>
</head>
<body>
<h2>Collapsed Borders</h2>
<p>If you want the borders to collapse into one border, add the CSS border-collapse property.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Collapsed Borders
If you want the borders to collapse into one border, add the CSS border-collapse property.
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border-collapse: collapse;
th, td {
background-color: #96D4D4;
</style>
<body>
<p>Style the table with white borders and a background color of the cells to make the impression of invisible
borders.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
Table With Invisible Borders
Style the table with white borders and a background color of the cells to make the impression of
invisible borders.
Use the CSS border-radius property to add rounded corners to the borders.
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border-radius: 10px;
</style>
</head>
<body>
<p>Use the CSS border-radius property to add rounded corners to the borders.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Use the CSS border-radius property to add rounded corners to the table cells.
<!DOCTYPE html>
<html>
<head>
<style>
th, td {
border-radius: 10px;
</style>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
</head>
<body>
<p>Use the CSS border-radius property to add rounded corners to the table cells.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Use the CSS border-style property to set the style of the borders.
Jill Smith 50
Eve Jackson 94
John Doe 80
Code:-
<!DOCTYPE html>
<html>
<head>
<style>
th, td {
border-style: dotted;
</style>
</head>
<body>
<p>Use the CSS border-style property to set the style of the borders.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Use the CSS border-color property to set the color of the borders.
<html>
<head>
<style>
th, td {
border-style:solid;
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
border-color: #96D4D4;
</style>
</head>
<body>
<p>Use the CSS border-color property to set the color of the borders.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Use the style attribute with the width or height properties to specify the size of a
table, row or column.
Example
Set the width of the table to 100%:
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
<!DOCTYPE html>
<html>
<style>
table, th, td {
border:1px solid black;
border-collapse: collapse;
}
</style>
<body>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Note: Using a percentage as the size unit for a width means how wide will this element
be compared to its parent element, which in this case is the <body> element.
To set the size of a specific column, add the style attribute on a <th> or <td> element:
Example
Set the width of the first column to 70%:
<table style="width:100%">
<tr>
<th style="width:70%">Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
<!DOCTYPE html>
<html>
</style>
<body>
<table style="width:100%">
<tr>
<th style="width:70%">Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
</body>
</html>
Jill Smith 50
Eve Jackson 94
John Doe 80
To set the height of a specific row, add the style attribute on a table row element:
Example
Set the height of the second row to 200 pixels:
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr style="height:200px">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<!DOCTYPE html>
<html>
<style>
table, th, td {
border:1px solid black;
border-collapse: collapse;
}
</style>
<body>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr style="height:200px">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
</tr>
</table>
</body>
</html>
Jill Smith 50
Eve Jackson 94
John Doe 80
TOBIAS LINUS
EMIL
8:00
9:00
10:00
11:00
13:00
8:00
9:00
10:00
11:00
12:00
DECEMBER
Example
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<h2>Table Headers</h2>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
</body>
</html>
Table Headers
Example
<table>
<tr>
<th>Firstname</th>
<td>Jill</td>
<td>Eve</td>
</tr>
<tr>
<th>Lastname</th>
<td>Smith</td>
<td>Jackson</td>
</tr>
<tr>
<th>Age</th>
<td>94</td>
<td>50</td>
</tr>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
</style>
</head>
<body>
<p>The first column becomes table headers if you set the first table cell in each table
row to a TH element:</p>
<table style="width:100%">
<tr>
<td>Eve</td>
</tr>
<tr>
<th>Lastname</th>
<td>Smith</td>
<td>Jackson</td>
</tr>
<tr>
<th>Age</th>
<td>50</td>
<td>94</td>
</tr>
</table>
</body>
</html>
The first column becomes table headers if you set the first table cell in each table row to a TH
element:
Jill Smith 50
Example
th {
text-align: left;
}
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
th {
text-align: left;
</style>
</head>
<body>
<h2>Left-align Headers</h2>
<p>To left-align the table headers, use the CSS text-align property.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
</body>
</html>
Left-align Headers
Name Age
Jill Smith 50
Eve Jackson 94
Example
<table>
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<p>Use the colspan attribute to have a header span over multiple columns.</p>
<table style="width:100%">
<tr>
<th colspan="2">Name</th>
<th>Age</th>
Use the colspan attribute to have a header span over multiple columns.
Name Age
Jill Smith 50
Eve Jackson 94
Table Caption
You can add a caption that serves as a heading for the entire table.
Monthly savings
Month Savings
January $100
February $50
Example
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Table Caption</h2>
<p>To add a caption to a table, use the caption tag.</p>
<table style="width:100%">
<caption>Monthly savings</caption>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
</body>
</html>
Table Caption
Monthly savings
Month Savings
January $100
February $50
With Padding
With Spacing
Example
th, td {
padding: 15px;
}
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
</style>
</head>
<body>
<h2>Cellpadding</h2>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<p>Cell padding specifies the space between the cell content and its borders.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Cellpadding
Cell padding specifies the space between the cell content and its borders.
Jill Smith 50
Eve Jackson 94
John Doe 80
And the others sides with the padding-bottom, padding-left, and padding-right properties:
Example
th, td {
padding-top: 10px;
padding-bottom: 20px;
padding-left: 30px;
padding-right: 40px;
}
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding-top: 10px;
padding-bottom: 20px;
padding-left: 30px;
padding-right: 40px;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Cellpadding - top - bottom - left - right
We can specify different padding for all fours sides of the cell content.
Jill Smith 50
Eve Jackson 94
John Doe 80
To change the space between table cells, use the CSS border-spacing property on
the table element:
Example
table {
border-spacing: 30px;
}
<!DOCTYPE html>
<html>
<head>
<style>
}
table {
border-spacing: 30px;
</style>
</head>
<body>
<h2>Cellspacing</h2>
<p>Change the space between the cells with the border-spacing property.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<td>80</td>
</tr>
</table>
</body>
</html>
Cellspacing
Change the space between the cells with the border-spacing property.
NAME
APRIL
2022
FIESTA
Example
<table>
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>43</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>57</td>
</tr>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>43</td>
</tr>
<tr>
<td>Eve</td>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<td>Jackson</td>
<td>57</td>
</tr>
</table>
</body>
</html>
Cell that spans two columns
To make a cell span more than one column, use the colspan attribute.
Name Age
Jill Smith 43
Eve Jackson 57
Note: The value of the colspan attribute represents the number of columns to span.
Example
<table>
<tr>
<th>Name</th>
<td>Jill</td>
</tr>
<tr>
<th rowspan="2">Phone</th>
<td>555-1234</td>
</tr>
<tr>
<td>555-8745</td>
</tr>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<p>To make a cell span more than one row, use the rowspan attribute.</p>
<table style="width:100%">
<tr>
<th>Name</th>
<td>Jill</td>
</tr>
<tr>
<th rowspan="2">Phone</th>
<td>555-1234</td>
</tr>
<tr>
<td>555-8745</td>
</tr>
</table>
</body>
</html>
To make a cell span more than one row, use the rowspan attribute.
Name Jill
555-1234
Phone
555-8745
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
To style every other table row element, use the :nth-child(even) selector like this:
Example
tr:nth-child(even) {
background-color: #D6EEEE;
}
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #D6EEEE;
}
</style>
</head>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or
odd) table rows:
Note: If you use (odd) instead of (even), the styling will occur on row 1,3,5 etc. instead of
2,4,6 etc.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Example
td:nth-child(even), th:nth-child(even) {
background-color: #D6EEEE;
}
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
th:nth-child(even),td:nth-child(even) {
</style>
</head>
<body>
<h2>Striped Table</h2>
<p>For zebra-striped tables, use the nth-child() selector and add a background-color to
all even (or odd) table rows:</p>
<table style="width:100%">
<tr>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
<th>SAT</th>
<th>SUN</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or
odd) table rows:
Note: Put the :nth-child() selector on both th and td elements if you want to have the
styling on both headers and regular table cells.
Example
tr:nth-child(even) {
background-color: rgba(150, 212, 212, 0.4);
}
th:nth-child(even),td:nth-child(even) {
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
tr:nth-child(even) {
background-color: rgba(150, 212, 212, 0.4);
th:nth-child(even),td:nth-child(even) {
}
</style>
</head>
<body>
<h2>Striped Table</h2>
<p>For zebra-striped tables, use the nth-child() selector and add a background-color to
all even (or odd) table rows:</p>
<table style="width:100%">
<tr>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>SUN</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
Striped Table
For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or
odd) table rows:
Horizontal Dividers
Example
tr {
border-bottom: 1px solid #ddd;
}
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
tr {
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Hoverable Table
Use the :hover selector on tr to highlight table rows on mouse over:
Example
tr:hover {background-color: #D6EEEE;}
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
</style>
</head>
<body>
<h2>Hoverable Table</h2>
<p>Move the mouse over the table rows to see the effect.</p>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Hoverable Table
Move the mouse over the table rows to see the effect.
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
The <colgroup> element should be used as a container for the column specifications.
The span attribute specifies how many columns that get the style.
Example
<table>
<colgroup>
<col span="2" style="background-color: #D6EEEE">
</colgroup>
<tr>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
...
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<h2>Colgroup</h2>
<p>Add the a colgroup with a col element that spans over two columns to define a style
for the two columns:</p>
RAHUL KACHHWAHA, REWA (M.P.) INDIA +91-9993969013
Disclaimer: - This eBook is not for sale only Education Purpose Only.
HTML - Day 9
Colgroup
Add the a colgroup with a col element that spans over two columns to define a style for the two
columns:
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
Note: The <colgroup> tag must be a child of a <table> element and should be placed before
any other table elements, like <thead>, <tr>, <td> etc., but after the <caption> element, if
present.
width property
visibility property
background properties
border properties
Example
<table>
<colgroup>
<col span="2" style="background-color: #D6EEEE">
</body>
</html>
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
Empty Colgroups
If you want to style columns in the middle of a table, insert a "empty" <col> element (with
no styles) for the columns before:
Example
<table>
<colgroup>
<col span="3">
<col span="2" style="background-color: pink">
</colgroup>
<tr>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<h2>Empty Colgroups</h2>
<p>Add "empty" col elements that represents the columns before the columns you
want to style:</p>
</body>
</html>
Empty Colgroups
Add "empty" col elements that represents the columns before the columns you want to style:
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
Hide Columns
You can hide columns with the visibility: collapse property:
Example
<table>
<colgroup>
<col span="2">
<col span="3" style="visibility: collapse">
</colgroup>
<tr>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
...
<!DOCTYPE html>
<html>
<head>
<h2>Hide Columns</h2>
<p>You can hide specific columns with the visibility property:</p>
Hide Columns
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
Note: The table columns does not collapse properly in Safari browsers.