ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: CSS :: CSS @ Ten: ÏÂÒ»¸öÖØÒªµÄ¶«Î÷

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

CSS
CSS2 Aural
CSS µ¥Î»
CSS ÑÕÉ«
CSS ÑÕɫֵ
CSS ÑÕÉ«Ãû³Æ
CSS ±àÂëÓë×éÖ¯¼¼..
CSS Ä£¿é»¯Éè¼ÆË¼..
CSSÍêÃÀ¼æÈÝIE6/I..
CSS @ Ten: ÏÂÒ»¸..

CSS @ Ten: ÏÂÒ»¸öÖØÒªµÄ¶«Î÷


×÷Õß:Håkon Wium Lie ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-29 ä¯ÀÀ:2863 :: ::

CSS is ten years old this year. Such an anniversary is an opportunity to revisit the past and chart the future. CSS has fundamentally changed web design by separating style from structure. It has provided designers with a set of properties that can be tweaked to make marked-up pages look right—and CSS3 proposes additional properties requested by designers.

CSS¼¼ÊõÒѾ­×ß¹ýÁË10Äê¡£ÔÚÕâÑùÒ»¸öÖÜÄê¼ÍÄîµÄÈÕ×ÓÓ¦¸ÃºÃºÃµÄ»Ø¹Ë¹ýÈ¥£¬Õ¹ÍûδÀ´¡£CSSͨ¹ýÑùʽÓë½á¹¹·ÖÀëµÄÔ­ÀíÈÃWEBÉè¼ÆÓÐÁ˸ù±¾ÐԵĸĸËüËùÓµÓеÄһϵÁÐÊôÐÔ¿ÉÒÔÈÃÉè¼ÆÕß°ÑÒ³Ãæµ÷ÕûÃÀ¹Û -- ²¢ÇÒCSS3Ëù²¹³äµÄÊôÐÔ¸üÊDZ»Éè¼ÆÕßËùÆÚÍûµÃµ½µÄ¡£

Many CSS properties, both old and new, deal with text: they describe text color, position, style, and direction. This is all very good—after all, text fills most of our screens. But in order for properties to reach their full potential, we need a good selection of fonts. And fonts are sorely missing from the web.

Ï൱һ²¿·ÖµÄCSSÊôÐÔ£¬²»ÂÛÆäоɶ¼ÊÇÔÚ´¦ÀíÎÄ×Ö£ºËüÃÇÃèÊöÎÄ×ÖµÄÑÕÉ«£¬¶¨Î»£¬Ñùʽ£¬ÒÔ¼°·½Ïò¡£ÕâЩ¶¼ÊǷdz£²»´íµÄ -- ±Ï¾¹£¬ÔÚÆÁÄ»ÀïÎÄ×ÖÊÇÖ÷Òª½ÇÉ«¡£µ«ÒªÈÃÊôÐÔÄܹ»³ä·ÖÑÓÉìËüÃǵÄËùÓÐDZÄÜ£¬ÎÒÃÇÐèÒªÔÚ×ÖÌåÑ¡ÔñÉÏ×ö×㹤·ò¡£¿ÉÓÃÓÚWEBµÄ×ÖÌå·Ç³£µÄÏ¡ÉÙ¡£

Consider the fine designs in the CSS Zen Garden. What makes them so exciting to look at? In part, it is the variety of fonts. Fonts convey design messages and create effect, and while in traditional print design there are a plethora of fonts available, fonts have been in limited supply on the web. Web designers depend on ten or so universally available fonts for their designs, and are reduced in large part to using Verdana and Arial over and over again. A typical CSS Zen Garden design, on the other hand, uses a hand-picked font to render text and aligns the glyphs to a pixel-perfect degree...and then uses that text as a background image.

