Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding on parent versus margin on child

Tags:

html

css

When I want to place some (logically meaningful) div inside a (logical) container div with some space apart as below,

div within div with space

which of the two ways would be more logically correct?

  • Set the padding property of the container
  • Set the margin property of the div that is inside

Sometimes, I have more than one element in the container like this:

multiple divs within a container div

For such cases, I would need to set the space between the elements independently from the space surrounding the elements. For the space between, I cannot use the padding property of the container, and I have to go with the margin (-left or -right) of the elements inside. Taking this into consideration, for setting the space between the container and the elements, I am wondering whether it makes more sense to set the margin (-left, -right, -top, -bottom) of the elements or the padding of the container.

I use flexbox. And I also have the setting as box-sizing: border-box;. I am free to use CSS selectors such nth-child if necessary.

like image 674
sawa Avatar asked Jun 02 '13 07:06

sawa


People also ask

When should you use margin vs padding?

A margin is the space around an element, while padding is the space inside of an element. You use a margin to ensure that other elements aren't too close to the element in question. You use padding to make space inside of the element itself.

What are margins and padding?

Margin is said to be the outer space of an element, i.e., the margin is the space outside of the element's border. Padding is said to be the inner space of an element, i.e., the padding is the space inside of the element's border. We can set the margin to auto.

Can margin and padding used interchangeably?

While web designers use both margin and padding to create white space, the two commands have different purposes and cannot be used interchangeably. Here are key differences between margin and padding: The way they create space around elements: Margin pushes the elements next to it farther away.

What is the difference between padding margin and borders?

In HTML margins work the same way in giving space away from the edge of the page. Borders simply wrap the element. A border can wrap immediately around an element, or it may look further away from an element if more padding has been applied to the element. Padding provides space around the element.


2 Answers

Consider what kind of gutter you want to add. Is it to seperate elements from each other? Is it to create space inside an element?

For gutter on all sides of an element, like the blue in your example:

enter image description here

Here, I'd use padding on the container. Logically, that's exactly what I want, so why consider anything else?


For gutter between elements on a row, like the space between the green elements in your second example:

enter image description here

Here, I'd use margin on the green elements. There's obviously a margin between them, so padding doesn't make a whole lot of sense.


When you use these two examples together, however, they create a problem where the margin on the green elements may be conflicting with their parent's padding. I manage this by removing the margins from the first and last children.

Additionally, you may want more of those fine, green elements on a new row. I usually clear on every row using an element wrapping the entire row with whatever appropriate method to clear the floats, so it makes a lot of sense to seperate the rows with a margin. Obviously, the same conflict with the parent's padding arises here, but it's easily handled in the same way (ie, removing the margin from the last row).

So, in short:

  • Padding on parent elements for gutter between its edges and its children.
  • Margin to seperate elements with the same parent from each other.
  • Remove margins from said children when its margin connects to the parent's padding (the first and/or last children in a row, the last child in a column).

Disclaimer: This is how I do things. I can't promise it's the most efficient way to do things, but it's the way that makes the most sense to me.

like image 173
Nils Kaspersson Avatar answered Sep 19 '22 14:09

Nils Kaspersson


I prefer to set margin on the div that resides inside the container.

Suppose the black div below is the outer container with display: flex, width: 300px and height: 200px. When you assign padding: 30px padding to the outer div, this will result in 360px in width and 260px in height. Given that you won't expect the container to stretch, this will affect the elements around the container div. Hence, it is better to use margin on the inner div.

enter image description here

When you assign margin between the inner div and the container, the actual outer div won't move, and the margin will only affect the inner div, thus not affecting the elements around it.

If you are using box-sizing: border-box; then things will change accordingly, so it totally depends on what actual size the elements has to be. Using margin/padding on the inner elements will be the right way.

like image 22
Mr. Alien Avatar answered Sep 21 '22 14:09

Mr. Alien