DEV Community

CSS Padding Guides

There's a lot of nuance in effective CSS padding.

This is a collection of top and trending guides written by the community on subjects related to CSS Padding concepts. For all things CSS, check out the CSS tag! Please contribute more posts like this to help your fellow developer in need.

Use em For Padding and Margin

You'd probably use em for padding and margin before. However, do you know how is it calculated?


Margin vs Padding in CSS Explained

Differentiating between margin and padding css properties can sometimes be confusing, the short description of these two properties is that padding can be considered as an internal element which helps to create space around the element (outside the border), in the opposite margin is used to control the space outside the container (inside the border).


Two(2) ways to prevent padding from causing an overflow in CSS

When padding is added to an element with a width or a height of 100% it causes that element of overflow. It’s no surprise that’s what padding does, it creates extra space within an element.


CSS to ignore parent padding in 4 lines

I recently tried to add a newsletter to my website (subscribe - shameless plug) and I wanted the form to extend to the side of the browser window, but there is a parent div with padding for getting that uniform look throughout the site.


Why padding in a div affects other elements?

By default in the CSS box model, the width and height you assign to an element is applied only to the element's content box. If the element has any border or padding, this is then added to the width and height to arrive at the size of the box that's rendered on the screen. This means that when you set width and height, you have to adjust the value you give to allow for any border or padding that may be added - MDN


Using Sass' @each and @for for Bootstrap-Like Behavior of Margin & Padding Classes

Bootstrap includes margin and padding functionality that allows you to decide on what side of the element the margin or padding should be applied and how much of it should be layered on (multiple of the base font-size).


Happy CSS Padding coding!