ÔÚCSS Zen Garden ÀïµÄÉè¼Æ·Ç³£²»´í¡£ ÈçºÎ²ÅÄÜ×öµ½Èç´ËÁîÈ˼¤¶¯µÄÍâ¹ÛÄØ£¿ Ëü·á¸»¶àÑùµÄ×ÖÌåÊÇÆäÖеÄÒ»²¿·Ö¡£×ÖÌå¿ÉÒÔ´«´ïһЩÉè¼ÆÀíÄÄܲúÉúЧ¹û£¬ËäÈ»ÔÚ´«Í³µÄÓ¡Ë¢Éè¼Æ¿ÉÒÔÑ¡ÔñµÄ×ÖÌåÉõÖÁÓÐЩ¹ýÊ££¬µ«×ÖÌåÔÚÖ§³ÖWEBÉ϶¼ÓÐÏÞÖÆ£¬WEBÉè¼ÆÕßÖ»ÄÜÔÚËûÃǵÄÉè¼Æ×÷Æ·ÀïÒÀ

Àµ10ÖÖ»òÊÇÄÇЩ¼¸ºõËùÓÐÉè¼ÆÕß¶¼ÔÚʹÓõÄ×ÖÌ壬¶øÇÒ¼«´ó²¿·Ö¶¼ÊÇÔÚÖØ¸´µÄʹÓÃVerdana ºÍ ArialÕâÁ½ÖÖ×ÖÌå¡£Ò»¸öµäÐ굀 CSS Zen Garden Éè¼Æ·ç¸ñ¾ÍÊdzÊÏÖ¾«ÖµÄÎÄ×Ö²¢ÇÒ×ÖÐÎÅÅÁе½ÏóËØ³Ì¶È£¬È»ºó½«ÎÄ×ÖÒÔ±³¾°Í¼Æ¬µÄÐÎʽÀ´Ê¹Óá£

A background image!
Ò»¸ö±³¾°Í¼Æ¬£¡

There are many reasons why background images should not be used to convey text. Images are expensive to transmit and hard to make. Imagine trying to translate a web page into 15 languages and having to produce a set of images for each language. Additionally, the quality of printed web pages suffers as images don’t scale to the resolutions offered by modern printers. Using background images is currently the only way designers can use their favorite fonts on the web. But shouldn’t web designers have access to a wider selection of fonts and be able to use them without having to resort to creating background images?

ÓÐÐí¶àÀíÓɲ»Ó¦¸ÃʹÓÃͼƬÀ´×÷ΪÎÄ×ֵĴ«µÝ·½Ê½¡£Í¼Æ¬µÄ´«Êä´ú¼Û¹ý¸ß¶øÇÒÖÆ×÷ÆðÀ´±È½ÏÀ§ÄÑ¡£ÉèÏëÒª½«Ò»ÕÅÒ³Ãæ×ª»»³É15ÖÖÓïÑÔÄǾͱØÐëΪÿÖÖÓïÑÔÌṩһ×éͼƬ¡£ÔÙ²¹³äһϣ¬Ê¹ÓÃÁ÷ÐеĴòÓ¡»úÎÞ·¨´òÓ¡³ö¸ßÖÊÁ¿µÄWEBÒ³Ãæ£¬ÒòΪͼƬÎÞ·¨´ïµ½ÏàÓ¦µÄ·Ö±æÂÊ¡£Ê¹Óñ³¾°Í¼Æ¬Êǵ±Ç°Éè¼ÆÕßΨһµÄÑ¡Ôñ£¬Ö»ÓÐÕâÑùËûÃDzſÉÒÔÔÚWEBÖÐʹÓÃÖÐÒâµÄ×ÖÌå¡£µ«Éè¼ÆÕßÄѵ¹²»Ó¦¸ÃŬÁ¦È¥Ñ°ÕÒÒ»ÖּȿÉÒÔʹÓ÷ḻµÄ×ÖÌåÓÖ¿ÉÒÔ²»ÐèÒª½«ËüÃÇ×ö³É±³¾°Í¼Æ¬µÄ·½·¨Ã´£¿

There is a way: web fonts. Instead of making pictures of fonts, the actual font files can be linked to and retrieved from the web. This way, designers can use TrueType fonts without having to freeze the text as background images.

ÓÐÕâôһÖÖ·½·¨£º WEB ×ÖÌ塣ȡ´úΪ×ÖÌåÖÆ×÷ͼƬ£¬ÕæÊµµÄ×ÖÌåÎļþ¿ÉÒÔͨ¹ýÍøÂç½øÐÐÁ¬½Ó²¢´ÓWEBÉÏÈ¡»Ø¡£ÕâÖÖ·½·¨£¬Éè¼ÆÕß¿ÉÒÔʹÓÃTrueType×ÖÌå¶ø²»Óý«ÎÄ×Ö¶³½á³É±³¾°Í¼Æ¬¡£

