CSS(二)
發佈於 2018-06-24
这一篇讲解在 CSS 中与字体和文本相关的属性。
font-family 定义了页面所使用的字体。
font-family: "Source Sans Pro", "Arial", sans-serif; |
---|
当使用多个值时,font-family 属性值字体列表定义浏览器应选择字体系列的优先级。 浏览器将在用户的计算机或者任何 @font-face 资源中查找每个字体。 所使用的字体优先级按从左到右的排序: 如果可用,它将使用第一个值,否则跳到下一个值,直到最后。默认字体系列由浏览器首选项定义。 在上面的示例中,浏览器将首先尝试使用 Source Sans Pro。如果不可用,它会尝试使用 Arial。如果也不可用,它将使用浏览器的 sans-serif 字体族中的可用字体。
font-family: serif; |
---|
font-family: sans-serif; |
---|
font-family: monospace; |
---|
font-family: cursive; |
---|
font-family: fantasy; |
---|
font-size 定义了页面字体尺寸,浏览器默认使用字体尺寸为 medium 关键字。
font-size: 20px; |
---|
font-size: 1.2em; |
---|
font-size: 1.2rem; |
---|
font-size: 90%; |
---|
font-size: smaller; |
---|
font-size: x-large; |
---|
font-style 定义字体样式。
font-style: normal; |
---|
还有两个之可选:
均为斜体。
font-weight 定义字重。
font-weight: bold; |
---|
font-weight: 600; |
---|
浏览器会选择下一个可用字重。
font-weight: bolder; |
---|
font-variant 定义了所使用的字形,normal 为浏览器默认取值。
font-variant: normal; |
---|
还有一个值可选:
注意 font 所有属性都是可继承的属性。
font: bold italic 18px Simsun,sans-serif; |
---|
font简写属性要求:
color 属性定义元素前景色。(边框,文本等)
color: transparent; |
---|
color: red; |
---|
color: #05ffb0; |
---|
color: rgb(50, 115, 220); |
---|
color: rgba(0, 0, 0, 0.5); |
---|
color: hsl(14, 100%, 53%); |
---|
color: hsla(14, 100%, 53%, 0.6); |
---|
text-indent 属性定义文本缩进。 可以使用 pixel,em,rem,percentage 等。 可以使用负值。
text-indent: 2em; |
---|
letter-spacing 属性定义字符间距,normal 为浏览器默认取值。
letter-spacing: normal; |
---|
letter-spacing: 2px; |
---|
letter-spacing: 0.1em; |
---|
word-spacing 属性定义单词间距,0px 为浏览器默认取值。
word-spacing: normal; |
---|
word-spacing: 5px; |
---|
word-spacing: 2em; |
---|
line-height 属性定义单行文本高度。
line-height: normal; |
---|
line-height: 1.6; |
---|
line-height: 30px; |
---|
line-height: 0.8em; |
---|
单行文本垂直居中只需要设置 height 和 line-height 相同即可。 注意: line-height 属性也可以放到 font 简写属性里,写法为在 font-size 后加一个 / ,再跟 line-height 的值。
font: bold italic 18px/1.5 Simsun,sans-serif; |
---|
text-decoration 属性定义文本装饰线样式。
text-decoration: none; |
---|
text-decoration: underline; |
---|
text-decoration: line-through; |
---|
text-decoration: overline; |
---|
text-overflow 属性定义文本溢出时的行为。
text-overflow: clip; |
---|
text-overflow: ellipsis; |
---|
text-transform 属性定义文本内容变换。
text-transform: none; |
---|
text-transform: capitalize; |
---|
text-transform: uppercase; |
---|
text-transform: lowercase; |
---|
text-align 属性作用于块级元素,控制其内部行内内容(如文本或行内元素等)对齐方式。是可继承属性。
text-align: left; |
---|
text-align: right; |
---|
text-align: center; |
---|
text-align: justify; |
---|
justify 只对多行文本有效,且多行文本的最后一行无效。要想对最后一行有效需要使用 text-align-last: justify; 或使用伪元素::after 把最后一行变为非最后一行
vertical-align 属性定义了一个行内元素如何垂直对齐。
vertical-align: baseline; |
---|
vertical-align: sub; |
---|
vertical-align: super; |
---|
vertical-align: text-top; |
---|
vertical-align: text-bottom; |
---|
vertical-align: middle; |
---|
vertical-align: top; |
---|
vertical-align: bottom; |
---|