网页设计教程:CSS文字排版技巧大全


译自:The Ultimate Guide to CSS Typography
中文:CSS文字排版终极指南
请尊重版权,转载请注明来源!多谢。

在今天的设计中,排版常常被忽视,特别是被网页设计师忽视。这真是件遗憾的事情因为CSS可以做很多事情来控制我们的排版。也就是说,我们被局限于某些“网络安全”排版,但是我们不应该限制我们自己的创造性。本文整理了一些用于在网页上排版的CSS技巧。

Font属性

CSS提供了font属性,它允许我们在我们的页面中调整文字。这是一系列的关于语法和如何使用CSS调整文字的概述。

Font-size

使用font-size可以修改你的文字的大小。

1
2
3
font-size: 1.2em;
font-size: 12px;
font-size: 10pt;
预览
  1. 文字大小为1.2em.
  2. 文字大小为12px.
  3. 文字大小为10pt.

 

Font-weight

该属性用来改变你的文字的粗细(比如bold, bolder)

1
2
3
4
font-weight: normal;
font-weight: bold;
font-weight: bolder;
font-weight: lighter;
预览
  1. font-weight设置为normal
  2. font-weight设置为bold
  3. font-weight设置为bolder
  4. font-weight设置为lighter

font-weight属性还可以使用100,200,300,400,500,600,700,800,900等数字值,数字越大,文字越粗。400等效于normal,700等效于bold。

Text-transform

text-transform属性允许你应用uppercase(大写), lowercase(小写), 以及capitalize(首字母大写)等效果到你的文字。当然,该属性不能用于中文。

1
2
3
4
text-transform: capitalize;
text-transform: uppercase;
text-transform: lowercase;
text-transform: inherit;
预览
  1. Capitalizes the first letter in every word
  2. Allows your html to be lower case the converts it all to uppercase characters
  3. ALLOWS YOUR HTML TO BE UPPERCASE THEN CONVERTS IT TO LOWERCASE, THIS WAS TYPED IN ALLCAPS
  4. inherits the text-transform property from its parent element

Text-decoration

text-transform属性允许你使用一些文字修饰,比如下划线(underline)、上划线(overline)、删除线(line-through)以及文字闪动(blink)。

1
2
3
4
5
text-decoration: normal;
text-decoration: underline;
text-decoration: overline;
text-decoration: line-through;
text-decoration: blink;
预览
  1. text-decoration设置为normal
  2. text-decoration设置为underline
  3. text-decoration设置为overline
  4. text-decoration设置为line-through
  5. text-decoration设置为blink (只有Firefox 和Opera 支持)

Font-Variant

font-variant属性允许你制作小型大写字母效果,也就是将小写字母变成大写并减小字体型号

1
2
3
font-variant: normal;
font-variant: small-caps;
font-variant: inherit;
预览
  1. Font Variant 设置为 normal
  2. Font Variant 设置为 small-caps
  3. Font variant 设置为 inherit

Letter-Spacing 和 word-spacing

这两个属性都用来添加他们对应的元素中的空白。letter-spacing添加字母之间的空白,而word-spacing添加每个单词之间的空白。请注意,word-spacing对中文无效。

1
2
3
letter-spacing: normal;
letter-spacing: 2px;
letter-spacing: inherit;
预览
  1. Letter spacing 设置为 normal
  2. Letter spacing 设置为 2px
  3. Letter spacing设置为 inherit

font缩写

上面的font属性可以缩写,这样可以大大的提高你的css的效率。

1
font:font-style font-variant font-weight font-size[/line-height] font-family

请注意上面的缩写的书写顺序。另外,如果这里不设置line-height,元素的line-height将会被设置为默认的1,而不会从父级元素或body元素继承。使用缩写的时候,font-size和font-family是必须的,其它几项可根据情况不写。

制作有吸引力的段落

对于更有吸引力的段落排版,可以采用Robert Bringhurst的方法,它约定:用你的文字大小乘以30就可以得到段落的宽度。

比如如果你的文字大小是12px,用30乘以它,也就是360px,这样每行大约可以呈现65个英文字符。

不正确的段落大小:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

正确的段落大小

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

Line-height示例

行高用来限定每行文字之间的空白大小。一个经验之谈就是让行高比你的字体大6-7px。

比如,如果你的文字大小是12px,加上6px就是你的行高,也就是18px。

不正确的Line-Height

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

正确的Line-Height

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

 

清晰的强调文字

下划线应该只用于文字链接,而斜体是一个更清晰的可选方案.

不正确的例子

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

正确的例子

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

first-letter与first-line

顾名思义,first-letter就是第一个字母,first-line是第一行文字,它们是少有的被所有主流浏览器支持的CSS 2.1 伪元素,你可以为它们定义任意样式。

示例代码
1
2
3
#demo5{ width:400px;}
#demo5:first-letter{ font-size:32px;color:green}
#demo5:first-line{color:red;}
前端观察 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

前瞻 – CSS3

已经有一部分很棒的CSS3特性可以用到你的设计中了。当然这些中的一部分新属性还不能用于IE,但是你或许了解这对IE来说是常事儿,所以你不妨试试。

@Font-face

@font-face允许你上次一个字体文件到你的网站服务器并将其引入到你的外部CSS文件中,然后就可以将它应用到你的网站的任何文字上。

这是处理文字的最大的功能,但是随之而来的是它的负面效果。由于版权问题,只有少数的字体“被允许”使用。这些字体也就是CSS3安全就在这里里面提到的。当然,对于中文字体来说,除了版权问题,还有网速的问题,一般中文字体动辄几MB的大小,字体文件下载到浏览器就要几分钟,这对于用户来说是不划算的。

使用@font-face

在这个例子中我在我的网站的根目录下放了一个font文件夹,然后将需要用到的字体放入该文件夹。你首先需要声明@font-face 属性以导入你的字体文件,然后使用该字体名称来控制其它元素的字体。

1
2
3
4
5
6
7
/* 声明字体 */
@font-face {
src:(font/diavlo.otf)
}
/* 将改字体应用到一个元素 */
h1 {font-family: diavlo, Arial;
}

 

Text-Shadow

text-shadow属性赶走了用Photoshop为某些元素制作下拉阴影的需求,并给你提供动态的阴影控制。当然,该属性IE并不支持。

示例标题

1
2
3
4
5
6
p {
font-size: 2em;
font-weight: bold;
color: #777;
text-shadow: 1px 1px 1px #222;
}

示例标题2

1
p {text-shadow: 2px 2px 2px #222;}

示例标题3

1
p {text-shadow: 1px 1px 5px #FFF;}

PS:呃,其实前端观察的文章标题和章节的二级标题都用了text-shadow,而且还是用了更漂亮的RGBa颜色。

挑战

本文中提到的这些技术都只是一个入门指南。试着采用并实现你喜欢的技术到你的设计中。排版可能是一个网站中最重要的部分,请不要无视它。

PS:本文在原文的基础上,补充了少量内容,比如first-letter和first-line部分——神飞

关于原作者

Shane JeffersThree Styles的创办者和作者。他对设计趋势和教程有独特的喜好。你可以通过这个联系页面和他联系到他或者在twitter上关注他 @ThreeStyles


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3