4 min read · Free · No login needed
A plain HTML table works, but it looks bare — thin grey lines, cramped text, no colour at all. CSS turns that same table into a clean menu, a neat timetable, or a readable marksheet. You do not change the HTML. You only add style.
Tables are used everywhere: a chai shop menu, a school timetable, a cricket scorecard, an exam result list. If people must read rows of data, the table has to be easy on the eyes. That is the whole job of CSS tables.
Give the table, the headings and the cells a border with the border property. Without one more property, you get a strange result: every cell draws its own border, so you see double lines between cells. The fix is border-collapse: collapse on the table. It merges the touching borders into a single clean line.
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #999;
padding: 10px;
}
Three small properties do most of the work. width decides how wide the table is — 100% makes it fill its container. padding puts breathing space inside each cell, so the text does not touch the border. text-align lines the text up: headings and names on the left, numbers and prices on the right.
th, td {
padding: 12px;
text-align: left;
}
.price {
text-align: right;
}
When a table has many rows, the eye slips to the wrong line. Zebra stripes fix that: give every even row a light background colour. The selector tr:nth-child(even) picks rows 2, 4, 6 and so on. You will meet :nth-child properly in the pseudo-classes lesson.
tr:nth-child(even) {
background-color: #f2f2f2;
}
A hover row lights up the row under the mouse. It is a small touch, but it makes a long table much easier to read — the reader always knows which row they are on.
tr:hover {
background-color: #ddd;
}
A wide table breaks a phone screen: it either squeezes the text or pushes the page sideways. The simple fix is to wrap the table in a <div> with overflow-x: auto. On a small screen the table scrolls sideways inside its own box, and the rest of the page stays still.
<div style="overflow-x: auto;"> <table> ... </table> </div>
border-collapse: collapse is the property beginners forget most. Without it, your table shows double borders — two thin lines with a tiny gap between them — and no amount of border styling will make it look right.border-collapse: collapse and wondering why the borders look doubled.<br> or extra spaces for spacing inside cells instead of padding.border-collapse: collapse turns double borders into single clean lines.padding gives the text room inside each cell; text-align lines it up.tr:nth-child(even) makes zebra stripes; tr:hover lights up the row under the mouse.overflow-x: auto so it scrolls on small screens.th) differently from the data rows (td).table { width: 80%; margin: 20px auto; border-collapse: collapse; } — the table takes 80% of the page width, sits in the centre, and its borders merge into single lines.th, td { border: 1px solid #d97706; padding: 12px; text-align: left; } — every heading cell and data cell gets the same warm brown border, roomy padding, and left-aligned text.th { background-color: #92400e; color: white; } — the heading row gets a dark brown background with white text, so the column names stand out.tr:nth-child(even) { background-color: #fef3c7; } — every even row gets a pale yellow background, giving the menu its zebra stripes.tr:hover { background-color: #fde68a; } — the row under the mouse turns a deeper yellow, so the reader never loses their place..price { text-align: right; } — the price column lines up on the right, which is how prices are normally read.<!DOCTYPE html>
<html>
<head>
<title>Chai Shop Menu</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #fff8f0;
}
h1 {
text-align: center;
color: #7b3f00;
}
table {
width: 80%;
margin: 20px auto;
border-collapse: collapse;
}
th, td {
border: 1px solid #d97706;
padding: 12px;
text-align: left;
}
th {
background-color: #92400e;
color: white;
}
tr:nth-child(even) {
background-color: #fef3c7;
}
tr:hover {
background-color: #fde68a;
}
.price {
text-align: right;
}
</style>
</head>
<body>
<h1>Chai & Snacks — Menu</h1>
<div style="overflow-x: auto;">
<table>
<tr>
<th>Item</th>
<th>Size</th>
<th class="price">Price</th>
</tr>
<tr>
<td>Masala Chai</td>
<td>Small</td>
<td class="price">Rs 10</td>
</tr>
<tr>
<td>Masala Chai</td>
<td>Large</td>
<td class="price">Rs 15</td>
</tr>
<tr>
<td>Adrak Elaichi Chai</td>
<td>Large</td>
<td class="price">Rs 20</td>
</tr>
<tr>
<td>Samosa</td>
<td>1 pc</td>
<td class="price">Rs 10</td>
</tr>
<tr>
<td>Bread Pakora</td>
<td>2 pc</td>
<td class="price">Rs 20</td>
</tr>
<tr>
<td>Bun Maska</td>
<td>1 pc</td>
<td class="price">Rs 15</td>
</tr>
</table>
</div>
<p style="text-align: center;">Fresh chai served all day, 7 AM to 9 PM.</p>
</body>
</html>
Change the code below and press Run to see the result instantly. No file to save, no login needed.
AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.
Finish all 30 lessons, pass the test (80%), pay ₹10 and get your certificate.
Take the testPrefer watching? Video courses teach the same topics step by step.
Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.
Browse all coursesAsk about courses, website & more
Request human callback:
Download karne ke liye login karo. Login ke baad yahi page pe rahoge!
Naya ho? Register karo →