本文介绍了CSS显示:inline-block不接受margin-top?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素与display:inline-block,但它似乎不接受margin-top。这是因为元素仍被视为内联元素?

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?

编辑#1

我的CSS很简单:

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

我最终将内容封装在另一个div中, 。

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 inline-block 元素只适用于正值。

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

推荐答案

我使用 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.

这篇关于CSS显示:inline-block不接受margin-top?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:28