Questions
Write a program to create html table having border and alternate columns background colors using CSS. Final Output should be like this.
Last Updated : 16 Jul 2025
Jul 23, Jan 24
Solution:
STUDENT MARK SHEET |
|||||
NAME |
Maths |
Science |
English |
Physics |
General Knowledge |
Anil Kumar |
49 |
69 |
79 |
67 |
78 |
Sunil Kumar |
58 |
78 |
87 |
85 |
87 |
Rakesh Sharma |
75 |
87 |
86 |
78 |
56 |
Farida Khan |
55 |
67 |
56 |
89 |
78 |
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
table{
width: 70%;
margin:20px auto;
border-collapse: collapse;
border:1px solid black;
}
th,td{
border: 1px solid black;
padding: 10px;
text-align: center;
}
th{
background-color: #f0f0f0;
}
td:nth-child(even){
background-color: #e6f7ff;
}
</style>
</head>
<body>
<table >
<tr>
<th colspan="6">Student Mark sheet</th>
</tr>
<tr>
<th>Name</th>
<th>Maths</th>
<th>Science</th>
<th>English</th>
<th>Physics</th>
<th>GK</th>
</tr>
<tr>
<td>Anil Kumar</td>
<td>49</td>
<td>69</td>
<td>79</td>
<td>67</td>
<td>78</td>
</tr>
<tr>
<td>Sunil Kumar</td>
<td>58</td>
<td>78</td>
<td>87</td>
<td>85</td>
<td>87</td>
</tr>
<tr>
<td>Rakesh Sharma</td>
<td>75</td>
<td>87</td>
<td>86</td>
<td>78</td>
<td>56</td>
</tr>
<tr>
<td>Farida khan</td>
<td>55</td>
<td>67</td>
<td>56</td>
<td>89</td>
<td>78</td>
</tr>
</table>
</body>
</html>
Code Explanation:
Code Explanation:
-
Table Styles:
width: 70%
: Sets the table width to 70% of the containing element.margin: 20px auto
: Centers the table horizontally with a top and bottom margin of 20px.border-collapse: collapse
: Ensures borders are collapsed into a single border.border: 1px solid black
: Adds a solid black border around the table.
-
Cell Styles:
border: 1px solid black
: Adds a solid black border around each cell.padding: 10px
: Adds padding inside cells for spacing.text-align: center
: Centers the text in cells.
-
Header Styles:
background-color: #f0f0f0
: Sets a light gray background color for header cells.
-
Row Styles:
td:nth-child(even)
: Applies a light blue background color (#e6f7ff
) to even-numbered columns in each row, creating a striped effect.
-
Table Content:
- Header Row: Contains the title "Student Mark Sheet" spanning all six columns.
- Column Headers: "Name", "Maths", "Science", "English", "Physics", "GK".
- Data Rows: Lists marks for four students: Anil Kumar, Sunil Kumar, Rakesh Sharma, and Farida Khan.
Meanwhile you can watch this video
Watch Video