When you’re building a WordPress plugin and styling your elements, you’ll often use CSS unit to add space around things — like margins and padding. But choosing the right unit can be confusing. Should you use px
, em
, or rem
?
Let’s explain it simply.
Use rem
for Most Spacing
The best CSS unit for margins and padding is rem
. It stands for “root em”, and it’s based on the root font size of the browser. Most browsers have a default font size of 16px
, so 1rem
usually equals 16px
.
Example:
padding: 1rem; /* = 16px */
margin-bottom: 2rem; /* = 32px */
Using rem
helps make your plugin design more flexible and accessible. If a user increases their browser font size for easier reading, the spacing will scale too — keeping your layout clean on all devices.
When to Use px
px
stands for pixels, which are fixed CSS units. It gives you exact control over spacing, but it doesn’t scale with screen size or user settings.
Use px
for:
- Borders (like
1px solid #ccc
) - Box shadows
- Icon sizes or pixel-perfect UI tweaks
Tip: Avoid Mixing Too Many Units
For a clean, responsive design:
- Use
rem
for spacing (padding, margin) - Use
rem
for font size - Use
px
for small details (borders, shadows) - Use
unitless
orem
for line height
Conclusion:
If you want your plugin to be responsive, accessible, and look good on all screen sizes, make rem
your default for spacing. It’s simple, scalable, and future-proof.