Mastering HTML Table Padding: Tips and Techniques

This post covers everything you need to know about table padding in HTML.

How to adjust the padding in HTML tables?

You can adjust padding in table cells using the CSS padding property. Apply this property directly to <td> elements or through CSS classes to make your styling more reusable and scalable.

Here's how to apply inline CSS for padding adjustment:

<table> <tr> <td style="padding: 10px;">Cell 1</td> <td style="padding: 10px;">Cell 2</td> </tr> <tr> <td style="padding: 20px;">Cell 3</td> <td style="padding: 20px;">Cell 4</td> </tr> </table>

In this example, the first row cells have 10px padding, and the second row cells have 20px. This method offers a quick way to adjust padding but can lead to a lot of repetition in larger tables or when striving for consistency across your site.

How to use CSS classes for padding?

Defining a CSS class for table padding is a more scalable and consistent approach. This way, you ensure uniformity and can easily adjust the padding for all cells by modifying one CSS rule.

<style> .padded-cell { padding: 15px; } </style> <table> <tr> <td class="padded-cell">Cell 1</td> <td class="padded-cell">Cell 2</td> </tr> <tr> <td class="padded-cell">Cell 3</td> <td class="padded-cell">Cell 4</td> </tr> </table>

By using the padded-cell class, you can easily apply consistent padding to any <td> element.

How to create responsive padding?

Adapting padding based on screen size enhances your tables' usability on different devices. You can do this with media queries.

.padded-cell { padding: 8px; } @media (min-width: 600px) { .padded-cell { padding: 12px; } } @media (min-width: 1000px) { .padded-cell { padding: 16px; } }

The CSS above dynamically adjusts the padding according to screen width, ensuring an optimal experience across various devices.

The next generation of charts and BI.

Coming soon.

Fast. Opinionated. Collaborative. Local-first. Keyboard centric.
Crafted to the last pixel. We're looking for early alpha users.