Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display: Inline block - What is that space? [duplicate]

Tags:

css

width

Inline blocks have this weird space in-between them. I could live with it, up to a point where, if I load more content with an AJAX call, the tiny space goes away. I know I'm missing something here.

div {     width: 100px;     height: auto;     border: 1px solid red;     outline: 1px solid blue;     margin: 0;     padding: 0;      display: inline-block; } 

http://jsfiddle.net/AWMMT/

How to make the spacing consistent in Inline blocks?

like image 238
pyronaur Avatar asked May 21 '13 20:05

pyronaur


People also ask

What does display inline block mean?

“display: inline-block” Property: This property is used to display an element as an inline-level block container. The element itself is formatted as an inline element, but it can apply height and width values. It is placed as an inline element (on the same line as adjacent content).

What is the deal with display inline block?

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 you put a space between two inline elements?

HTML Break (<br>) Tag If you want to prevent a line break between two words, use a non-breaking space. If you want to insert a line break, use the HTML break tag, written as <br>.


2 Answers

The space is in the HTML. There are several possible solutions. From best to worst:

  1. Remove the actual space in the HTML (ideally your server could do this for you when the file is served, or at least your input template could be spaced appropriately) http://jsfiddle.net/AWMMT/2/
  2. Use float: left instead of display: inline-block, but this has undesirable effects on t he height: http://jsfiddle.net/AWMMT/3/
  3. Set the container's font-size to 0 and set an appropriate font-size for the internal elements: http://jsfiddle.net/AWMMT/4/ -- this is pretty simple, but then you can't take advantage of relative font size rules on the internal elements (percentages, em)
like image 200
Explosion Pills Avatar answered Sep 19 '22 10:09

Explosion Pills


http://jsfiddle.net/AWMMT/1/

<div>...</div><div>...</div>               ^               |--- no whitespace/new line here. 

Your spaces were the new lines the browser converted to "spaces" when displaying it.

Or you could try to hack a bit with CSS:

A flexbox conveniently ignores whitespace between its child elements and will display similarly to consecutive inline-block elements.

http://jsfiddle.net/AWMMT/470/

body { display: flex; flex-wrap: wrap; align-items: end; } 

Old answer (still applies to older, pre-flexbox browsers) http://jsfiddle.net/AWMMT/6/

body { white-space: -0.125em; } body > * { white-space: 0; /* reset to default */ } 
like image 22
bwoebi Avatar answered Sep 19 '22 10:09

bwoebi