参考文献: 《HTML, CSS, JavaScript 整合详解》 王津涛 编著,机械工业出版社
1. CSS,Cascading StyleSheets,叠层样式表,将格式从功能中分离出来,定义HTML的显示,CSS有4种类型,优先级由高到低为:行内样式表(Inline Style Sheet),内部样式表(Internal StyleSheet),外部样式表(External Style Sheet),浏览器缺省(Browser Default)
2. CSS语法由选择器(selector)、属性(property)和值(value)三部分组成
selector{ property : value }
注:① 用分号将多个属性隔开,如: p { text-align: center; color: Red; }
② 属性值为多个单词,双引号括起 如: p { font-family: "sansSerif"; }
3. 为提高效率,可以将相同属性和值付给多个选择器,用逗号分隔选择器
如:h1, h2,h3, h4, h5, h6 { color: Blue }
4. 类选择器,可以将同一类型的HTML元素定义出不同样式,类名不能以数字开头,通用类选择器没有标签的局限性,可以用于不同的标签,同一标签可以同时使用多个不同的类选择器
5. ID选择器,ID Selector,用id定义HTML标签样式,用“#”定义,如:
#idone { text-align:center; color:blue }
注:在CSS中id属性值唯一,所以可以省略标签;id名称也不能以数字开头
6. 关联选择器,ContextualSelector,可以为嵌入的其他标签定义样式,如:
h3 em {color:Blue}
<h3>这是一个<em>关联选择器</em></h3>
7. CSS
8. CSS按位置不同,主要分为3类:行内样式表(Inline Style Sheet),内部样式表(Internal Style Sheet),外部样式表(External Style Sheet)
① 行内样式表(Inline Style Sheet),定义在<html>标签中,只对所在标签有效,使用行内样式就失去了样式表的优势,将内容和形式相混淆,一般这个别元素中使用,建议不使用,如:
<p style="font-size:14px; text-decoration:underline;color:Blue">行内样式定义段落,14px,蓝色,下划线</p>
② 内部样式表(Internal Style Sheet),使用<style>标签在head区域内定义样式,只对所在网页有效,如:
<head>
<style type="text/css">
h3 em { color:Blue}
</style>
</head>
③ 外部样式表(External Style Sheet),作为单独的文件存储,文件后缀名“.css”,对样式表的修改,会影响所有引用该样式表的网页,引用方式为在<head>与</head>标签中键入<link href="url" rel="stylesheet" type="text/css" />
④ 多重样式表,如果一些属性被相同的选择器设置成不同的样式,值就会按照样式表的优先级进行继承,即高优先级的样式表会覆盖低优先级的样式表,高优先级不包含、低优先级包含的样式表会被继承下来
9. CSS字体属性,设置文本字体属性,如字体粗细、大小和样式等
字体属性 | 属性值 | 说明 | CSS版本 | 兼容性 |
font | font-style font-variant font-weight font-size/ line-height font-family caption icon menu message-box small-caption status-bar | 复合属性,设置页面元素文本特性 | CSS1/ CSS2 | IE 4 NS 4 F 1 |
font-family | family-name generic-family | 设置字体名称 | CSS1 | IE 4 NS 4 F 1 |
font-size | absolute-size relative-size length | 设置字体大小 | CSS1 | IE 4 NS 4 F 1 |
font-size-adjust | none number | 字体名称序列是否强制使用同一尺寸 | CSS2 | NONE |
font-stretch | normal wider narrower ultra-condensed extra-condensed condensed semi-condensed semi-expanded expanded extra-expanded ultra-expanded | 设置或检索用于对象中文本的文字是否横向拉伸变形 | CSS2 | NONE |
font-style | normal italic oblique | 设置字体样式 | CSS1 | IE 4 NS 4 F 1 |
font-variant | normal small-caps | 设置字体为小型大写字母 | CSS1 | IE 4 NS 6 F 1 |
font-weight | normal bold bolder lighter number | 设置字体粗细 | CSS1 | IE 4 NS 4 F 1 |
IE(Internet Explorer);N(Netscape);F(Firefox)
① font属性,设置网页文本特性,复合属性,可以一次性定义font-style、font-variant、font-weight、font-size、line-height、font-family等属性,语法格式:
a) 选择器 { font : font-style | font-variant |font-weight | font-size | line-height | font-family }
b) 选择器{ font : caption | icon | menu |message-box | small-caption | status-bar }
注:a类声明为复合属性,设置字体的多个属性,参见以下各参数对应属性;b类声明为属性参数,其中:
l caption 定义用在控件上的说明文字(按钮,下拉列表等)
l icon 定义画像标签文字字体
l menu 定义下拉菜单字体
l message-box 信息对话框的文本字体
l small-caption 小控件字体
l status-bar窗口状态栏文字字体
② font-family属性,设置文字字体名称,语法格式:
a) 选择器 { font-family : family-name }
a) 选择器 { font-family : generic-family }
注:a类声明中family-name是字体系列的名称,如:times、courier、arial等;b类声明是字体一般名称,如:serif、sans-serif、cursive、fantasy、monospace等
③ font-size属性,设置字体大小,绝对尺寸、相对尺寸,语法格式:
选择器 { font-size : absolute-size| relative-size | length }
l absolute-size 绝对尺寸,值为xx-small |x-small | small | medium | large | x-large | xx-large
l relative-size 相对尺寸,相对于父级对象中的字体尺寸,以相对长度单位em计算,值为larger | smaller
l length 百分比(浮点数)为长度,不可为负数,百分比取值为父级对象字体尺寸
注:还可以用像素值,不知道应该分在哪个里面~
④ font-style属性,设置字体样式,语法格式:
选择器 { font-style :normal | italic | oblique }
l normal 正常字体
l italic 倾斜字体
l oblique 倾斜字体 (ps:效果上两个似乎一样,区别暂时不懂~)
⑤font-variant属性,设置页面中的字体是否为小型的大写字母,即代码中的小写字母,在页面中以大写显示,语法格式:
选择器 { font-variant :normal | small-caps }
l normal 正常字体
l small-caps 小型的大写字体
<p style="font-variant: normal">font-variant: normal</p>
⑥ font-weight属性,设置字体粗细,语法格式:
选择器 { font-weight : normal| bold | bolder | lighter | number }
normal 正常字体
bold 粗体字符
bolder 更粗的字符
lighter 更细的字符
number 值为100 | 200 | 300 | 400 | 500 | 600 |700 | 800 | 900
10. CSS文本属性,设置文字外观,包括文本颜色、文本书写方向、下划线、文字间距、字母大小写以及文字对齐等
文本属性 | 属性值 | 说明 | CSS版本 | 兼容性 |
color | color | 设置字体颜色 | CSS1 | IE 3 NS 4 F 1 |
direction | ltr trl | 文本书写方向 | CSS2 | IE 6 NS 6 F 1 |
letter-spacing | normal length | 文本字符间距 | CSS1 | IE 4 NS 6 F 1 |
text-align | left right center justify | 文本对齐方式 | CSS1 | IE 4 NS 4 F 1 |
text-decoration | none underline overline line-through bink | 文本装饰,如下划线、闪烁等 | CSS1 | IE 4 NS 4 F 1 |
text-indent | length % | 行首文字缩进 | CSS1 | IE 4 NS 4 F 1 |
text-shadow | none color length | 阴影、模糊效果 | CSS2 | NONE |
text-transform | none capitalize uppercase lowercase | 设置文本大小写 | CSS1 | IE 4 NS 4 F 1 |
unicode-bidi | normal embed bidi-override | 在同一个页面从不同方向显示文本 | CSS2 | IE 5 |
layout-flow | horizontal vertical-ideographic | 文本书写方向 | IE专有 | IE 5 |
line-height | normal length | 文本行高 | CSS1 | IE 4 NS 5 F 1 |
white-space | normal pre nowrap | 空格处理方式 | CSS1 | IE 5 NS 4 F 1 |
word-spacing | normal length | 设置字符间隙 | CSS2 | IE 6 NS 6 F 1 |
IE(Internet Explorer);N(Netscape);F(Firefox)
① color属性,设置文字颜色,语法格式:
选择器 { color : color }
color值可以为颜色名(如<hr color=red>)、十六位进制的颜色值(如<hr color=#FF000)、RGB混合模式(如<hr color=rgb(255,0,0))
② direction属性,设置文本方向,语法格式:
选择器 { direction : ltr| rtl }
l ltr 从左到右
l rtl 从右到左
③ layout-flow属性,设置文本方向,仅适用于IE,语法格式:
选择器 { layout-flow : horizontal| vertical-ideographic }
l horizontal 从左到右,适用于拉丁语
l vertical-ideographic从上到下,从右到左,适用于亚洲语系
④ letter-spacing属性,设置字符间距,范围中英文,语法格式为:
选择器 { letter-spacing : normal | length }
normal 正常间距
length 定义固定的文字间距,可以是绝对单位如(c、mm、in、pt、pc),也可是相对单位(em、ex、px),该长度值可以为负值
⑤ word-spacing属性,设置文本间隔,英文单词之间的间隔,中文无效,语法格式为:
选择器 { word–spacing : normal | length }
⑥ text-align属性,设置文本对齐方式,语法格式为:
选择器 { text–align : left | right | center| justify }
l justify 两端对齐 (貌似无效~)
⑦ text-decoration属性,设置文本装饰,包括下划线、闪烁等,语法格式为:
选择器 { text–decoration : none | underline| bink | overline | line-through }
l none 无装饰
l underline 下划线
l bink 闪烁效果(Firefox可见)
l overline 上划线
l line-through删除线
⑧ text-indent属性,设置文本缩进,语法格式为:
选择器 { text–indent : length | % }
l length 允许负值
l % 百分比设置网页中文本的缩进量
⑨ text-transform属性,设置文本大小写显示,语法格式为:
选择器 { text–transform : none | capitalize| uppercase | lowercase }
l none 普通文字,不改变大小写
l capitalize 将每个单词首字母转换成大写字母
l uppercase 大写显示英文
l lowercase 小写显示英文
⑩ line-height属性,设置文本行高,语法格式为:
选择器 { line–height : normal | length }
l normal 默认行高
l length 百分比或浮点数设置行高,可以为负值
⑪ white-space属性,设置空格处理方式,语法格式为:
选择器 { white–space : normal | pre |nowrap }
l normal 默认处理方式
l pre 不合并字间空白距离,并进行两端对齐
l nowrap 强制文本在一行内显示,直到文本结束或遇到<br/>标签