0

I have made some code and css, now I'm learning to understand them..

What is the difference if I use margin or padding? What is recommended to use on my <p> tag?

I'm using margin now, but margin is for the outside of an element, why whould I use that if padding is for the space between the content and border.

So let's start what I have:

<div id="MainContainer">
  <section>
    <article>
      <h1>Title</h1>
        <p> text text text text text </p>
    </article>
  </section>
</div>

My CSS looks like this

#MainContainer {
    width:980px; 
    margin:0 auto; 

    background-color:#FFF; 

    }

article {
    text-align:left;
    color:#000000;
    padding-left: 205px;
    padding-bottom: 25px;
}

section {

    margin-left: 10px;
    margin-right: 10px;
    height:350px;
    }
p {
    margin: 10px 0px 15px 0px;
    }
Quest
  • 15
  • 3

3 Answers3

0

padding contains the space between content and the border, but margin contains space of outside the border. below image can introduce the difference

enter image description here

Summary

So, in brief, margins separate elements from each other and away from page edges, adding space outside of elements. Padding adds space inside of an element, separating the element’s content from the edges of the targeted element.

Farshad
  • 1,435
  • 1
  • 8
  • 12
0

Answer: When to use margin vs padding in CSS

Quoting:

Margin is on the outside of block elements while padding is on the inside.

Use margin to separate the block from things outside it

Use padding to move the contents away from the edges of the block.

Community
  • 1
  • 1
sitilge
  • 3,569
  • 4
  • 27
  • 49
0

One important difference (asides from margin means outside and padding means inside): Margins of adjacent elements overlap, when paddings don't. Take this example:

<p>Lorem ipsum</p>
<p>Dolor sic amet</p>

If you use margin:

p { margin: 10px 0; }

The space between these 2 paragraphs will be 10px.

But if you go:

p { padding: 10px 0; }

The contents will be 20px separated.

An Phan
  • 2,417
  • 1
  • 21
  • 27
  • Why does

    Dolor sic amet

    has 20 px above it? It only has padding-top 10px? So how does it became 20px?
    – Quest Aug 17 '14 at 10:23
  • It has padding top *and* bottom. `padding: 10px 0` is a shorthand for `padding: 10px 0 10px 0`. – An Phan Aug 17 '14 at 10:30