Apply border and color to a table in a webpage
To apply borders and color to a table, you use CSS properties such as border
, border-color
, border-style
, and background-color
.
Example Code:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Table Border and Color</title>
<style>
table {
width: 100%;
border-collapse: collapse; /* Ensures borders don’t double up */
}
table, th, td {
border: 2px solid black; /* Adds border to table, th, and td */
}
th, td {
padding: 10px;
text-align: center;
}
th {
background-color: lightblue; /* Background color for header */
}
tr:nth-child(even) {
background-color: lightgray; /* Alternating row colors */
}
</style>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>30</td>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
</tr>
</table>
</body>
</html>