CSS, nested divs & margins vs. padding

后端 未结 7 2216
攒了一身酷
攒了一身酷 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: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:

    
    
    

    i have margins, padding and a border.

    i have margins, and a border.

    i have padding and a border.

    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

提交回复
热议问题