Remove white space above and below large text in an inline-block element
It appears as though you need to explicitly set a font, and change the line-height
and height
as needed. Assuming 'Times New Roman' is your browser's default font:
span {
display: inline-block;
font-size: 50px;
background-color: green;
/*new:*/
font-family: 'Times New Roman';
line-height: 34px;
height: 35px;
}
<span>
BIG TEXT
</span>
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
.
How to remove space above and below the in-line block elements?
ul
has by default margin
and padding
, so reset it
body { padding: 0; margin: 0; font-family: sans-serif;}header { background: #00795f; width: 100%; padding: 40px 0; /* 40px top and bottom, 0px left and right */ color: white; text-align: center;}nav { background-color: #43a286; color: white; overflow: hidden; padding: 0px; text-align: center; margin: 0;}ul { /* padding: 0; you may need only the margin */ margin: 0}a { text-decoration: none; color: inherit;}nav ul li { font-size: 16px; display: inline-block; padding: 20px; border: 3px solid black;}ul li:hover { background-color: #399077;}section { line-height: 1.5em; font-size: 1.5em; padding: 40px; width: 75%; margin: 0 auto;}
<header> <h1>Loving it more everyday</h1> </header> <nav> <ul> <li> <a href="#" target="_blank"> Home </a> </li> <li> <a href="#" target="_blank"> About </a> </li> <li> <a href="#" target="_blank"> Blog </a> </li> <li> <a href="#" target="_blank"> Shop </a> </li> <li> <a href="#" target="_blank"> Gallery </a> </li> <li> <a href="#" target="_blank"> Contact </a> </li> </ul> </nav> <section> When you try your best, but you don't succeed. When you get what you want, but not what you need. When you feel so tired, but you can't sleep. Stuck in reverse. And the tears come streaming down your face. When you lose something you can't replace.When you love someone, but it goes to waste. Could it be worse? Lights will guide you home. And ignite your bones. And I will try to fix you. And high up above or down below. When you're too in love to let it go. But if you never try you'll never know. Just what you're worth. When you try your best, but you don't succeed. When you get what you want, but not what you need. When you feel so tired, but you can't sleep. Stuck in reverse. And the tears come streaming down your face. When you lose something you can't replace.When you love someone, but it goes to waste. Could it be worse? Lights will guide you home. And ignite your bones. And I will try to fix you. And high up above or down below. When you're too in love to let it go. But if you never try you'll never know. Just what you're worth </section>
How to remove whitespace from above text in a div
Try fiddling with line-height and em like so:
line-height: 1em;
This way, whatever your font-size is, the line-height will adjust automagically. 1em might be too much.. perhaps .667em is a better fit?
White space above second inline-block div
Default value of vertical-align
is baseline
and when applied to blocks of different heights, it's often unwanted.
Applying a value of top
will solve your problem. Here's a working fiddle: http://jsfiddle.net/PhilippeVay/B2S4r/3/ (as there's no stylesheet in your fiddle but only inline CSS, I won't even try to find how to aim for the one on the right)
Remove white space above and below large text in an inline-block element
It appears as though you need to explicitly set a font, and change the line-height
and height
as needed. Assuming 'Times New Roman' is your browser's default font:
span {
display: inline-block;
font-size: 50px;
background-color: green;
/*new:*/
font-family: 'Times New Roman';
line-height: 34px;
height: 35px;
}
<span>
BIG TEXT
</span>
Related Topics
Change Bootstrap Tooltip Color
How to Delete an Item or Object from an Array Using Ng-Click
Css Content Property: How to Insert HTML Instead of Text
How to Create Custom Tags For Html
How to Wrap Text Around an Image Using Html/Css
Are Single Quotes Allowed in Html
Why Is It a Bad Thing to Have Multiple HTML Elements With the Same Id Attribute
Add Centered Text to the Middle of a Horizontal Rule
Why Is a Div With "Display: Table-Cell;" Not Affected by Margin
Drop-Down Menu That Opens Up/Upward With Pure Css
Difference Between ≪Section≫ and ≪Div≫
Css Background Image to Fit Width, Height Should Auto-Scale in Proportion
Detect When an Html5 Video Finishes