~/
hackweb.dev
CSS Lists & Tables Styling
Quiz
⌘K
...
~/
/tutorials
/css/css-lists-tables/edit
~ Contribute
Suggest a correction or improvement. The author reviews it before it goes live.
Loading...
Comment
0 / 300
Typo
Grammar
Broken link
Clarify
Code
en/tutorials/css/16css-lists-tables
Write
Preview
Diff
# CSS Lists & Tables Styling Style lists and tables to match your design while maintaining accessibility. ## List Styling ### List Style Type ```css /* Unordered list markers */ ul { list-style-type: disc; /* Default: bullet */ list-style-type: circle; /* Circle */ list-style-type: square; /* Square */ list-style-type: none; /* No marker */ } /* Ordered list markers */ ol { list-style-type: decimal; /* 1, 2, 3 */ list-style-type: decimal-leading-zero; /* 01, 02, 03 */ list-style-type: lower-alpha; /* a, b, c */ list-style-type: upper-alpha; /* A, B, C */ list-style-type: lower-roman; /* i, ii, iii */ list-style-type: upper-roman; /* I, II, III */ } ``` ### List Style Position ```css /* Inside: Marker inside content flow */ ul { list-style-position: inside; } /* Outside: Marker outside content (default) */ ul { list-style-position: outside; } ``` ### List Style Image ```css ul { list-style-image: url('bullet.png'); } ``` ### Shorthand ```css ul { list-style: square inside url('bullet.png'); } ``` ## Complete Example: Custom List ```css /* Remove default styles */ .custom-list { list-style: none; padding: 0; margin: 0; } .custom-list li { position: relative; padding-left: 1.5rem; margin-bottom: 0.75rem; line-height: 1.6; } /* Custom bullet */ .custom-list li::before { content: "→"; position: absolute; left: 0; color: #3498db; font-weight: bold; } /* Numbered list */ .numbered-list { list-style: none; padding: 0; counter-reset: item; } .numbered-list li { counter-increment: item; position: relative; padding-left: 2rem; margin-bottom: 0.75rem; } .numbered-list li::before { content: counter(item); position: absolute; left: 0; width: 1.5rem; height: 1.5rem; background-color: #3498db; color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 0.8rem; font-weight: bold; } ``` ```html <ul class="custom-list"> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul> <ol class="numbered-list"> <li>Step one</li> <li>Step two</li> <li>Step three</li> </ol> ``` ## Table Styling ### Basic Table Styles ```css table { width: 100%; border-collapse: collapse; margin: 1rem 0; } th, td { padding: 0.75rem; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #f5f5f5; font-weight: 600; } tr:hover { background-color: #f9f9f9; } ``` ### Border Collapse ```css /* Collapse borders (default) */ table { border-collapse: collapse; } /* Separate borders */ table { border-collapse: separate; border-spacing: 10px; } ``` ### Table Layout ```css /* Auto layout (default) */ table { table-layout: auto; } /* Fixed layout (faster) */ table { table-layout: fixed; } ``` ### Striped Rows ```css /* Using nth-child */ tbody tr:nth-child(even) { background-color: #f9f9f9; } /* Using nth-child formula */ tbody tr:nth-child(3n) { background-color: #f5f5f5; } ``` ## Complete Example: Styled Table ```css .table-container { width: 100%; overflow-x: auto; border: 1px solid #ddd; border-radius: 8px; } .table { width: 100%; border-collapse: collapse; font-size: 0.9rem; } .table thead { background-color: #3498db; color: white; } .table th, .table td { padding: 1rem; text-align: left; } .table th { font-weight: 600; text-transform: uppercase; font-size: 0.8rem; letter-spacing: 0.05em; } .table tbody tr { border-bottom: 1px solid #ddd; transition: background-color 0.2s; } .table tbody tr:hover { background-color: #f5f5f5; } .table tbody tr:last-child { border-bottom: none; } /* Status badges */ .badge { display: inline-block; padding: 0.25rem 0.75rem; border-radius: 20px; font-size: 0.75rem; font-weight: 600; } .badge-success { background-color: #d4edda; color: #155724; } .badge-warning { background-color: #fff3cd; color: #856404; } .badge-danger { background-color: #f8d7da; color: #721c24; } ``` ```html <div class="table-container"> <table class="table"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Status</th> <th>Actions</th> </tr> </thead> <tbody> <tr> <td>John Doe</td> <td>john@example.com</td> <td><span class="badge badge-success">Active</span></td> <td><button>Edit</button></td> </tr> <tr> <td>Jane Smith</td> <td>jane@example.com</td> <td><span class="badge badge-warning">Pending</span></td> <td><button>Edit</button></td> </tr> </tbody> </table> </div> ``` ## Responsive Tables ### Method 1: Horizontal Scroll ```css .table-wrapper { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; } ``` ### Method 2: Stacked on Mobile ```css @media (max-width: 768px) { table, thead, tbody, th, td, tr { display: block; } thead { display: none; } tr { margin-bottom: 1rem; border: 1px solid #ddd; border-radius: 5px; } td { text-align: right; padding-left: 50%; position: relative; } td::before { content: attr(data-label); position: absolute; left: 1rem; font-weight: 600; text-align: left; } } ``` ```html <table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Status</th> </tr> </thead> <tbody> <tr> <td data-label="Name">John Doe</td> <td data-label="Email">john@example.com</td> <td data-label="Status">Active</td> </tr> </tbody> </table> ``` ## Complete Example: Pricing Table ```css .pricing-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 2rem; padding: 2rem; } .pricing-card { border: 1px solid #ddd; border-radius: 10px; padding: 2rem; text-align: center; transition: transform 0.3s, box-shadow 0.3s; } .pricing-card:hover { transform: translateY(-5px); box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); } .pricing-card.featured { border-color: #3498db; position: relative; } .pricing-card.featured::before { content: "Popular"; position: absolute; top: -12px; left: 50%; transform: translateX(-50%); background-color: #3498db; color: white; padding: 0.25rem 1rem; border-radius: 20px; font-size: 0.8rem; font-weight: 600; } .pricing-title { font-size: 1.5rem; margin-bottom: 1rem; } .pricing-price { font-size: 3rem; font-weight: 700; margin-bottom: 1rem; } .pricing-features { list-style: none; padding: 0; margin: 2rem 0; text-align: left; } .pricing-features li { padding: 0.5rem 0; border-bottom: 1px solid #eee; } .pricing-features li::before { content: "✓"; color: #27ae60; margin-right: 0.5rem; } ``` ## Best Practices 1. **Use border-collapse: collapse** — Cleaner table borders 2. **Add hover states** — Improve interactivity 3. **Make tables responsive** — Horizontal scroll or stacked 4. **Use semantic HTML** — thead, tbody, th, td 5. **Add loading states** — For dynamic tables ## Common Mistakes 1. **Using tables for layout** — Use CSS Grid/Flexbox instead 2. **Missing thead/tbody** — Hurts accessibility 3. **Not making tables responsive** — Broken on mobile 4. **Overusing zebra striping** — Can be distracting 5. **Missing hover states** — Poor interactivity
No changes yet
Reset to original
Submit suggestion
cancel