"Table Styling with CSS" is a comprehensive guide to making your tables not only functional but visually appealing. Tables are essential for organizing and presenting data.
Tables consist of rows ('<tr>'
), columns ('<td>'
for data cells, '<th>'
for header cells), and the table itself ('<table>'
). Structure your table elements appropriately.
Header 1
Header 2
Data 1
Data 2
Modify the border and padding properties for better visual distinction.
table {
border-collapse: collapse; /* Merge cell borders */
width: 100%;
}
th, td {
border: 1px solid #dddddd; /* Border color for cells */
padding: 8px; /* Padding inside cells */
}
Improve readability by applying alternating colors to rows.
tbody tr:nth-child(even) {
background-color: #f2f2f2; /* Light gray background for even rows */
}
Enhance user interaction by adding hover effects to rows.
tbody tr:hover {
background-color: #e0e0e0; /* Lighter background on hover */
}
Align text within cells for a cleaner appearance.
th, td {
text-align: center; /* Center text in cells */
}
Make tables responsive by allowing horizontal scrolling on smaller screens.
@media only screen and (max-width: 600px) {
table {
overflow-x: auto;
}
}
Create a modern and dynamic feel by applying gradient backgrounds to cells.
tbody td {
background: linear-gradient(to right, #e0f7fa, #81d4fa); /* Gradient background for cells */
}
Softening the corners can add a touch of elegance to your table.
table {
border-radius: 8px; /* Rounded corners for the entire table */
}
th, td {
border-radius: 4px; /* Rounded corners for cells */
}
Emphasize specific cells for important data.
td.highlight {
background-color: #ffcc00; /* Yellow background for highlighted cells */
}
Style the header for a professional look.
thead {
background-color: #333; /* Dark background for header */
color: #ffffff; /* White text color for header */
}
Implement collapsible rows for an efficient use of space on mobile devices.
@media only screen and (max-width: 600px) {
tbody tr td:not(:first-child) {
display: none; /* Hide cells in collapsible rows */
}
tbody tr.collapsible {
cursor: pointer;
}
tbody tr.collapsible:hover td {
display: table-cell; /* Reveal cells on hover */
}
}
"Table Styling with CSS" provides you with the tools to transform plain tables into visually appealing and user-friendly components. Whether you're working with basic border adjustments or implementing advanced techniques like hover effects, thoughtful table styling contributes significantly to the overall aesthetics of your web pages. Happy Coding! ❤️