Is there a space by default for inline block elements? i think there's both margin-left
and margin-bottom
set to -4px
(on chrome, margin-bottom is -5px). why would they want to do that? i mean because of this, while setting margin-top
for the container of all these inline-block
elements (each were 25%), the third element is ending up on the next line. why does this happen?
Asked
Active
Viewed 202 times
0

Roopendra
- 7,674
- 16
- 65
- 92

user2957214
- 119
- 2
- 9
-
i have like 75+ lines of code for html, mate.:-( – user2957214 Nov 19 '13 at 09:11
-
1add it and make a fiddle. – Nitesh Nov 19 '13 at 09:12
-
2Inline block elements are treated like words in a sentence so if you have spaces between them in your code (ie for indentation) then you will get spaces between them on the screen. Either delete the space or comment it out – Pete Nov 19 '13 at 09:13
-
does that mean breaking space will end up -4px margin as well? – user2957214 Nov 19 '13 at 09:15
-
Depends on your font size – Pete Nov 19 '13 at 09:22
2 Answers
2
Just comment the spaces and line breaks between the elements to avoid the unwanted margin:
<div class="inline-block-element></div><!--
--><div class="inline-block-element></div>

Jonas Grumann
- 10,438
- 2
- 22
- 40
-
i tried margin-left: -4px. it worked. but why would adding margin/padding top affect the structure of the elements? – user2957214 Nov 19 '13 at 09:20
-
You should thing about inline-blocks elements as single words. The only difference is that they also are tags, which means they can be styled, allowing them to have a margin that actually moves them around. You could add margin to a single word, if there is a way to target only that word in the css. – Jonas Grumann Nov 19 '13 at 09:28
1
Here are two common ways to avoid the space:
- You can shift the tags up to avoid line breaks between elements:
<ul>
<li>
one</li><li>
two</li><li>
three</li>
</ul>
- Or you can comment out the line break:
<ul>
<li>one</li><!--
--><li>two</li><!--
--><li>three</li>
</ul>
You get the space because there is some space between the elements (tabs and line breaks count as "spaces" ). By getting rid of the spaces between the elements, you'll fix the problem. :)

Richard Shi
- 625
- 1
- 9
- 21

Mr. iC
- 129
- 9