CSS, nested divs & margins vs. padding

后端 未结 7 2067
攒了一身酷
攒了一身酷 2021-01-13 16:10

i totally understand the box model. this question is more about trying to pin down a semantic methodology regarding when to use margins and when to use padding.

her

相关标签:
7条回答
  • 2021-01-13 16:10

    Vertical padding on the division - because if I decided I wanted a different amount of vertical space in between the multiple paragraphs I'd use bottom margins, and the top/bottom padding of the enclosing division pretty much will always stay intact assuming you just have staticly positioned elements inside.

    0 讨论(0)
  • 2021-01-13 16:15

    It depends on what you're trying to accomplish visually. Would container have other child elements which might hang over into the gutter on either side of the paragraph? If so, a margin makes more sense. But if container should have a 12-pixel gutter for all elements, period, it makes the most sense to use the padding to avoid having to apply margins to multiple element sets.

    Generally speaking you always want paragraphs to have vertical margins to ensure consistent paragraph leading.

    0 讨论(0)
  • 2021-01-13 16:19

    Paddings and margins gives the same effect, Except in the following cases (I might miss some):

    1. You have some kind of background properties. Margins won't get them.
    2. You have a border
    3. You use TD (no margins)
    4. Two nested items, The margins are collapsed together, where paddings not.
    5. (need to check this one) They probably affect the width and height of the element differently. (If some one knows better, pls edit this).
    0 讨论(0)
  • 2021-01-13 16:24

    Personally, I prefer option A. Why? Say now I have to add other HTML elements into the div and I want the padding to be maintained, I would not have to add other rules to my CSS files to get it working.

    0 讨论(0)
  • 2021-01-13 16:24

    The difference is where the border sits.

    The border sits SMACK DAB in the middle of the margins and padding. If you specify margins, that is white space OUTSIDE the border.

    If you specify padding, that is white space INSIDE the border (pushes the border further out from the element)

    Can't show you here due to css stripping, but try this out:

    
    <body style="background-color: #aaa">
    <p style="background-color: #aee; margin: 40px; padding: 40px; border: solid 2px black;">
      i have margins, padding and a border.
    </p>
    
    <p style="background-color: #aee; margin: 40px; padding: 0; border: solid 2px black;">
      i have margins, and a border.
    </p>
    
    <p style="background-color: #aee; margin: 0; padding: 40px; border: solid 2px black;">
      i have padding and a border.
    </p>
    </body>
    

    other stuff!

    • padding brings in background color of the element, margins are basically transparent

    • some elements ( like td ) seem to ignore margins, while they respond to changes in padding

    0 讨论(0)
  • 2021-01-13 16:26

    This is a bug in css, here are examples:

    http://creexe.zxq.net/div-issue-padding.html = padding issue

    http://creexe.zxq.net/div-issue-margin.html = margin issue

    the red and green div tags in the examples were created by the css property TOP,but it has its own disadvantages athat TOP,BOTTOM etc works only when the position of the div tag is Absolute and relative, but not static

    0 讨论(0)
提交回复
热议问题