99

Say I have a single span element defined as an inline-block. It's only contents is plain text. When the font size is very large, you can clearly see how the browser adds a little padding above and below the text.

HTML:

CSS:

span {
  display: inline-block;
  font-size: 50px;
  background-color: green;
}

​
<span>BIG TEXT</span>

Looking at the box model, it's clear the browser is adding padding inside the content edge. I need to remove this "padding", one way is to simply alter the line-height, as with:

http://jsfiddle.net/7vNpJ/1/

This works great in Chrome but in Firefox the text is shifting towards the top (FF17, Chrome 23, Mac OSX).

Any idea of a cross-browser solution? Thanks!

Esko
  • 4,109
  • 2
  • 22
  • 37
MusikAnimal
  • 2,286
  • 2
  • 22
  • 28
  • I can't see the difference of the second fiddle between Firefox and Chrome. – erenon Dec 27 '12 at 21:06
  • Firefox 17, Chrome 23, Mac OSX – MusikAnimal Dec 27 '12 at 21:09
  • According to http://www.w3schools.com/cssref/pr_dim_line-height.asp, CSS line-height is fully compatible with Firefox. – Victor Dec 27 '12 at 21:23
  • Of course it is... that doesn't mean it renders the same. Also w3schools is a terrible resource, please see [w3fools](http://w3fools.com/). I appreciate the help nonetheless – MusikAnimal Dec 27 '12 at 21:31
  • I tried all the solutions mentioned above but none of them worked as I have to position the text at the bottom of the screen and the font itself was taking extra space. So, I added height, line-height and overflow hidden and it worked – watereffect Jun 15 '17 at 10:58

9 Answers9

77

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>
Roj
  • 995
  • 1
  • 8
  • 22
karacas
  • 2,054
  • 1
  • 19
  • 29
  • Appears to have the same issue in Firefox, the text is cut off at the top – MusikAnimal Dec 27 '12 at 21:20
  • Perhaps the font in win is different from mac, try loading another font, example: http://jsfiddle.net/7vNpJ/18 – karacas Dec 27 '12 at 21:25
  • 2
    Interesting, it appears as though setting a font, does, indeed, work! I'm guessing it's an issue with Times New Roman, luckily I'll be using Arial. Thanks! – MusikAnimal Dec 27 '12 at 21:35
  • 4
    This heavily depends on the font: the font designer decides how tall the letters are with respect to the font size. Even with Arial as the font, my Firefox displays the sample text as partly cut off. – Jukka K. Korpela Dec 27 '12 at 21:43
  • I was able to switch the font to Arial, and adjust the `line-height` and `height` to make it appear the same in both Chrome and FF. I have not tested such on Windows, however – MusikAnimal Dec 27 '12 at 21:46
  • Is there a similar simple solution for multi-line text containing divs? – Julian F. Weinert Apr 25 '14 at 11:49
  • 1
    This is not a working solution. You can see if small letter "gjy" is input, they overflow. http://jsfiddle.net/7vNpJ/665/ – cytsunny Mar 23 '17 at 08:58
48

The browser is not adding any padding. Instead, letters (even uppercase letters) are generally considerably smaller in the vertical direction than the height of the font, not to mention the line height, which is typically by default about 1.2 times the font height (font size).

There is no general solution to this because fonts are different. Even for fixed font size, the height of a letter varies by font. And uppercase letters need not have the same height in a font.

Practical solutions can be found by experimentation, but they are unavoidably font-dependent. You will need to set the line height essentially smaller than the font size. The following seems to yield the desired result in different browsers on Windows, for the Arial font:

span.foo
{
  display: inline-block;
  font-size: 50px;
  background-color: green;
  line-height: 0.75em;
  font-family: Arial;
}

span.bar
{
  position: relative;
  bottom: -0.02em;
}
<span class=foo><span class=bar>BIG TEXT</span></span>

The nested span elements are used to displace the text vertically. Otherwise, the text sits on the baseline, and under the baseline, there is room reserved for descenders (as in letters j and y).

