CSS Tables allow you to style HTML tables with colors, borders, spacing,
hover effects, zebra striping, and responsive behavior. Proper styling
makes tables more readable and professional.
Step 1: Basic Table Styling
What this code does: Turns a plain HTML table into a neat grid. Collapsing the borders gives single thin lines between cells, padding gives every cell room to breathe, and the table stretches across the full width. In the preview you will see a two-column table (Name and Age) with a header row and one data row, all aligned to the left.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the basic table styling demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule for the table and one for its cells. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
border-collapse: collapse merges the borders of neighboring cells into a single line. Without it, each cell keeps its own border with a small gap between them, which gives a double-line look. width: 100% stretches the table across its container. By default a table is only as wide as its content.
This group selector means every th and every td inside .demo-table. Starting with .demo-table limits the rule to this table, so other tables on a page are not affected. The border goes on the cells, not on the table, because that draws lines between all the cells. #ddd is a light gray.
padding: 12px adds space inside every cell, so the text does not touch the lines. text-align: left aligns the text to the left. Without it the header cells would be centered, since th is centered by default, and they would not line up with the data below.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a table with a header row and one data row.
<table> creates the table, and the demo-table class connects it to the CSS rules above. Rows (tr) and cells (th, td) go inside it.
<tr> is a table row. Each <th> is a header cell: it is bold and centered by default, and screen readers use it to describe the data below. This first row will be styled by the th rules.
A row of data. Each <td> is a normal cell, and the cells line up with the headers above them, in the same order.
</table> closes the table.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 2: Style the Table Header
What this code does: Makes the header row stand out from the data. The header cells (th) get a blue background and white text, while the data cells (td) keep a plain white look. In the preview you will see a blue header (Student and Marks) above the row for Emma.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the table header styling demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses separate rules for header cells and data cells. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
border-collapse: collapse merges the borders of neighboring cells into a single line. Without it, each cell keeps its own border with a small gap between them, which gives a double-line look. width: 100% stretches the table across its container. By default a table is only as wide as its content.
The selector .demo-table th targets only header cells. A blue background with white text makes the header row stand out from the data. White on a dark or saturated color keeps the text readable.
The header cells get the same padding and border as the data cells, so the whole table has a consistent grid. text-align: left replaces the default centered header text.
Data cells have the same padding and a light gray border. Data cells are left-aligned by default, so no text-align is needed here.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a table with a header row and one data row.
<table> creates the table, and the demo-table class connects it to the CSS rules above. Rows (tr) and cells (th, td) go inside it.
<tr> is a table row. Each <th> is a header cell: it is bold and centered by default, and screen readers use it to describe the data below. This first row will be styled by the th rules.
A row of data. Each <td> is a normal cell, and the cells line up with the headers above them, in the same order.
</table> closes the table.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 3: Add Zebra Striping
What this code does: Colours every second row light gray (zebra striping) with :nth-child(even), so the eye can follow a row across the table. The header counts as row 1, so in the preview the Laptop row (row 2) is gray and the Mouse row (row 3) is white, under a green header.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the zebra striping demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo adds a stripe rule for alternating rows. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
border-collapse: collapse merges the borders of neighboring cells into a single line. Without it, each cell keeps its own border with a small gap between them, which gives a double-line look. width: 100% stretches the table across its container. By default a table is only as wide as its content.
A green header with white text. This rule only sets the colors.
Padding, a light border and left alignment for both header and data cells are written once in a group selector.
:nth-child(even) selects every second row: the 2nd, 4th, 6th and so on. These rows get a light gray background. The header row counts as the 1st row, so the first data row (Laptop) is gray and the next one (Mouse) is white. Use odd to start the pattern on the other row. The green header keeps its own color, because the th background is painted on top of the row background.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a table with a header row and two data rows.
<table> creates the table, and the demo-table class connects it to the CSS rules above. Rows (tr) and cells (th, td) go inside it.
<tr> is a table row. Each <th> is a header cell: it is bold and centered by default, and screen readers use it to describe the data below. This first row will be styled by the th rules.
This is the 2nd row of the table, so :nth-child(even) gives it the gray background.
This is the 3rd row, an odd one, so it keeps the white background. The stripes alternate automatically when you add more rows.
</table> closes the table.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 4: Add Hover Effect
What this code does: Highlights the row under the mouse pointer in light yellow, so readers can follow one row across the table. Move the pointer over the Paris and Tokyo rows in the preview: the highlight follows the pointer, while the orange header never changes.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the table hover effect demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo adds a hover rule for rows. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
border-collapse: collapse merges the borders of neighboring cells into a single line. Without it, each cell keeps its own border with a small gap between them, which gives a double-line look. width: 100% stretches the table across its container. By default a table is only as wide as its content.
An orange header with white text.
A light border, 12px of padding and left alignment for all cells.
:hover applies to the row that is under the mouse pointer. That row turns light yellow, which helps readers follow a row across wide tables. The header row does not change, because its cells have their own orange background, which covers the row background.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a table with a header row and two data rows.
<table> creates the table, and the demo-table class connects it to the CSS rules above. Rows (tr) and cells (th, td) go inside it.
<tr> is a table row. Each <th> is a header cell: it is bold and centered by default, and screen readers use it to describe the data below. This first row will be styled by the th rules.
A row of data. Each <td> is a normal cell, and the cells line up with the headers above them, in the same order.
A row of data. Each <td> is a normal cell, and the cells line up with the headers above them, in the same order.
</table> closes the table.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 5: Create a Responsive Table
What this code does: Puts the table inside a container that scrolls sideways when the table is wider than the screen, instead of breaking the page layout. The table has a minimum width of 500px, so make the preview window narrower than that and a horizontal scrollbar appears under the table.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the responsive table demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo wraps the table in a scrollable container. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
overflow-x: auto adds a horizontal scrollbar to the container only when its content is wider than it is. On a narrow screen the user scrolls the table sideways, instead of the whole page breaking. The class has the demo- prefix like all the others.
width: 100% fills the container on wide screens. min-width: 500px stops the table from getting narrower than 500px. When the container is narrower than that, the table keeps its width and the container scrolls, so you can see the effect by making the window narrow.
A blue header with white text.
A light border, padding and left alignment for every cell.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a container that holds a table with three columns.
This div is the scroll area. The table must be inside it, because the scrollbar belongs to the container, not to the table.
<table> creates the table, and the demo-table class connects it to the CSS rules above. Rows (tr) and cells (th, td) go inside it.
<tr> is a table row. Each <th> is a header cell: it is bold and centered by default, and screen readers use it to describe the data below. This first row will be styled by the th rules.
A row of data. Each <td> is a normal cell, and the cells line up with the headers above them, in the same order.
</table> closes the table.
</div> closes the scroll container.
</body> closes the body.
</html> is the final closing tag and ends the document.
Complete CSS Table Example
What this code does: Combines the earlier techniques in one table: a blue header, zebra stripes on even rows, a light blue hover highlight, and a scrollable container for small screens. In the preview the HTML and JavaScript rows are striped, and the row under the mouse pointer turns light blue.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the complete table demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets the font and 24px of padding. background: #f5f9ff gives the preview a light blue color.
overflow-x: auto lets the table scroll sideways inside this box when the screen is too narrow.
The table fills the container (width: 100%) but never gets narrower than 480px (min-width), so the container scrolls on small screens. border-collapse: collapse merges the cell borders into single lines.
A blue header with white text.
A light border, 12px of padding and left alignment for both header and data cells.
Every even row (2nd, 4th and so on) gets a very light gray background. The header is row 1, so the first data row, HTML, is striped.
The row under the mouse turns light blue. This rule is written after the zebra rule on purpose: both have the same specificity, so the later one wins, and the hover color shows even on striped rows. If you swap the two rules, the hover color would not appear on even rows.
</style> closes the CSS.
</head> closes the head. The visible content comes next.
<body> starts the visible content: a container with a table of courses.
The scroll container that holds the table.
<table> creates the table, and the demo-table class connects it to the CSS rules above. Rows (tr) and cells (th, td) go inside it.
<tr> is a table row. Each <th> is a header cell: it is bold and centered by default, and screen readers use it to describe the data below. This first row will be styled by the th rules.
The 2nd row of the table, an even row, so it gets the light gray stripe.
The 3rd row, an odd row, which keeps the white background.
The 4th row, an even row, so it is striped again. Add more rows and the pattern continues.
</table> closes the table.
</div> closes the scroll container.
</body> closes the body.
</html> ends the document.
Advantages of CSS Tables
Advantage
Benefit
Description
Better Readability
Easy Viewing
Makes table data easier to understand.
Professional Design
Modern Look
Improves the appearance of HTML tables.
Responsive Layout
Mobile Friendly
Tables can adapt to smaller screens.
Interactive
Better User Experience
Hover effects make rows easier to identify.
Best Practices
Use border-collapse: collapse; for clean borders.
Add enough padding for better readability.
Highlight table headers with a different background color.