A brief history of web fonts
WEB×ÖÌå´ó¸Ù

This is not a new idea. In 1998, CSS2 described a way to link to fonts from style sheets, and both Microsoft and Netscape added support for web fonts in their browsers. However, neither vendor supported the most widely used font format, TrueType. Instead, they each picked a different, little-used format with few tools to support it (EOT and TrueDoc, respectively). And so web fonts disappeared from the designer’s toolbox.

Õâ²¢²»ÊÇʲôеÄÏë·¨¡£ÔçÔÚ1998Ä꣬CSS2¾Í±»Ãè»æ³öÁËÒ»ÖÖ¿ÉÒÔÑùʽ±íÁ¬½Ó×ÖÌåµÄ·½·¨£¬Í¬Ê±Î¢ÈíºÍÍø¾°¶¼·Ö±ðΪËûÃǵÄä¯ÀÀÆ÷Ôö¼ÓÁËWEB×ÖÌåµÄÖ§³Ö¡£ È»¶øÁ½Õß¶¼Ã»ÓÐÖ§³Ö¹ã·ºÊ¹ÓõÄ×ÖÌå¸ñʽ£¬ºÜÉٵĸñʽ¿ÉÒÔÓò»¶àµÄ¹¤¾ßÀ´Ö§³ÖËü(EOT ºÍ TrueDoc, respectively)¡£ÓÚÊÇ WEB×ÖÌå´ÓÉè¼ÆÕߵŤ¾ßÏäÀïÏûʧÁË¡£

Better news
¸üºÃµÄÏûÏ¢

It’s a pleasure for me to announce that web fonts are back! Here are some recently generated examples that show how a familiar document can be rendered when TrueType web fonts are available:

Õâ¶ÔÓÚÎÒÀ´ËµÊǸöÖµµÃÐû¸æµÄºÃÏûÏ¢£¬WEB×ÖÌ廨À´ÁË£¡ÕâÀïÓÐһЩ×î½ü²úÉúµÄ°¸ÀýչʾÁËÒ»µ©µ±TrueTypeWEB×ÖÌåÉúЧÒÔÍùÊìϤµÄÎĵµÄܳÊÏÖÔõÑùµÄ²»Í¬£º

Screenshot of web page using real TrueType fonts. PDF (via Prince). HTML (via your browser).

Screenshot of web page using real TrueType fonts. PDF (via Prince). HTML (via your browser).

Screenshot of web page using real TrueType fonts. PDF (via Prince). HTML (via your browser).

The PDF (and image) examples shown in this article have been produced by Prince 6, a web-to-PDF-through-CSS renderer. (The Macintosh version must be installed via Terminal. Instructions are included in a ReadMe file. —Ed.) Prince is currently the only program that supports TrueType web fonts, but it provides the proof of concept we need to begin thinking seriously about web fonts—and to begin advocating for their implementation in common web browsers. Before we get to that, though, let’s take a look at the code beneath these examples.

(Disclosure: I’m on the board of YesLogic, the company behind Prince. Being on the board is a neat way of pushing for your favorite features to be implemented.)

The examples are coded in HTML and CSS only; no images are involved. CSS3 multi-column layouts and rounded borders have been added for good measure, but they are not essential to the designs. Each example uses four different web fonts. The style sheets point to web font files which are fetched before the documents are rendered.

°¸ÀýµÄЧ¹ûÖ»Ðè±àдHTMLºÍCSS´úÂ룻²»°üº¬ÈκÎÓйØÍ¼Æ¬µÄÐÅÏ¢¡£CSS3 µÄ¶à²ãÖù²¼¾ÖÒÔ¼°Ô­Ðα߿òµÄÔö¼ÓÆðµ½Á˺ܺõĵ÷Õû£¬µ«ËüÃDz»ÊÇÉè¼ÆÉϵĹؼü¡£°¸ÀýʹÓÃÁËËÄÖÖ²»Í¬µÄWEB×ÖÌå¡£Ñùʽ±íÖ¸ÏòÁËÎĵµÐèÒª³ÊÏÖ×ÖÌåµÄÎļþ¡£

