How to remove the space between inline/inline-block elements?
Alternatively, you should now use flexbox to achieve many of the layouts that you may previously have used inline-block for: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Since this answer has become rather popular, I'm rewriting it significantly.
Let's not forget the actual question that was asked:
How to remove the space between inline-block elements? I was hoping
for a CSS solution that doesn't require the HTML source code to be
tampered with. Can this issue be solved with CSS alone?
It is possible to solve this problem with CSS alone, but there are no completely robust CSS fixes.
The solution I had in my initial answer was to add font-size: 0
to the parent element, and then declare a sensible font-size
on the children.
http://jsfiddle.net/thirtydot/dGHFV/1361/
This works in recent versions of all modern browsers. It works in IE8. It does not work in Safari 5, but it does work in Safari 6. Safari 5 is nearly a dead browser (0.33%, August 2015).
Most of the possible issues with relative font sizes are not complicated to fix.
However, while this is a reasonable solution if you specifically need a CSS only fix, it's not what I recommend if you're free to change your HTML (as most of us are).
This is what I, as a reasonably experienced web developer, actually do to solve this problem:
<p>
<span>Foo</span><span>Bar</span>
</p>
Yes, that's right. I remove the whitespace in the HTML between the inline-block elements.
It's easy. It's simple. It works everywhere. It's the pragmatic solution.
You do sometimes have to carefully consider where whitespace will come from. Will appending another element with JavaScript add whitespace? No, not if you do it properly.
Let's go on a magical journey of different ways to remove the whitespace, with some new HTML:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
You can do this, as I usually do:
<ul>
<li>Item 1</li><li>Item 2</li><li>Item 3</li>
</ul>
http://jsfiddle.net/thirtydot/dGHFV/1362/
Or, this:
<ul>
<li>Item 1</li
><li>Item 2</li
><li>Item 3</li>
</ul>Or, use comments:
<ul>
<li>Item 1</li><!--
--><li>Item 2</li><!--
--><li>Item 3</li>
</ul>Or, if you are using using PHP or similar:
<ul>
<li>Item 1</li><?
?><li>Item 2</li><?
?><li>Item 3</li>
</ul>Or, you can even skip certain closing tags entirely (all browsers are fine with this):
<ul>
<li>Item 1
<li>Item 2
<li>Item 3
</ul>
Now that I've gone and bored you to death with "one thousand different ways to remove whitespace, by thirtydot", hopefully you've forgotten all about font-size: 0
.
Extra Space Between inline-block DIV Elements
You need to comment out the white space between the elements, like so:
<div class="tile-container">
<div class="tile-large">1</div><!--
--><div class="tile-wide">2</div><!--
--><div class="tile-small">3</div><!--
--><div class="tile-small">4</div><!--
--><div class="tile-small">5</div><!--
--><div class="tile-small">6</div><!--
--><div class="tile-wide">7</div><!--
--><div class="tile-large">8</div>
</div>
DEMO: http://jsfiddle.net/P7cbA/11/
Another way is to put the elements in the HTML in one line without any spaces between them, but this reduces the code readability:
<div class="tile-container">
<div class="tile-large">1</div><div class="tile-wide">2</div><div class="tile-small">3</div><div class="tile-small">4</div><div class="tile-small">5</div><div class="tile-small">6</div><div class="tile-wide">7</div><div class="tile-large">8</div>
</div>
Spacing between inline-block divs, can't solve by removing whitespace
You could ty using a negative margin on the inline-block
items.
Take a look at this jsFiddle
.col-center {
display: inline-block;
border: 1px solid blue;
text-align: left;
margin: 0 -2px;
padding: 10px;
}
How can I eliminate spacing between inline elements in CSS?
try to add img {margin:0;padding:0;float:left}
in other words remove any default margin
and padding
of browsers for img
and float
them.
Demo: http://jsfiddle.net/PZPbJ/
Unwanted spacing between inline-table elements
Either float the elements, or remove the whitespace in between. On inline elements (which includes styling them to display: inline-table
or inline-block
), whitespace is shown too, even if it is collapsed to a single space. So:
<div class="content"><div class="left"></div><div class="middle"></div><div class="right"></div></div>
works fine.
Why is there an unexplainable gap between these inline-block div elements?
In this instance, your div
elements have been changed from block
level elements to inline
elements. A typical characteristic of inline
elements is that they respect the whitespace in the markup. This explains why a gap of space is generated between the elements. (example)
There are a few solutions that can be used to solve this.
Method 1 - Remove the whitespace from the markup
Example 1 - Comment the whitespace out: (example)
<div>text</div><!--
--><div>text</div><!--
--><div>text</div><!--
--><div>text</div><!--
--><div>text</div>
Example 2 - Remove the line breaks: (example)
<div>text</div><div>text</div><div>text</div><div>text</div><div>text</div>
Example 3 - Close part of the tag on the next line (example)
<div>text</div
><div>text</div
><div>text</div
><div>text</div
><div>text</div>
Example 4 - Close the entire tag on the next line: (example)
<div>text
</div><div>text
</div><div>text
</div><div>text
</div><div>text
</div>
Method 2 - Reset the font-size
Since the whitespace between the inline
elements is determined by the font-size
, you could simply reset the font-size
to 0
, and thus remove the space between the elements.
Just set font-size: 0
on the parent elements, and then declare a new font-size
for the children elements. This works, as demonstrated here (example)
#parent {
font-size: 0;
}
#child {
font-size: 16px;
}
This method works pretty well, as it doesn't require a change in the markup; however, it doesn't work if the child element's font-size
is declared using em
units. I would therefore recommend removing the whitespace from the markup, or alternatively floating the elements and thus avoiding the space generated by inline
elements.
Method 3 - Set the parent element to display: flex
In some cases, you can also set the display
of the parent element to flex
. (example)
This effectively removes the spaces between the elements in supported browsers. Don't forget to add appropriate vendor prefixes for additional support.
.parent {
display: flex;
}
.parent > div {
display: inline-block;
padding: 1em;
border: 2px solid #f00;
}
.parent {
display: flex;
}
.parent > div {
display: inline-block;
padding: 1em;
border: 2px solid #f00;
}
<div class="parent">
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
</div>
Mysterious whitespace between inline-block divs
Solution 1: Add comments:
<div class="tr" style="width: 150px;">
<div class="td" style="width: 50px; background-color: #CCC;"></div><!--
--><div class="td" style="width: 50px; background-color: #AAA;"></div><!--
--><div class="td" style="width: 50px; background-color: #666;"></div>
</div>
You can write everything on the same line, too, but it looks cleaner with comments.
Solution 2: Add font-size:0
to the parent element. Don't forget to define the font-size for child elements:
.tr {
font-size: 0;
}
.td {
font-size: 15px;
}
Related Topics
Css3Pie in MVC, Where to Place the Pie.Htc File
Controlling CSS Cube Rotation(Transform) and Extracting Values from 3D Matrix
Fontawesome - Failed to Decode Downloaded Font
How to Identify and Eliminate Unused CSS Styles from My Bloated Stylesheet
Passing CSS Property:Value as SASS Mixin Argument
Ie11 Making Background Image Text Blurry
Apply Style to First Element in a Row of Similar Elements
Conditional CSS Rule Targeting Firefox Quantum
Svg with Radialgradient Not Work in Browsers
How to Make My Font Bold Using CSS
Google Chrome, Flash and Z-Index Wrong Behaviour
Extracting Content in :After Using Xpath
How to Compensate for Vertical Scrollbar When It Is Not Yet Present
Keep Width When Using Letter-Spacing on Hover
Maintain Aspect Ratio of a Div According to Height
How to Draw a Trapezoid-Like Card
Bootstrap 4 How to Have Margin Between Columns Without Going Over Space