Expanding your vocabulary
总述
在上一章节介绍了 CSS 的基础之后,这一章节更加具体地展开关于 CSS 的各种 rules 能够达成的效果。比如
- 字体:font-family
- 字号:font-size
- 字重:font-weight
- 斜体字:font-style:italic/oblique
- 字体的颜色:color
- 背景的颜色:background-dcolor
- 下划线:textdecoration:underline
font-family
在 CSS 中,有5个font-family,而每个 font-family 中包含着很多类型的 font,它们的名字和例子如下:
- Serif family:Times New Roman, Georgia
- San-serif family:Arial,Geneva
- Monospace family:Courier
- Cursive family:Comic Sans
- Fantasy family
他们各自的特点是:
- Serif family:font with serifs
- San-serif family:font without serifs
- Monospace family:fonts that have constant-width characters
- Cursive family:look handwritten
- Fantasy family:contains stylized decorative fonts
对于不同的 font-family,有着对人不同感觉:
Take a good look at the font families:serif fonts have an elegant, traditional look, while sans-serif fonts have a very clean and readable look. Monospace fonts feel like they were typed on a typewriter. Cursive and fantasy fonts have a playful or stylized feel.
设置 font-family specification
当设置 font-family 的 value 的时候,可以这样写:
需要明白以下几点:
- 前面三个字体都是具体字体的名称,这里的大小写是有区别的
- 是否显示相应的字体与对应的浏览器有关,如果浏览器没有 find 到相应的字体,那么寻找下一款字体
- 如果前面的都没有找到,那么就应用浏览器 default 的 sans-serif 字体(sans-serif 并不是一种特定的字体,而是浏览器默认的字体)
- 如果字体的名字是分开的形式,比如 Courier New,应该在 value 中写成"Courier New"
- 在选择字体的时候,最好选择 Windows 和 Mac 系统都支持的字体,它们有:
•Andale Mono
•Arial
•Comic sans
•Courier New
•Georgia
•Impact
•Time New Roman
•Trebuchet MS
•Verdana
Using Web Font
由于系统和浏览器的原因,无法使得每一个网页的 visitor 都可以看到 CSS 中设置的字体,这时候,就可以使用 Web Font 来达成这一目标
How Web fonts Work
- 你储存在 Web Server 对应的 ".woof"格式的文件
- Browser 检测到网页使用了 Web Font,请求 Web Server 用来 retrive 相应的 Web Font
- Web Server 返回相应的字体文件,browser 显示相应的字体的文本。
How to use Web Font to your page
- Find a font:visit sites to find free and licensed font you can use in your websites
- Make sure you have all the format of the font you need
- Place your font files on the Web:You can use font services online , then you'll use the URL of your font files.
- Add the @font-face rule to your CSS:You should assign a name for the font and then note down the src by using URL
- Using the family name in your CSS
这里需要明白和注意几点:
字体的格式:
- Truetype:.ttf
- Opentype:.otf Opentype is on top of Truetype
- Embedded Opentype fonts:.eot supported only on IE
- SVG font:.svg short for Scable Vector Graphics
- Web open font format:.woff Base on Truetype and is developed as a standard Web Font.
关于@font-face :
@font-face 其实是一个 built-in CSS rule(还有其他,比如:@media)这个 rule 与普通的含 selector 的rule 不同,它的语法为:
调节 font-size 的三种方式:
有三种方式去调节字体的大小,分别是:
- px:font-size:14px
- %:font-size:120%
- em:font-size:1.2em
需要注意的是: - px方式:其中的14与 px 之间不要有空格
- %方式和em方式都可以达到相同的效果
- 在使用的时候可以根据 inheritance 来系统地调节字体的大小
Specify 颜色的三种方式
电脑屏幕的颜色显示模式为 RGB 模式,所以显示不同颜色的思路是通过 R,G,B 三种色彩的占比来显示,可以采用:
- By name :CSS 预定义了150种颜色的名字,如 red,blue,yellow 等等, basic color 有16种,可以直接用这些名字:
- By red,green and blue values:可以采用
background-color:rgb(80%,40%,0%)
这样的占比的方式,也可以采用数值的方式background-color:rgb(204,102,0)
- By hex code:比如#cc6600
其他知识
- 一个寻找 Web fonts 的网站:https://www.fontsquirrel.com
- 一个用于搜查颜色的网站:https://en.wikipedia.org/wiki/Web_colors