Styling lists and tables with CSS

In this article, we'll explore advanced techniques for styling lists and tables using CSS. You'll learn how to improve the appearance of these basic elements to create more attractive and functional web interfaces.
List Styling
Lists in HTML can be ordered (<ol>) or disordered (<ul>With CSS, we can significantly change its appearance.
Customizing Unordered Lists (<ul>
Removing Bullets from Lists
You can remove bullets from lists using the propertylist-style-type
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
Add Icons to List Items
You can use images or custom icons:
ul li {
background: url("icon.png") no-repeat left center;
padding-left: 20px; /* Ajustar según el tamaño del icono */
}
Styling List Items
Change the color, font, and add hover effects:
ul li {
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
transition: color 0.3s;
}
ul li:hover {
color: #007bff;
}
Customizing Sorted Lists (<ol>)
Change Number Type
You can change the numbers to Roman numerals, letters, etc.:
ol {
list-style-type: upper-roman;
}
Styling Numbers
Apply specific styles to numbers:
ol {
counter-reset: list;
}
ol li {
counter-increment: list;
position: relative;
}
ol li::before {
content: counter(list) ". ";
position: absolute;
left: -2em;
color: #007bff;
font-weight: bold;
}
Table Styling
Tables are HTML elements used to display tabular data. CSS allows us to transform a basic table into something much more attractive and readable. Basic Table Styling
Adding Borders
Add borders to the table and its cells:
table,
th,
td {
border: 1px solid #ddd;
border-collapse: collapse;
}
th,
td {
padding: 8px;
text-align: left;
}
Alternate Row Colors
Usenth-childTo alternate the row colors:
tr:nth-child(even) {
background-color: #f2f2f2;
}
Style Headers
Change the background color and font of the headings:
th {
background-color: #007bff;
color: white;
font-weight: bold;
}
Advanced Table Styling
Styling Inner and Outer Borders Separately
Apply different styles to inner and outer borders:
table {
border: 2px solid #007bff;
border-collapse: separate;
border-spacing: 0;
}
th,
td {
border: 1px solid #ddd;
}
Hover Effects on Rows
Add effects when hovering over rows:
tr:hover {
background-color: #f5f5f5;
}
Conclusion
Advanced styling of lists and tables with CSS allows you to transform basic elements into visually appealing and functional components. With these techniques, you can significantly improve the appearance and usability of your websites. Practice and experiment to discover all the possibilities that CSS has to offer!
-
Units of measurement:Units of measurement and positioning in CSS
-
Flexbox:Flexbox in CSS
-
More about lists:MDN - Applying Style to Lists
-
More about tables:MDN - Stylizing Tables
💻Get your hands on the code with these simple challenges: