一、什么是CSS
CSS是层叠样式表(Cascading Style Sheet )的简称是一种标记语言,用于设置页面的外观样式实现结构与样式分离,便于开发维
简单的说:
1、CSS 是一种描述 HTML 文档样式的语言。
2、CSS 描述应该如何显示 HTML 元素。
二、CSS 的属性
字体属性:(font)
大小 font-size:x-large;(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX、PD
样式 font-style:oblique;(偏斜体) italic;(斜体) normal;(正常)
行高 line-height:normal;(正常) 单位:PX、PD、EM
粗细 font-weight:bold;(粗体) lighter;(细体) normal;(正常)
变体font-variant: small-caps;(小型大写字母) normal;(正常)
大小写text-transform: capitalize;(首字母大写) uppercase;(大写) lowercase;(小写) none;(无)
修饰text-decoration: underline;(下划线) overline;(上划线) line-through;(删除线) blink;(闪烁)
常用字体:(font-family)
"Courier New", Courier, monospace, "Times New Roman",Times, serif, Arial, Helvetica, sans-serif, Verdana
背景属性: (background)
色彩background-color:#FFFFFF; 图片background-image:url(); 重复background-repeat:no-repeat; 滚动background-attachment:fixed;(固定) scroll;(滚动) 位置background-position:left(水平) top(垂直); 简写方法 background:#000url(..) repeat fixed left top;
区块属性: (Block)
字间距letter-spacing:normal; 数值
对刘text-align:justify;(两端对齐) left;(左对齐) right;(右对齐) center;(居中)
缩进text-indent: 数值px;
垂直对齐vertical-align:baseline;(基线) sub;(下标) super;(下标) top; text-top; middle;bottom; text-bottom;
词间距word-spacing:normal; 数值
空格white-space:pre;(保留) nowrap;(不换行)
显示display:block;(块) inline;(内嵌) list-item;(列表项) run-in;(追加部分) compact;(紧凑) marker;(标记) table; inline-table;table-raw-group; table-header-group; table-footer-group; table-raw; table-column-group;table-column; table-cell; table-caption;(表格标题)
方框属性: (Box)
width:; height:; float:; clear:both; margin:; padding:; 顺序:上右下左
列表属性: (List-style)
类型list-style-type: disc;(圆点) circle;(圆圈) square;(方块) decimal;(数字) lower-roman;(小罗码数字) upper-roman;lower-alpha; upper-alpha;
位置list-style-position:outside;(外) inside;
图像list-style-image:url(..);
定位属性: (Position)
Position: absolute; relative; static;
visibility: inherit; visible; hidden;
overflow: visible; hidden; scroll; auto;
clip: rect(12px,auto,12px,auto) (裁切)
三、边距
内边距:Padding(内边距) - 清除内容周围的区域,内边距是透明的。
padding-bottom 设置元素的底部填充
padding-left 设置元素的左部填充
padding-right 设置元素的右部填充
padding-top 设置元素的顶部填充
外边距:margin 简写属性。在一个声明中设置所有外边距属性。
margin-bottom 设置元素的下外边距。
margin-left 设置元素的左外边距。
margin-right 设置元素的右外边距。
margin-top 设置元素的上外边距。
四、边框
border-top : 1px solid #6699cc; /\*上框线\*/
border-bottom : 1px solid #6699cc; /\*下框线\*/
border-left : 1px solid #6699cc; /\*左框线\*/
border-right : 1px solid #6699cc; /\*右框线\*/
以上是建议书写方式,但也可以使用常规的方式如下:
border-top-color : #369 /\*设置上框线top颜色\*/
border-top-width :1px /\*设置上框线top宽度\*/
border-top-style : solid/\*设置上框线top样式\*/
其他框线样式
solid /\*实线框\*/
dotted /\*虚线框\*/
double /\*双线框\*/
groove /\*立体内凸框\*/
ridge /\*立体浮雕框\*/
inset /\*凹框\*/
outset /\*凸框\*/
五、如何添加 CSS
当浏览器读到样式表时,它将根据样式表中的信息来格式化 HTML 文档。
三种使用 CSS 的方法
有三种插入样式表的方法:
外部 CSS
内部 CSS
行内 CSS
通过使用外部样式表,您只需修改一个文件即可改变整个网站的外观!
每张 HTML 页面必须在 head 部分的 <link> 元素内包含对外部样式表文件的引用。
实例
外部样式在 HTML 页面 <head> 部分内的 <link> 元素中进行定义:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
内部 CSS
如果一张 HTML 页面拥有唯一的样式,那么可以使用内部样式表
内部样式是在 head 部分的 <style> 元素中进行定义。
实例
内部样式在 HTML 页面的 <head> 部分内的 <style> 元素中进行定义:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
行内 CSS
行内样式(也称内联样式)可用于为单个元素应用唯一的样式。
如需使用行内样式,请将 style 属性添加到相关元素。style 属性可包含任何 CSS 属性。
实例
行内样式在相关元素的 "style" 属性中定义:
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
多个样式表
如果在不同样式表中为同一选择器(元素)定义了一些属性,则将使用最后读取的样式表中的值。
假设某个外部样式表为 <h1> 元素设定的如下样式:
h1 {
color: navy;
}
然后,假设某个内部样式表也为 <h1> 元素设置了如下样式:
h1 {
color: orange;
}
实例
如果内部样式是在链接到外部样式表之后定义的,则 <h1> 元素将是橙色:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
层叠顺序
当为某个 HTML 元素指定了多个样式时,会使用哪种样式呢?
页面中的所有样式将按照以下规则“层叠”为新的“虚拟”样式表,其中第一优先级最高:
- 行内样式(在 HTML 元素中)
- 外部和内部样式表(在 head 部分)
- 浏览器默认样式]
六、CSS 伪类
什么是伪类?
伪类用于定义元素的特殊状态。
伪类和 CSS 类
伪类可以与 CSS 类结合使用:
当您将鼠标悬停在例子中的链接上时,它会改变颜色:
实例
a.highlight:hover {
color: #ff0000;
}
什么是伪元素?
CSS 伪元素用于设置元素指定部分的样式。
例如,它可用于:
- 设置元素的首字母、首行的样式
- 在元素的内容之前或之后插入内容
语法
伪元素的语法:
selector::pseudo-element {
property: value;
}
first-line 伪元素
first-line
伪元素用于向文本的首行添加特殊样式。
下面的例子为所有 <p> 元素中的首行添加样式:
实例
p::first-line {
color: #ff0000;
font-variant: small-caps;
}