-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>
6
  • 5
    Apply background-color:red; to both. See what happens. Decide for yourself. Commented Nov 10, 2017 at 19:34
  • nice pointer thanks. Commented Nov 10, 2017 at 19:37
  • 2
    it all comes clear when the background or border element is styled and when among other contents. margin is to put space between elements, padding is mostly used to push contents away from edges for visual clarity Commented Nov 10, 2017 at 19:37
  • 1
    Padding is space inside an element. Margin is space outside an element. Commented Nov 10, 2017 at 19:45
  • Refer to developer.mozilla.org/en-US/docs/Learn/CSS/Styling_boxes/… Commented Nov 10, 2017 at 19:49

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think picture from @Nick's response helps to illustrate this.
1

This image can help you understand better.

enter image description here

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.