CSS,全称Cascading Style Sheets,中文翻译为“层叠样式表”。
一、样式表的引入
- 嵌入式:在<head></head>标签内
<style type="text/css"> CSS样式规则 </style>
- 链接式:在<head></head>标签内
<link href="main.css" rel="Stylesheet">
- 引入式:在<head></head>标签内
<style type="text/css"> @import url(main.css); </style>
二、CSS样式规则
类别选择符
p.bold { font-weight:bold; }
<p class="bold">这个段落是粗体</p>
ID选择符
p#bold { font-weight:bold; }
<p id="bold">这个段落是粗体</p>
群选择符
可以同时为多个选择符应用同一套规则,每个选择符之间用逗号隔开。p, h1, h2{ text-align: left; }
还可以在群选择符中掺杂ID选择符或类别选择符
p.navigation, h1#title{ font-weight: bold; }
Color属性
p { color: red; }
5种定义颜色的方法 Property Value Description color: <color keyword> 设置颜色为给定的值,如red color: #rrggbb 用HTML的16进制表示,如#FF00BB color: rgb(rrr,ggg,bbb) 通过RGB三种颜色的数值(0到255),如rgb(100,200,255) color: rgb(rrr%,ggg%,bbb%) 通过RGB三种颜色的百分比指定,如rgb(0%,100%,0% ) color: inherit 继承父值 背景颜色属性
p { background-color: red; }
5种定义背景颜色的方法 Property Value Description background-color: <color keyword> 设置颜色为给定的值,如red background-color: #rrggbb 用HTML的16进制表示,如#FF00BB background-color: rgb(rrr,ggg,bbb) 通过RGB三种颜色的数值(0到255),如rgb(100,200,255) background-color: rgb(rrr%,ggg%,bbb%) 通过RGB三种颜色的百分比指定,如rgb(0%,100%,0% ) background-color: inherit 继承父值 布局
边框
(1) 边框宽度
可以用关键字来指定边框宽度
可以用具体的数值来指定
边框宽度规则 Property Value Notes border-width: thin border-width: medium 默认边框宽度 border-width: thick border-width: <length> 用具体数值来指定 border-top-width: <length> 设置上边框 border-right-width: <length> 设置右边框 border-bottom-width: <length> 设置下边框 border-left-width: <length> 设置左边框
(2) 边框颜色
边框颜色规则 Property Value Type Example Result border-color: color name red 关键字设定边框颜色 border-color: hex value #FFFFFF 指定边框颜色 border-color: rgb() value rgb(10%, 80%, 0) 指定边框颜色
(3) 边框风格
边界距
边界距属性是用来指定两个元素边框之间的距离
边界距规则 Property Value Notes margin-top: <length> 设置上边界距 margin-right: <length> 设置右边界距 margin-bottom: <length> 设置下边界距 margin-left: <length> 设置左边界距 margin: <top> <right> <bottom> <left> 设置每个边界距
填充属性
填充属性用来控制元素边框内空间的大小
填充属性规则 Property Value Notes padding-top: <length> 设置上填充 padding-right: <length> 设置右填充 padding-bottom: <length> 设置下填充 padding-left: <length> 设置左填充 padding: <top> <right> <bottom> <left> 设置每个填充
背景图片
在CSS中,可以对任何元素指定背景图片body { background-image: url("..."); }
Property
Value Notes background-image: url("<location>") 通过网址来指定背景图片 background-image: none 无背景图片 background-image: inherit 继承父值
背景重复
通过 background-repeat 属性来指定背景的重复方式
背景重复规则 Property Value Notes background-repeat: repeat 重复背景 background-repeat: repeat-x 仅平行重复 background-repeat: repeat-y 仅垂直重复 background-repeat: no-repeat 不重复 background-repeat: inherit 继承父值
背景附着
通过 background-attachment 属性控制背景是否随网页滚动
背景附着规则 Property Value Notes background-attachment: scroll 背景随着滚动 background-attachment: fixed 背景固定 background-attachment: inherit 继承父值
背景定位
此属性用来指定背景图片在网页的展示位置
背景定位规则 Property Value Notes background-position: <x> <y> 用具体数值或百分比指定 background-position: [top \ center \ botton]
[left \ center \ right]用关键字定位
字体属性
p { font-family: Verdana }
说明:当 font-family 的属性值为多个值时,浏览器会先去找第一种字体,若没有第一种字体,则找第二种字体,以此类推。
字体规则 Property Value font-family: Verdana font-family: Times font-family: Georgia font-family: Arial, Verdana, sans-serif font-family: Times New Roman, serif font-family: I Don't Have This Font, sans-serif
字体风格属性
此属性用来控制字体是否斜体
p { font-style: italic; }
Property Value Notes font-style: oblique 斜体 font-style: italic 斜体 font-style: normal 正常
字体的粗细属性
此属性用来设置字体的粗细程度
p { font-weight: bold; }
Property Value font-weight: normal font-weight: bold font-weight: bolder font-weight: lighter font-weight: <100 - 900>
字体大小属性
设置字体的大小(绝对和相对)
p { font-size: 12pt; }
Property Value Unit Type font-size: 12pt Absolute font-size: 12px Absolute font-size: medium Absolute font-size: smaller Relative font-size: normal Relative font-size: larger Relative font-size: 50% Relative
行间距属性
此属性用来指定行与行之间的垂直间距。
p { line-height: 14pt; }
字体变量属性
font-variant 用来控制小写变大写
Property Value font-variant: normal font-variant: small-caps
设置 font-variant:small-caps 属性后变为
文本缩进
通过此属性可以在CSS中对每个段落的首行缩进。
Property Value Description text-indent: <length> 根据具体数值对段落首行进行缩进 text-indent: <percentage> 根据百分比缩进
文本对齐属性
CSS支持水平的对齐和适当的文本。
Property Value Description text-align: left 文本左对齐 text-align: right 文本右对齐 text-align: center 文本居中对齐 text-align: justify 文本两端对齐
文本装饰
在CSS中,文本装饰效果涉及到上划线,下划线,删除线等对文本的一些装饰效果。
Property Value Example text-decoration: none text-decoration: underline text-decoration: overline text-decoration: line-through
文本变形
文本变形是指大小写转换首字母大写等属性。
Property Value Description text-transform: none 不变 text-transform: capitalize 首字母大写 text-transform: uppercase 全部变大写 text-transform: lowercase 全部变小写
链接与化类别
用CSS的 :link 化类别用来指定没有访问过链接的属性。
Pseudo-Class Example Use Description :link a:link { ... } 设置未访问链接属性 :visited a:visited { ... } 设置访问过链接属性 :active a:active { ... } 鼠标点击与释放之间时 :hover a:hover { ... } 鼠标悬停时
整理来源:http://www.caopeng.org/css/
CSS基础知识小结
最新推荐文章于 2022-04-29 10:45:43 发布