Web fonts: the code you'll use
WEB×ÖÌ壺Ä㽫ʹÓõ½µÄ´úÂë

Here is a simple example of how to use web fonts (Line wraps marked » —Ed.):

ÕâÊǸöʹÓÃWEB×ÖÌåµÄ¼òµ¥¾ÙÀý£º

@font-face {
font-family: "Kimberley";
src: url(http://www.princexml.com/fonts/larabie/ »
kimberle.ttf) format("truetype");
}
h1 { font-family: "Kimberley", sans-serif }

Those familiar with CSS syntax will recognize the last line. The @font-face construct may not be familiar, however it’s easy to explain: as the “Kimberley” font is requested, the font file is fetched from the specified URL. The syntax is described in the CSS2 specification.

×îºóÒ»ÐÐÊÇÎÒÃÇËùÊìϤµÄCSSÓï·¨¡£@font-face ½á¹¹»òÐí¾Í²»ÄÇôÊìϤÁË£¬È»¶øÕâ¸ö½âÊÍÆðÀ´Ò²ÈÝÒ×£ºÇëÇó“Kimberley”×ÖÌ壬×ÖÌåÎļþ»á´ÓÖ¸¶¨µÄURLµØÖ·ÉÏ»ñÈ¡µ½£¬Óï·¨ÒѾ­Ð´ÈëÁËCSS2¹æ¸ñÎĵµ

To avoid long lists of @font-face declarations in the style sheet, they can be hidden using @import (Line wraps marked » —Ed.):

¿ÉÒÔʹÓÃ@importÀ´±ÜÃâÔÚÑùʽ±íÖÐÂÞÁд󴮵Ä@font-faceÉùÃ÷£º

@import url(http://www.princexml.com/fonts/  »
larabie/index.css) all;
h1 { font-family: Goodfish, serif }

The TrueType files used in the examples above are designed by Ray Larabie. He is a renowned font designer who has made hundreds of interesting TrueType fonts freely available for use on the web. His fonts are elegant, decorative, and playful.

ÔÚ¾ÙÀýÖÐÓõ½µÄTrueType ÎļþÊÇÓÉRay LarabieÉè¼ÆµÄ¡£ ËûÊÇÓÐÃûµÄ×ÖÌåÉè¼ÆÊ¦£¬Ôø¾­ÖÆ×÷ÁËÊý°Ù¸öÓÐȤµÄTrueType×ÖÌå¿ÉÒÔÖ±½ÓÓÃÔÚWEBÉÏ¡£ËûµÄ×ÖÌå¶Ëׯ£¬×°ÊÎÐÔÇ¿£¬¶øÇÒ»¹·Ç³£ºÃÍæ¡£

Dieter Steffmann is another great font designer. He, too, has made many beautiful fonts available for anyone to use. Below are some examples that use his fonts:

Dieter Steffmann ÊÇÁíÍâÒ»Ãûΰ´óµÄ×ÖÌåÉè¼ÆÊ¦¡£ËûͬÑùÒ²ÖÆ×÷³öÁËÐí¶àƯÁÁµÄ×ÖÌ壬Ìṩ¸øËùÓÐÈËʹÓá£ÏÂÃæ¾ÍÊÇһЩʹÓÃËûµÄ×ÖÌåµÄ¾ÙÀý£º

Screenshot of web page using real TrueType fonts. PDF (via Prince). HTML (via your browser).

Screenshot of web page using real TrueType fonts. PDF (via Prince). HTML (via your browser).

Screenshot of web page using real TrueType fonts. PDF (via Prince). HTML (via your browser).

Getting more complicated
¸ü¸´ÔÓµÄ

When browsers start supporting web fonts, designers will be tempted to adjust more properties than just font-family. In the examples shown above, line-height, letter-spacing, word-spacing, and absolute positioning have been tweaked. This adjustment of other properties works well when the specified fonts are fetched from the web. However, it may have unexpected results in browsers that don’t support web fonts, and web designers should always be make sure their pages degrade gracefully.

