Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why display=inline-block adds uncontrollable vertical margins

Tags:

html

css

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

enter image description here

works OK afer changing to simple block enter image description here

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> 
like image 262
Xtra Coder Avatar asked Nov 16 '13 22:11

Xtra Coder


People also ask

What is the deal with display inline block?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

How do I stop inline block wrapping?

As a result, the elements can sit next to each other. The major difference between display: inline; and display: inline-block; is that, display: inline-block; also allows us to set the width and height of the element. We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes.

Does margin working with inline block?

Inline-block elements are similar to inline elements, except they can have padding and margins added on all four sides.

Do inline elements respect margins?

The display:inline property treats an element as if it were a normal inline element such as <span> . For inline elements, horizontal padding and margins are respected, but the vertical margin and padding is discarded.


2 Answers

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.

like image 196
GolezTrol Avatar answered Oct 17 '22 21:10

GolezTrol


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/

like image 35
Michelle Avatar answered Oct 17 '22 22:10

Michelle