Padding a table row
Padding a table row is achieved using <td>
or <th>
elements, since the <tr>
element does not support padding. Insert CSS like so:
Take a look at this HTML example with padding inside the cells:
Boom! Each cell in the row has 10px padding, effectively cushioning that entire row.
Padding with charm: Pseudo-classes and operators
We can use the :first-child
pseudo-class to style the first cell of each row differently; this can add a pleasing visual element:
To bypass certain cells like the first column, the :not()
operator comes to rescue:
Always keep in mind the browser support for these selectors to ensure your aesthetic choices remain universally appreciated.
Going beyond borders: Advanced table padding techniques
For horizontal padding between rows, or to increase that pleasing space, mix border-spacing
with border-collapse
:
How about a master trick? You can use the ::before
pseudo-element to simulate padding in the cell:
Just make sure to set the display
property to inline
or inline-block
to not mess with text alignment.
Spacing out problems: Special cases and forward compatibility
The CSS Editor's Draft hints towards direct padding on <tr>
elements in future. Keeping an eye on the future of CSS might just land you a job at Google! (Okay, maybe not.)
Let's not forget to account for how CSS versions deal with padding and borders. Who knows, you might just stumble upon an alien browser from the past abusing your template.
The Responsive Approach: Ensuring reuse at its finest
Following the relative path for responsive design
For responsive designs, consider using em
or rem
for padding. These relative units scale eloquently with your text size:
This ensures your layout behaves and not break a dance on different devices.
Hitting the bulls-eye: Precise styling using Class
For more control over a specific cell’s styles, you can utilize CSS classes:
Just remember to test run these classes on multiple devices. Cross-test your application and always stay one step ahead of your users' complaints.
The transparent shields: Using border as an alternative to padding
When every other trick fails, shift the game altogether. Consider using transparent borders. This maintains cell dimensions and yet, provides the illusion of space:
Was this article helpful?