Padding on parent versus margin on child

前端 未结 4 1503
渐次进展
渐次进展 2021-02-05 12:11

When I want to place some (logically meaningful) div inside a (logical) container div with some space apart as below,

4条回答
  •  执念已碎
    2021-02-05 12:55

    It depends on how your subsequent content should render. If you're going to have multiple inner

    s (green) separated by a similar amount of spacing, it will make sense to use margin so that the margin collapse allows the divs to render as:

    +-------
    |        <- blue
    |  +----
    |  |     <- green
    |  |
    |  +----
    |
    |  +----
    |  |     <- green
    |  |
    |  +----
    |
    +-------
    

    Here is an example of some of the various options.

提交回复
热议问题