block adds uncontrollable vertical margins

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 margins come from? Does appear on jsfiddle.net (at least in Chrome and FF), do not appear when copy/pasting to local standalone file ...

在这里输入图像描述

works OK afer changing to simple block 在这里输入图像描述

Sample for standalone test file: .btn { padding: 0px; border: 1px solid red; display: inline-block; }

.txt {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: none;
    padding: 0;
    margin: 0;
    background: #77FF77;
}
</style>

<div class="btn"><div class="txt"></div></div>

When you make the .txt element inline-block , it is taken into the text flow of the parent .btn element. At that point, the line-height of .btn kicks in, which is larger than the height of the .txt element.

So, add .btn {line-height: 10px;} (for example) and your problem is fixed. I saw you already tried to influence the line-height of the inner .txt element, so you were pretty close with your attempts. Adjusting the font-size would work as well, since the default line-height is formula-based on the font-size. Anyway, the key is to do that on the parent element, not the child element.

You don't have this problem when you make the inner element a block , because then there's no text-like content, so there's no line-height applied at all.


inline-block is brilliant and convenient for so many cases. But they come with one annoying pitfall: unnecessary whitespace . As you're nesting an inline-block within an inline-block, that results in a white-space disaster. You can read all about whitespace and inline-blocks on David Walsh's blog.

There are many ways to handle this, but my favorite (and most widely supported) solution is setting this to the parent inline-block element:

.btn {
  font-size: 0;
}

Here is an example of before/after: http://jsfiddle.net/C6V3S/1/


This is not caused by whitespace (you don't have any inside the div s in your HTML), nor by putting inline-block inside of another inline-block . This is caused because of the line-height of the outer div . As you can see from the below fiddle, the current line-height of the red-border div has a line-height that is being set by the browser (and it varies from browser to browser), and as long as there is something inside of the div that takes up space as an inline or inline-block item, the height will be affected by the line-height .

There are many ways around this, and they all pretty much do the same thing:

1) Take the inside element out of flow, and then reinsert it back in. This will make the outer div think that there is nothing inside of it, and try to become as small as possible, and then fill up space exactly around the element when it inserted back in. The easiest way to do this is by float ing it.

2) Make the outer element take up no space. This will make the inside element define the height and width of its parent. If you do font-size: 0 , as mentioned before, this will make the outer element think that the inline inside element takes up no space, and therefore not take up any space itself, and wrap itself tightly around the inner element. This can be done by setting line-height: 0 as well (which is a perfect fix for the problem, as the problem is line-height ).

There are other ways to make the parent element not have its line-height , but that should get you started.

http://jsfiddle.net/C6V3S/4/

链接地址: http://www.djcxy.com/p/89258.html

上一篇: 使用display时不需要的空格:inline

下一篇: 块增加了不可控制的垂直边距