How to space the children of a div with css?

前端 未结 8 610
-上瘾入骨i
-上瘾入骨i 2021-02-03 17:37

I want a gap of say 30px; between all children of my div. E.g if I have:

...

8条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 18:15

    Probably the easiest way is this:

    #parent * {
      margin-bottom: 30px;
    }
    

    or

    #parent * {
      margin: 15px 0;
    }
    

    Keep in mind, though, that this will get everything in #parent, including things inside the p and div tags. If you want just the direct children, you can use #parent > * (this is call the direct descendent selector) instead.

    Keep in mind, is an inline element by default, so you might need to do:

    #parent img {
      display: block;
    }
    

    for it to use the margins.

提交回复
热议问题