Why display=inline-block adds uncontrollable vertical margins

后端 未结 4 1131
梦谈多话
梦谈多话 2020-12-05 02:42

I\'m trying to fiddle my problem on http://jsfiddle.net and have got strangest behaviour there. Can you please explain where these (http://jsfiddle.net/C6V3S/) vertical marg

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 03:08

    It's "just how it works," but it can be worked around. The easiest way to fix it would be to set negative margins on .btn. It should work for any modern browser (IE8 and above, if I recall). Floating the elements should achieve what you want, too. As a wholly different solution if your problem with it is merely aesthetic, you can just wrap the elements in a parent, set that parent's background-color to what you want, and not worry about its child elements' backgrounds. They'll be transparent to whatever is beneath them, and you'll get rid of those visual breaks.

    If you want to stick with negative margins on with display: inline-block, but need that pesky first element not to jump leftward out of its parent, you could target it explicitly:

    .btn {
        margin-left: -4px;
    }
    
    .btn:first-of-type {
        margin-left: 0px;
    }
    

    Edit: After reading another answer, I'm not sure if I understood the question -- do you mean the margin atop the elements (which would be horizontal) or the margin to the sides (vertical)?

提交回复
热议问题