-1

With reference to this, if I want to align a div to left by 20px, I can use both padding-left or margin-left. Which one is preferred and in which case?

<div style="padding-left: 20px;">
    Move me by 20px left!
</div>

<div style="margin-left: 20px;">
    Move me by 20px left!
</div>
Lypyrhythm
  • 326
  • 2
  • 12
Kurkula
  • 6,673
  • 23
  • 99
  • 170

3 Answers3

2

This image can help you understand better.

enter image description here

Nick
  • 374
  • 1
  • 9
1

To build off of what @G-Cyr said, think of it in terms of pictures on a wall.

Your picture is your element in question; the frame around the picture can be considered the padding, or the immediate space surrounding image, yet unique to that element. Margin could be considered the spacing between two of the pictures on said wall.

From a philosophical standpoint, you may want to defer to margin when changing spacing between two elements.

TCharb
  • 356
  • 5
  • 15
0

The fact is that margin is used to add space to things from the outside while padding is used to add spacing inside of that same thing, but for your question i am not sure if you're talking about aligning group of div as an example to the left, if i am to do that i will use the keyword alight-left which is available in css.

Wale
  • 1,307
  • 8
  • 25