Margin or padding what is recommended?

前提是你 提交于 2019-12-12 04:07:00

问题


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;
    }

回答1:


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.




回答2:


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

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.




回答3:


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.



来源:https://stackoverflow.com/questions/25348213/margin-or-padding-what-is-recommended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!