72

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, does anyone have a workaround?


EDIT #1:

My CSS is quite simple:

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

I ended up wrapping the content in another div and giving that a margin-top. But that causes a lot of extra markup and makes my code less clear.

EDIT #2:

margin-top & margin-bottom on inline-block elements only seems to work with positive values.

user2771704
  • 5,994
  • 6
  • 37
  • 38
Gregory Bolkenstijn
  • 10,003
  • 7
  • 36
  • 38
  • sure it does, post the markup and css you're having trouble if you want our help – bevacqua Sep 30 '11 at 13:26
  • No and to help further we would need to see your code. Here's a helpful link though http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/ – CBRRacer Sep 30 '11 at 13:29
  • i have several layouts with inline-block elements positioned using margins, even margin-top – Einacio Sep 30 '11 at 13:32

4 Answers4

99

you can also try replacing the negative margin with

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

in addition to the rest of your .label style

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Einacio
  • 3,502
  • 1
  • 17
  • 21
  • 9
    position: relative will move the element up by 2px relative to it's position, but it won't move the rest of the content below it. Thanks anyway. – Gregory Bolkenstijn Sep 30 '11 at 15:47
  • you can add `margin-bottom:-2px` to `.label` to balance it, and you don't have to add an extra div. but if you want to move everything from label down, wouldn't it be easier to set `margin-bottom:-2px` on whatever there is before .label? – Einacio Sep 30 '11 at 15:53
  • 3
    Negative margin-top or bottom on inline-block elements doesn't work, as we discoverd earlier on. :) – Gregory Bolkenstijn Sep 30 '11 at 16:56
  • 1
    Not a good solution. `position:relative;` always occupies space. You end up having unnecessary `2px` below. – Green Sep 10 '17 at 18:43
30

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.

.label {
  background: #ffffff;
  display: table;
  margin-top: -2px;
  padding: 7px 7px 5px;
}
baohouse
  • 490
  • 5
  • 9
21

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.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Zachary
  • 257
  • 2
  • 7
9

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. ;)

GolezTrol
  • 114,394
  • 18
  • 182
  • 210