CSS display: inline-block does not accept margin-top?

后端 未结 4 943
小蘑菇
小蘑菇 2020-12-05 22:31

I have an element with display: inline-block, but it doesn\'t seem to accept margin-top. Is this because the element is still treated as an inline element?

If yes, d

相关标签:
4条回答
  • That is indeed the case. Instead of a margin, you could use a padding. Another solution would be to use a container div for the element. You make that div inline-block, and make your current element a block inside that container. Then, you can give a margin to your element.

    It would help if you got a concrete example, preferably in jsfiddle.net or so. It would help answers to be more specific too, instead of containing just general descriptions like mine here. ;)

    0 讨论(0)
  • 2020-12-05 23:01

    you can also try replacing the negative margin with

    .label{
        position:relative;
        top:-2px;
    }
    

    in addition to the rest of your .label style

    0 讨论(0)
  • 2020-12-05 23:03

    You can try vertical-align like this:

    .label {
      background: #ffffff;
      display: inline-block;
      margin-top: -2px;
      vertical-align: 2px;
      padding: 7px 7px 5px;
    }
    

    I made an example on jsfiddle: http://jsfiddle.net/zmmbreeze/X6BjK/.
    But vertical-align not work well on IE6/7. And there is a opera(11.64) rendering bug.

    So I recommend to use position:relative instead.

    0 讨论(0)
  • 2020-12-05 23:06

    I used display: table. It has the content-fitting properties of inline-block but also supports negative margins in a way that will shift the content following it up along with it. Probably not how you should be using it, but it works.

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