Display inline block text inside issue

后端 未结 3 807
野的像风
野的像风 2021-01-16 21:05

I have been trying out display inline-block, everything worked out great if I didn\'t add anything into the divs but when I do the div collapsed and I don\'t know exactly wh

相关标签:
3条回答
  • 2021-01-16 21:26

    The issue is due to default vertical alignment of inline elements – baseline, the text inside element affects it and pushes div to the bottom. Use vertical-align: top, for example, to suppress this behavior.

    JSFiddle

    0 讨论(0)
  • 2021-01-16 21:31

    display:inline-block creates a small gap, so add font-size:0 to parent. plus add vertical-align:top since by default inline-block is baseline

    with all of this you have fixed your issue.

    here is the snippet:

    .caja {
      background-color: gray;
      width: 100px;
      height: 100px;
      border: 1px solid black;
      margin: 5px;
    }
    .caja-grande {
      background-color: gray;
      border: 1px solid black;
      padding: 5px;
      width: 350px;
      margin: 5px;
      font-size: 0
    }
    .inline-block {
      display: inline-block;
      vertical-align: top;
      font-size: 16px;
    }
    <div class="caja"></div>
    <div class="caja-grande">
      <div class="caja inline-block">
        <span>Hola mundo</span>
      </div>
      <div class="caja inline-block">
      </div>
      <div class="caja inline-block">
      </div>
    </div>
    <div class="caja inline-block"></div>
    <div class="caja inline-block"></div>
    <div class="caja"></div>

    you can find more info (and options on how to solve this in other ways) in this article Fighting the Space Between Inline Block Elements

    0 讨论(0)
  • 2021-01-16 21:50

    Use overflow: hidden at .inline-block to make this work.

    .inline-block{
        display: inline-block;
        overflow: hidden;
    }
    
    0 讨论(0)
提交回复
热议问题