µ±ä¯ÀÀÆ÷¿ªÊ¼Ö§³ÖWEB×ÖÌ壬Éè¼ÆÕß½«±»ÎüÒýµ½µ÷Õû¸ü¶àµÄÊôÐÔ¶ø²»½ö½öÊÇ×ÖÌåϵÁС£ÔÚÉÏÃæËùչʾµÄ¾ÙÀýÖУ¬Ðиߣ¬×Öĸ¼ä¾à£¬´Ê»ã¼ä¾à£¬ÒÔ¼°¾ø¶Ô¶¨Î»¶¼ÒѾ­×öºÃµ÷Õû¡£ÕâÖÖµ÷Õû,´ÓWEBÉÏÖ¸¶¨×ÖÌå¿ÉÒÔÈÃÆäËûÊôÐÔÔË×÷Á¼ºÃ¡£µ«ÊǶÔÓÚһЩ²»ÄÜÖ§³ÖWEB ×ÖÌåµÄä¯ÀÀÆ÷£¬Éè¼ÆÕßÓ¦¸ÃÈ·±£ËûÃǵÄÒ³Ãæ¿ÉÒÔ¼æÈÝ¡£

The most obvious way to ensure graceful degradation is to specify a list of similar font families. For example, Ray Larabie’s “Primer Apples” is a charming, stitched font. You can see it used in this example:

Òª±£Ö¤ÕâÖÖ¼æÈÝÐÔºÜÃ÷ÏԵķ½·¨¾ÍÊÇʹÓÃÏàËÆµÄ×ÖÌåϵÁС£¾ÙÀýÀ´Ëµ£¬Ray LarabieµÄ“Primer Apples” ÊÇÖÖÃÔÈ˵ķìºÏ×ÖÌ壬Äã¿ÉÒÔÔÚÕâ¸ö¾ÙÀýÖп´µ½ËüµÄ±íÏÖ£º

Screenshot of web page using real TrueType fonts. PDF (via Prince). HTML (via your browser).

“Primer Apples” has no obvious replacement in the list of fonts normally used on the web. However, the font metrics are quite similar to the ubiquitous “Trebuchet” font which can be specified as an alternative (Line wraps marked » —Ed.):

font-family: "Primer Apples", "Trebuchet MS", »
sans-serif;

In a legacy browser without support for web fonts (or multi-column layouts or rounded borders), the second example will look like this:

ÔÚһЩ¹Å¶­ä¯ÀÀÆ÷ÉÏ£¬Åöµ½²»Ö§³ÖµÄWEB×ÖÌ壨»òÊǶàÁв¼¾Ö»òÊÇÔ²½Ç±ß¿ò£©£¬¿´ÉÏÈ¥½«»áÏñµÚ¶þ¸ö¾ÙÀýÒ»Ñù£º

Screenshot of web page without support for web fonts.

The stitching is gone, but the degradation is graceful.

·ìºÏЧ¹ûûÁË£¬µ«¿´ÉÏ»¹ÊÇÓÅÃÀµÄ¡£

A proposal to the W3C: a media query for web fonts
¸øW3CÒ»¸ö½¨Ò飺Õë¶ÔWEB×ÖÌåŪ¸öýÌå²éѯ¡£

To further limit degradation, it would be useful to have a media query for web fonts. Consider this example:

¸üÉî²ã´ÎµÄ½µ¼¶ÏÞÖÆ£¬Ã½ÌåµÄ²éѯ¶ÔÓÚWEB×ÖÌåÀ´ËµÓ¦¸ÃÊÇÓÐÓô¦µÄ¡£Ë¼¿¼Ò»ÏÂÕâ¸ö¾ÙÀý£º

h1 { 
font-family: "Trebuchet MS", sans-serif;
letter-spacing: 0.1em;
}

@media all and (web-fonts: "truetype") {
h1 {
font-family: "Primer Apples", sans-serif;
letter-spacing: 0.2em;
}}


ºóÃæÊÇһЩ×÷ÕßµÄÉèÏ룬¾Í²»·­ÒëÁË£¬´ó¼ÒÓÐÐËȤ¿ÉÒԲο¼Ô­ÎÄ
µØÖ·£ºhttp://www.alistapart.com/articles/cssatten

 

ÆÀÂÛ (0) All