Embedded padding/margin in fonts

后端 未结 16 967
执念已碎
执念已碎 2021-01-08 00:56

It seems that all fonts have some sort of embedded padding or margin. By setting:

margin: 0px;
padding: 0px;

You never get what you want. D

相关标签:
16条回答
  • 2021-01-08 01:25

    You can do it with line-height

    I know it's not too common in HTML5, and is more HTML4.1, but...

    <font style="line-height: 5px;">
    

    and if it's really that important:

    <font style="padding: 5px;">L</font>
    <font style="padding: 5px;">o/font>
    <font style="padding: 5px;">r</font>
    <font style="padding: 5px;">e</font>
    <font style="padding: 5px;">m</font>
    
    0 讨论(0)
  • 2021-01-08 01:26

    Its not a problem with the font as such. Yes, as @matthew said, the font design itself has some character built in. For example, check out difference between say "Segoe" family and "Verdana" family. You will keep on resetting your css if you need to use both. One style just won't work.

    The larger part of the problem lies in the way different browsers render even on the same OS. Heck, even different versions of IE render differently. ClearType, Anti-aliasing, font smoothing, software rendering instead of GPU rendering, rendering engine itself, etc. etc. all play their role to make sure you don't end up with pixel-perfect design across all browsers across all OSs.

    ClearType tries to align with pixel-grid causing another set of problems with subtle differences in height.

    This link is very old, but still very relevant: http://www.joelonsoftware.com/items/2007/06/12.html

    See Also: http://www.codinghorror.com/blog/2007/06/whats-wrong-with-apples-font-rendering.html

    See Also: https://webmasters.stackexchange.com/questions/9972/how-can-i-make-fonts-render-the-same-way-across-different-web-browsers

    See Also: CSS font differences between browsers

    Your best bet would be to keep tinkering with css until you get close enough.

    0 讨论(0)
  • 2021-01-08 01:26

    I think is kerning what you intend to describe

    take a look to this library

    http://kerningjs.com/

    CSS, meet kerning. Kerning, meet CSS. Kern, style, transform, and scale your web type with real CSS rules, automatically.

    Print designers have had it easy for way too long. This is 2012; the web has been around for over two decades, yet web designers don’t get full control over their typography? Forget that, use Kerning.js!

    it's free

    0 讨论(0)
  • 2021-01-08 01:28

    The "padding" at the top and bottom of fonts is essentially reserved space for diacritics (https://en.wikipedia.org/wiki/Diacritic). Some scripts stack multiple diacritics on some letters, including capitals (for example, Vietnamese https://en.wikipedia.org/wiki/Vietnamese_alphabet) so a font designer that forgets to reserve some place for them won't be able to extend his font later. Also, horizontal scripts require some separation (leading) between lines to be readable.

    Only very specific glyphs like box drawing elements extend to the limits of a glyph box http://www.unicode.org/charts/PDF/U2500.pdf That's also why the "padding" is built-in each glyph. If it was an external property it would not be possible to differentiate between glyphs intended to be jointive and glyphs that need some separation (in other words the amount of padding is a glyph property no a whole-font property).

    The following example requires a good font with decent Unicode coverage (http://dejavu-fonts.org/ works)

    Jointive box drawing elements

    ┃ÇŖŞ
    ┃ẤỄǛȰ┃U ← You really need to include the "padding" to align with box drawings
      ↑
     Latin capitals with multiple diacritics (really crowded)

    Lastly fonts stem from very old technology (movable type), and the conventions used to describe them still refer to fifteenth century habits, which makes them quite un-obvious to laymen.

    (See also http://www.webfonts.info/node/330 for info on complications added by computer font formats)

    fonts opentype text-rendering

    0 讨论(0)
  • 2021-01-08 01:29

    I often run into the same issue, especially when trying to get text to top-align with something beside it that isn't text, such as an image.

    Here's a technique I've had some success with. Select a portion of the text so that a colored background appears behind it. The top of the selection highlight will reveal what the font considers the "top" and "bottom" of the letter. Take screenshots of the font at various sizes and across multiple browsers. Zoom in on the screen capture in Photoshop and count the number of pixels from what you believe "should" be the top and the actual top. Calculate the percentage that number of pixels represents within the entire selection height.

    Then set a negative top margin on the text in ems that is equal to the percentage of the "overflow." So if the text should be 10px tall and it's actually 12px tall, then give it a negative top margin of -0.2em.

    Then wherever you assign the font-family that's causing the problem, also include this negative top margin as well. You can use the same technique for bottom and side overflow as well, but I typically find it's the top that causes the biggest headaches.

    0 讨论(0)
  • 2021-01-08 01:34

    The native margins for text elements are as follows (at least in Firefox and Chrome):

    Working Example

     p{margin:16px 0;}
    h1{margin:21px 0;}
    h2{margin:19px 0;}
    h3{margin:18px 0;}
    h4{margin:21px 0;}
    h5{margin:22px 0;}
    h5{margin:24px 0;}
    

    To remove them you'll have to re-set the margin like so:

    p, h1, h2, h3, h4, h5, h6{margin:0;}
    
    0 讨论(0)
提交回复
热议问题