A brief guide to using tables in CSS
Forms in the past were overused in CSS because they are one of the only ways we can create beautiful page layouts.
Now, with the help of Grid and Flexbox, we can move the table back to what it should do: style sheets.
Let's start with HTML. This is a basic table:
<table>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Flavio</th>
<td>36</td>
</tr>
<tr>
<th scope="row">Roger</th>
<td>7</td>
</tr>
</tbody>
</table>
It is not very attractive by default. The browser provides some standard styles, nothing more:
Of course, we can use CSS to style all the elements of the table.
Let's start with the border. A good border can go a long way.
We can apply it totable
Elements, and internal elements, such asth
withtd
:
table, th, td {
border: 1px solid #333;
}
If we pair it with a certain margin, we get a good result:
A common problem with tables is the ability to add a color in one row and another color in another row. use:nth-child(odd)
or:nth-child(even)
Selector:
tbody tr:nth-child(odd) {
background-color: #af47ff;
}
This gives us:
If you addborder-collapse: collapse;
On the table element, all borders are collapsed into one:
Download mine for freeCSS Manual
More CSS tutorials:
- Flexbox guide
- CSS grid tutorial
- CSS variables (custom attributes)
- Introduction to PostCSS
- CSS guarantees metallicity
- How to center an element using CSS
- CSS system fonts
- How to print HTML with styles
- Getting started with CSS transition
- CSS animation tutorial
- Introduction to CSS
- CSS guide
- How to set up Tailwind with PurgeCSS and PostCSS
- Tail wind cheat sheet
- How to continuously rotate an image using CSS
- Use CSS to make the table responsive
- How to debug CSS by halving
- CSS selector
- CSS cascade
- CSS specificity
- CSS attribute selector
- CSS colors
- CSS Unit
- CSS url()
- CSS typography
- CSS Box model
- CSS position properties
- CSS media queries and responsive design
- CSS function query
- CSS conversion
- How to style a list with CSS
- CSS vendor prefix
- CSS inheritance
- CSS pseudo-classes
- CSS pseudo elements
- Style HTML tables with CSS
- CSS Display property
- CSS calc() function
- CSS border
- Use @import to import CSS files
- CSS error handling
- CSS filter
- CSS Box size
- CSS background
- CSS comments
- CSS fonts
- CSS padding
- CSS float property and clear
- CSS normalization
- CSS z-index property
- How to disable text selection using CSS
- How to use CSS to place items at the bottom of their container
- How to invert colors using CSS
- Responsive front tags in CSS
- Responsive YouTube video embedding
- What is a good CSS breakpoint value for responsive design?
- How to align the center in Flexbox