Introduction
CSS table properties help you create clean, professional, and easy-to-read tables. You can add borders, change cell spacing, adjust alignment, set table width, and improve the overall appearance of data. In this chapter, you’ll practice CSS table properties through simple solved questions that are easy for beginners to understand. CSS Tables practice questions with solutions help to understand the concepts.
Question 1: Add a Border to a Table
Problem Statement
Write CSS to add a 1px solid black border to a table, its rows, and its cells.
CSS Code
table,
th,
td {
border: 1px solid black;
}
Output
The table displays with 1px solid black borders around the table, headings, and data cells.
Explanation
borderadds a border around table elements.1pxsets the border thickness.soliddefines the border style.blacksets the border color.
Question 2: Collapse Table Borders
Problem Statement
Write CSS to merge all table borders into a single border.
CSS Code
table {
border-collapse: collapse;
}
Output
The table displays with single merged borders instead of double borders.
Explanation
border-collapsecontrols how table borders are displayed.collapsemerges adjacent borders into one.- This creates a cleaner table layout.
Question 3: Set the Table Width
Problem Statement
Write CSS to make a table use 100% of its parent container’s width.
CSS Code
table {
width: 100%;
}
Output
The table stretches across the full width of its parent element.
Explanation
widthcontrols the table width.100%makes the table responsive within its container.- This is commonly used in modern web design.
Question 4: Center Text Inside Table Cells
Problem Statement
Write CSS to center-align the text inside all table headings and data cells.
CSS Code
th,
td {
text-align: center;
}
Output
All text inside table cells appears centered.
Explanation
text-aligncontrols horizontal text alignment.centerplaces the content in the middle of each cell.- This is useful for tables containing numbers or short values.
Question 5: Add Padding to Table Cells
Problem Statement
Write CSS to add 12px padding inside every table cell.
CSS Code
th,
td {
padding: 12px;
}
Output
Each table cell displays with 12px inner spacing, making the content easier to read.
Explanation
paddingcreates space between the cell border and its content.- More padding improves readability.
- It also gives the table a cleaner appearance.
Question 6: Change the Table Header Background Color
Problem Statement
Write CSS to display the table header with a blue background and white text.
CSS Code
th {
background-color: blue;
color: white;
}
Output
The table header displays with:
- Blue background
- White text
Explanation
background-colorchanges the background of the table header.colorchanges the text color.- This style makes table headings easy to identify.
Question 7: Add Zebra Striping to Table Rows
Problem Statement
Write CSS to display every even table row with a light gray background.
CSS Code
tr:nth-child(even) {
background-color: #f2f2f2;
}
Output
Every second row displays with a light gray background.
Explanation
:nth-child(even)selects all even-numbered rows.background-colorcreates alternating row colors.- Zebra striping improves table readability.
Question 8: Change Row Color on Hover
Problem Statement
Write CSS to change the background color of a table row to light blue when the mouse pointer moves over it.
CSS Code
tr:hover {
background-color: lightblue;
}
Output
The row changes to a light blue background when hovered.
Explanation
:hoverapplies styles while the mouse pointer is over the row.background-colorchanges the row color.- Hover effects help users focus on a specific row.
Question 9: Position the Table Caption at the Bottom
Problem Statement
Write CSS to display the table caption below the table.
CSS Code
caption {
caption-side: bottom;
}
Output
The table caption appears below the table.
Explanation
caption-sidecontrols the position of the table caption.bottomplaces the caption below the table.- The default position is
top.
Question 10: Create a Complete Styled Table
Problem Statement
Write CSS to create a table with the following styles:
- Full width
- Collapsed borders
- 1px solid gray border
- 10px cell padding
- Center-aligned text
CSS Code
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: 1px solid gray;
padding: 10px;
text-align: center;
}
Output
The table displays with:
- Full width
- Single borders
- Comfortable cell spacing
- Centered content
Explanation
width: 100%;makes the table responsive.border-collapse: collapse;merges adjacent borders.paddingimproves readability.text-align: center;aligns the content in the middle of each cell.
Key Takeaways
borderadds borders around tables and cells.border-collapseremoves double borders.widthcontrols the table width.paddingimproves spacing inside cells.text-aligncontrols the alignment of cell content.:nth-child(even)creates zebra-striped rows.:hoverhighlights rows when the mouse pointer moves over them.caption-sidechanges the position of the table caption.
Frequently Asked Questions (FAQs)
1. What does the border-collapse property do?
The border-collapse property merges adjacent table borders into a single border, creating a cleaner table layout.
2. How can I make a table fill the available width?
Use the following CSS:
table {
width: 100%;
}
This makes the table use the full width of its parent container.
3. What is zebra striping in a table?
Zebra striping applies alternating background colors to rows, making large tables easier to read.
4. How do I add space inside table cells?
Use the padding property.
th,
td {
padding: 10px;
}
5. How can I highlight a table row when the mouse hovers over it?
Use the :hover pseudo-class.
tr:hover {
background-color: lightblue;
}
6. What is the purpose of the caption-side property?
The caption-side property changes the position of the table caption, allowing it to appear above or below the table.
7. Which CSS properties are commonly used to style tables?
The most commonly used properties are:
borderborder-collapsewidthpaddingtext-alignbackground-colorcaption-side
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
