Professional Documents
Culture Documents
WORKING WITH
TABLES
WHAT IS A TABLE IN HTML?
<!DOCTYPE html>
<html> <tr>
<body> <td>Rohit</td>
<table style="width:100 %"> <td>Kumar</td>
<tr> <td>30</td>
<th>Firstname</th> </tr>
<th>Lastname</th> </table>
<th>Age</th>
</tr> </body>
</html>
<tr>
<td>Rahul</td>
<td>Sharma</td>
<td>20</td>
</tr>
<tr>
<td>Sahil</td>
<td>Verma</td>
<td>22</td>
</tr>
HTML TABLE - ADDING A BORDER
Example
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
HTML TABLE - COLLAPSED BORDERS
If you want the borders to collapse into one border, add the CSS
border-collapse property:
Example
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
HTML TABLE – ST YLE TABLE BORDERS
If you set a background color of each cell, and give the border a
white color (the same as the document background), you get
the impression of an invisible border :
Example:
table,th,td{
border:1px solid white;
border-collapse:collapse;
}
th, td{
background-color:#96D4D4;
}
HTML TABLE – ROUND TABLE BORDERS
Example:
table,th,td{
border:1px solid white;
border-radius:10px;
}
HTML TABLE – DOTTED TABLE BORDERS
Example:
th,td{
border-style: dotted;
}
HTML TABLE SIZES
HTML tables can have dif ferent sizes for each column, row or
the entire table.
Use the style attribute with the width or height properties to
specify the size of a table, row or column .
HTML TABLE WIDTH
<!DOCTYPE html>
<html> <tr>
<body> <td>Rohit</td>
<table style="width:100 %"> <td>Kumar</td>
<tr> <td>30</td>
<th>Firstname</th> </tr>
<th>Lastname</th> </table>
<th>Age</th>
</tr> </body>
</html>
<tr>
<td>Rahul</td>
<td>Sharma</td>
<td>20</td>
</tr>
<tr>
<td>Sahil</td>
<td>Verma</td>
<td>22</td>
</tr>
HTML TABLE COLUMN WIDTH
<!DOCTYPE html>
<tr>
<html>
<td>Eve</td>
<style> <td>Jackson</td>
table, th, td { <td>94</td>
border:1px solid black; </tr>
border-collapse: collapse; <tr>
} <td>John</td>
<td>Doe</td>
</style> <td>80</td>
<body> </tr>
</table>
<h2>Set the first column to 70% of the table
width</h2>
</body>
<table style="width:100%"> </html>
<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>
HTML TABLE ROW HEIGHT
Cell padding specifies the space between the cell content and its borders.
If you do not specify a padding, the table cells will be displayed without
padding.
To set the padding, use the CSS padding property:
Example:
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
</style>
</head>
To add padding only above the content, use the padding-
top property.
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;
}
HTML TABLE - CELL SPACING
Example:
table{
border-spacing: 30px;
}
HTML TABLE - LEFT-ALIGN HEADINGS
To set the border spacing for a table, use the CSS border-spacing property:
Example:
<head>
<style>
table, th, td {
border: 1px solid black;
padding: 5px;
}
table {
border-spacing: 15px;
}
</style>
</head>
HTML TABLE - CELLS THAT SPAN MANY
COLUMNS
To make a cell span more than one column, use the colspan
attribute:
EXAMPLE
< ! D O C T Y P E ht m l >
< ht m l >
<head>
<style>
ta b l e , t h , td {
b o rd e r : 1 px s o l i d b l a c k ;
b o rd e r - co l l a ps e: co l l a ps e ;
}
t h , td {
p a d d i n g : 5 px ;
t ex t - a l i g n : l ef t ;
}
< /s t y l e >
</head>
<body>
< h 2 > C e l l t h a t s p a n s t w o co l u m n s : < /h 2 >
< ta b l e s t y l e = " w i d t h : 1 00 % " >
<tr>
<th>Name</th>
< t h co l s p a n =" 2 " > D eta i l s < / t h >
</tr>
<tr>
< td > R a h u l < / td >
< td > K 1 5 0 8 < / td >
< td > 1 8 < / td >
</tr>
</table>
</body>
< / ht m l >
HTML TABLE - CELLS THAT SPAN MANY
ROWS
To make a cell span more than one row, use the rowspan attribute:
EXAMPLE
<body>
<!DOCTYPE html> <h2>Cell that spans two rows:</h2>
<html> <table style="width:100%">
<head> <tr>
<style> <th>Name:</th>
table, th, td { <td>Rahul</td>
border: 1px solid black; </tr>
border-collapse: collapse; <tr>
<th rowspan="2">Details</th>
}
<td>K1508</td>
th, td { </tr>
padding: 5px; <tr>
text-align: left; <td>18</td>
} </tr>
</style> </table>
</head>
</body>
</html>
HTML TABLE - ADDING A CAPTION
Example:
tr:hover
{background-color: #D6ABAB;}