Cascading Style Sheet 级联样式表
表现HTML或XHTML文件样式的计算机语言
包括对字体、颜色、边距、高度、背景图片、网页定位等设定
语法:
选择器{
声明1;
声明2;
}
最后一条声明后的“;”可写可不写。
style标签:
<h1 style="color:red;">style属性的应用h1>
<p style="font-size:14px;color:green;">直接在HTML标签中设置的样式p>
<style>
h1{
color:green;
}
style>
优缺点:
CSS代码保存在扩展名为.css的样式表中
HTML文件引用扩展名为.css的样式表,有两种方式:链接式,导入式。
语法:
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
文件路径 使用外部样式表 文件类型
语法:
<head>
<style type="text/css">
@import url("style.css");
style>
head>
HTML标签直接作为标签选择器的名称
<style>
.class{}
style>
<标签名 class="自定义名称">标签名>
<style>
#id{}
style>
<标签名 id="自定义名称">标签名>
*{
}
选择所有元素
<style>
.borther,#aa,a{
color: blueviolet;
font-size:25px;
}
style>
<body>
<ul class="borther">
<li id="aa"><a>大娃a>li>
<li>二娃li>
<li>三娃li>
ul>
<a>我是一个虚假连接a>
<ul>
<li>孙悟空li>
<li>猪八戒li>
<li>沙悟净li>
<li>唐三藏li>
ul>
body>
| 选择器 | 类型 | 功能描述 |
|---|---|---|
| EF | 后代选择器 | 选择匹配的F元素,且匹配的F元素被包含在匹配的E元素内 |
| E>F | 子选择器 | 选择匹配的F元素,且匹配的F元素是匹配的E元素的子元素 |
| E+F | 相邻兄弟选择器 | 选择匹配的F元素,且匹配的F元素紧位于匹配的E元素后面 |
| E~F | 通用兄弟选择器 | 选择匹配的F元素,且位于匹配的E元素后的所有匹配的F元素 |
body p{
}
实例
<style>
ul li a{
color: red;
}
style>
<body>
<ul>
<li><a>大娃a>li>
<li>二娃li>
<li>三娃li>
ul>
<ul>
<li>孙悟空li>
<li>猪八戒li>
<li>沙悟净li>
<li>唐三藏li>
ul>
body>
后代选择器两个选择符之间必须要以空格隔开,中间不能有任何其他的符号插入
body>p{
}
<style>
ul>li{
font-size: 30px;
}
li>a{
font-size: 30px;
}
style>
<body>
<ul>
<li><a>大娃a>li>
<li>二娃li>
<li>三娃li>
ul>
<ul>
<li>孙悟空li>
<li>猪八戒li>
<li>沙悟净li>
<li>唐三藏li>
ul>
body>
.active+p{
}

【同级ul不会改变:】

<style>
#aa~li{
color: blue;
}
.borther~ul{
font-size: 20px;
}
a~ul{
color: red;
}
style>
<body>
<ul class="borther">
<li id="aa"><a>大娃a>li>
<li>二娃li>
ul>
<a>我是一个虚假连接a>
<ul>
<li>孙悟空li>
ul>
<ul>
<li>1li>
<li>2li>
ul>
body>
| 选择器 | 功能描述 |
|---|---|
| E:first-child | 作为父元素的第一个子元素的元素E,此标签必须在第一行 |
| E:last-child | 作为父元素的最后一个子元素的元素E |
| E F:nth-child(n) | 选择父级元素E的第n个子元素F,(可以是1,2,3),关键字为even、odd |
| E:first-of-type | 选择父元素内具有指定类型的第一个E元素 |
| E:last-of-type | 选择父元素内具有指定类型的最后一个E元素 |
| E F:nth-of-type(n) | 选择父元素内具有指定类型的第n个F元素 |
E F:nth-child(n)在父级里从一个元素开始查找,不分类型
E F:nth-of-type(n)在父级里先看类型,再看位置
| 属性选择器 | 功能描述 |
|---|---|
| E[attr] | 选择匹配具有属性attr的E元素 |
| E[attr=val] | 选择匹配具有属性attr的E元素,并且属性值为val**(val区分大小写)** |
| E[attr^=val] | 选择匹配元素E,且E元素定义了属性attr,其属性值以val开头的任意字符串 |
| E[attr$=val] | 选择匹配元素E,且E元素定义了属性attr,其属性值以val结尾的任意字符串 |
| E[attr=val]* | 选择匹配元素E,且E元素定义了属性attr,其属性值包含了"val"(字符串val与属性值中的任意位置相匹配) |
示例:
<style>
p[id]{
background: yellow;
}
p[id=first]{
background: red;
}
p[class*=links]{
background: greenyellow;
}
p[href^=http]{
background: blueviolet;
}
p[href$=png]{
background: goldenrod;
}
style>
<body>
<p class="demo">
<p class="links item" id="first">段落一p>
<p href="http://xxx1" class="links item">段落二p>
<p href="http://xxx2.png" class="links item">段落三p>
<p class="links item">段落四p>
<p class="links item">段落五p>
<p class="links item" id="last">段落六p>
p>
body>
<body>
<p class="demo">
<p class="links item" id="first">段落一p>
<p href="http://xxx1" class="links item">段落二p>
<p href="http://xxx2.png" class="links item">段落三p>
<p class="links item">段落四p>
<p class="links item">段落五p>
<p class="links item" id="last">段落六p>
p>
body>