CSS显示:inline-block不接受margin-top?
我有一个带有 display: inline-block 的元素,但它似乎不接受 margin-top 。这是因为该元素仍然被视为内联元素吗?
如果是,有人有解决方法吗?
编辑 #1:
我的 CSS 非常简单:
.label {
background: #ffffff;
display: inline-block;
margin-top: -2px;
padding: 7px 7px 5px;
}
我最终将内容包装在另一个 div 中,并给它一个 margin-top 。但这会导致很多额外的标记,并使我的代码不太清晰。
编辑#2:
margin-top
& inline-block
元素上的 margin-bottom
似乎仅适用于正值。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以尝试将负边距替换为
除了
.label
样式的其余部分之外,you can also try replacing the negative margin with
in addition to the rest of your
.label
style我使用了
display: table
。它具有内联块的内容适配属性,但也支持负边距,这种方式会随之移动其后面的内容。可能不是你应该如何使用它,但它确实有效。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.你可以像这样尝试
vertical-align
:我在 jsfiddle 上做了一个例子: http:// /jsfiddle.net/zmmbreeze/X6BjK/。
但垂直对齐在 IE6/7 上效果不佳。并且存在 Opera(11.64) 渲染错误。
所以我建议使用
position:relative
代替。You can try
vertical-align
like this: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.确实如此。您可以使用填充来代替边距。另一种解决方案是为元素使用容器 div。您将该 div 设置为
inline-block
,并将当前元素设置为该容器内的块。然后,您可以为元素留出边距。如果您有一个具体的示例,最好是在 jsfiddle.net 等地方,这将会有所帮助。这也将有助于答案更加具体,而不是像我这里那样只包含一般性描述。 ;)
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. ;)