Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

html - Embedded padding/margin in fonts

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. Does anybody know why this is?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It sounds like the issue you are having is not with the CSS but the font itself. Most fonts define a baseline, x-height and line-height out-of-the-box. Your specific issue is likely with the font's leading, the space between lines. Sometimes these values can be wildly inconsistent. If you are really trying to get a specific font to line up correctly, I would recommend taking a look at FontLab and editing the glyphs/baseline/line-height for the specific font you are working with.

You can also look at a web-safe version of the font. These types of fonts usually have been specifically spaced to render best on the web. This isn't always the case, but it might get you the results you are looking for. Check out Google's library of web fonts here.

Update

This answer has received enough attention that I decided to add the first comment below to the answer and expound on it.

Browser Reset: Every browser will set default states for many of the reserved HTML tags like a and strong. There are other things defined by default including fonts, line-heights, weights and sizes. This could have an affect on the rendering of a font. Generally this is localized to specific browsers. So, by using a CSS reset, you can eliminate default rendering issues in browsers. Eric Meyers Reset is a good reset, but there are definitely others. Try using different ones to see which works best for you.

However, CSS resets work by targeting all browsers and setting them to all be the same. Some people prefer to use something that, instead, targets only the issues with each browser. That is were Normalize will be better.

There are also issues that a CSS reset will not fix. Such as font aliasing (making the fonts seem smooth rather than jagged). Since some browsers and operating systems do not add anti-aliasing to fonts, you will also see glyph width differences. This, unfortunately, is unavoidable in most cases. Some people go the route of using a flash font replacement tool like Cufon or Sifr. This too has it's own list of issues (such as the FOUC).

Another Update

One other issue that is still out there is the problem with kerning, or the space between glyphs. There is a CSS property letter-spacing that will allow you to do a global kern on a block of text, but it lacks the ability to target individual glyphs like Photoshop or InDesign. The kerning is also based on whole-pixels, so you are limited by what you can achieve. It also has issues with IE and is not as reliable as one would hope. There is a javascript called kerningjs that is pretty decent but it, too, is whole-pixel based and therefore not as accurate as rasterized text.

On the whole, fonts on the web have gotten better over the past few years. Sadly, we are still dealing with issues from the past, when fonts were only intended to be printed or rasterized. There is hope on the horizon for us font enthusiasts, though. As @allcaps said, the CSS3 specification for linebox is out there, so it's only a matter of time until it is widely accepted!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...