If you look closely (with zooming), you will notice that there is very small space above and below most letters here. I have set things so that the letter “G” fits in. It extends vertically a bit farther than other uppercase letters because that way the letters look similar in height. There are similar issues with other letters, like “O”. And you need to tune the settings if you’ll need the letter “Q” since it has a descender that extends a bit below the baseline (in Arial). And of course, if you’ll ever need “É”, or almost any diacritic mark, you’re in trouble.

Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
31

I'm a designer and our devs had this issue when dealing with Android initially, and our web devs are having the same problem. We found that the spacing between a line of text and another object (either a component like a button, or a separate line of text) that a design program spits out is incorrect. This is because the design program isn't accounting for diacritics when it is defining the "size" of a single line of text.

We ended up adding Êg to every line of text and manually creating spacers (little blue rectangles) that act as the "measurement" from the actual top of the text (ie, the top of the accent mark on the E) or from the descender (the bottom of a "g"). For example, say you have a really boring top navigation that is just a rectangle, and a headline beneath it. The design program will say that the space between the bottom of the top nav and the top of the headline textbox 24px. However, when you measure from the bottom of the nav to the top of an Ê accent mark, the spacing is actually 20px.

While I realize that this isn't a code solution, it should help explain the discrepancies between the design specs and what the build looks like.

See this image for an example of what Sketch does with type

Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
Sarah McDermott
  • 411
  • 4
  • 2
  • Answer says that problem is not solvable? – Homayoun Behzadian Jun 03 '20 at 07:28
  • 2
    love the mention of `Êg` now things make more sense – Toskan Feb 12 '21 at 20:39
  • 3
    over 20 years of web development and yet, today, this answered a perpetual question about line-height... that is... "yeah... but WHY???"... now i know! this is one of my all time favorite answers because it approached the answer, not so much with a direct solution, but with something more powerful: UNDERSTANDING. – aequalsb May 26 '21 at 16:45
3

I had a similar problem. As you increase the line-height the space above the text increases. It's not padding but it will affect the vertical space between content. I found that adding a negative top margin seemed to do the trick. It had to be done for all of the different instances of line-height and it varies with font-family too. Maybe this is something which designers need to be more aware of when passing design requirements (?) So for a particular instance of font-family and line-height:

h1 {
    font-family: 'Garamond Premier Pro Regular';
    font-size: 24px;
    color: #001230;
    line-height: 29px;
    margin-top: -5px;    /* CORRECTION FOR LINE-HEIGHT */
}
isherwood
  • 58,414
  • 16
  • 114
  • 157
omar j
  • 521
  • 6
  • 8
  • Yeah, definitely check the line-height – Kevin Lee Nov 17 '16 at 15:19
  • This solution is elegant and simple, works in a second. You will need to manually correct for smaller font sizes at smaller screen size queries but that doesn't take long to optimize. – Alex Banman Mar 24 '21 at 14:42
3
span::before,
span::after {
    content: '';
    display: block;
    height: 0;
    width: 0;
}

span::before{
    margin-top:-6px;
}

span::after{
    margin-bottom:-8px;
}

Find out the margin-top and margin-bottom negative margins with this tool: http://text-crop.eightshapes.com/

The tool also gives you SCSS, LESS and Stylus examples. You can read more about it here: https://medium.com/eightshapes-llc/cropping-away-negative-impacts-of-line-height-84d744e016ce

galengodis
  • 901
  • 1
  • 9
  • 18
1

This worked for me:

line-height: 80%;
Samhamsam
  • 87
  • 9
0

If its text that has to scale proportionally to the screenwidth, you can also use the font as an svg, you can just export it from something like illustrator. I had to do this in my case, because I wanted to align the top of left and right border with the font's top |TEXT| . Using line-height, the text will jump up and down when scaling the window.

Servus
  • 479
  • 3
  • 13
0

The best way is to use display:

inline-block; 

and

overflow: hidden;
-4

I've been annoyed by this problem often. Vertical-align would only work on bottom and center, but never top! :-(

It seems I may have stumbled on a solution that works for both table elements and free paragraph elements. I hope we are at least talking similar problem here.

CSS:

p {
    font-family: "Times New Roman", Times, serif;
    font-size: 15px;
    background: #FFFFFF;
    margin: 0
    margin-top: 3px;
    margin-bottom: 10px;
}

For me, the margin settings sorted it out no matter where I put my "p>.../p>" code.

Hope this helps...

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107