Lists and Tables
Style list markers, spacing, table borders, and table cells.
Lists and tables are common elements used on websites to organize and display information. Lists are useful for showing items, steps, features, or menus, while tables help present structured data in rows and columns. CSS allows developers to style both lists and tables to make them more attractive, readable, and user-friendly.
Without CSS, lists and tables use the browser's default styling, which often looks plain. CSS helps customize their appearance to match the overall design of a website.
Why Style Lists and Tables?
CSS styling helps:
- Improve readability
- Create professional designs
- Organize information clearly
- Match website branding
- Enhance user experience
For example, an e-commerce website may use styled tables to display product comparisons and custom lists for navigation menus.
Styling Lists
HTML provides two common types of lists:
- Unordered Lists
- Ordered Lists
By default, unordered lists use bullet points and ordered lists use numbers.
Basic List Example
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>Changing List Markers
The list-style-type property changes the appearance of list markers.
Example
ul {
list-style-type: square;
}Common values include:
- disc
- circle
- square
- none
To remove bullets completely:
ul {
list-style-type: none;
}This is often used when creating navigation menus.
Adding Custom List Styling
You can also customize spacing and colors.
Example
li {
color: blue;
margin-bottom: 10px;
}This makes list items blue and adds spacing between them.
Styling Tables
Tables display data using rows and columns.
Basic Table Example
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
</table>Without CSS, tables often look simple and difficult to read.
Adding Borders to Tables
The border property helps define table boundaries.
Example
table, th, td {
border: 1px solid black;
}This creates borders around the table and its cells.
Collapsing Borders
To avoid double borders, use:
table {
border-collapse: collapse;
}This creates a cleaner table design.
Adding Padding
Padding improves readability by creating space inside cells.
Example
th, td {
padding: 10px;
}This prevents text from touching the cell borders.
Zebra Striping
Zebra striping uses alternating row colors to make tables easier to read.
Example
tr:nth-child(even) {
background-color: #f2f2f2;
}This highlights every second row with a light background color.
Real-World Example
Imagine an online course website:
- A list displays course topics.
- A table shows pricing plans.
- Custom colors match the website theme.
- Zebra striping improves readability of pricing details.
These enhancements make information easier to understand and more visually appealing.
Conclusion
CSS lists and tables help organize information in a clear and professional way. By using properties such as list-style-type, border, border-collapse, padding, and row styling techniques, developers can transform basic lists and tables into attractive and user-friendly components. Learning how to style lists and tables is an important step toward creating polished and professional websites.