1. 快速入门及导入方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范:<style> 可以编写css代码 每一个声明最好使用分号结尾
语法:
选择器{
声明1;
声明2;
}
-->
<!--内部样式-->
<style>
h1{
color: aqua;
}
</style>
<!--外部样式-->
<link rel="stylesheet" href="css.css">
</head>
<body>
<!--style="color: chocolate" 行内样式-->
<h1 style="color: chocolate">标题</h1>
</body>
</html>
h1{
color: blue;
}
CSS的优势:
- 内容和表现分离
- 网页结构表现统一,可以实现复用
- 样式丰富
- 建议使用独立于html的css文件
- 利用SEO,容易被搜索引擎收录
- 优先级:就近原则(行内样式最近,其次哪个样式离h1近,优先级高于远的)
拓展:外部样式的两种写法
- 链接式
<!--外部样式-->
<link rel="stylesheet" href="css.css">
- 导入式
<style>
@import "css.css";
</style>
2. 三种选择器(重要)
作用:选择页面上的某一个或某一类元素
2.1 基本选择器
- 标签选择器:选择一类标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*标签选择器:会选择到页面上所有这个标签的元素*/
h1{
color: chocolate;
background: burlywood;
border-radius: 16px;
}
p{
font-size: 80px;
}
</style>
</head>
<body>
<h1>标题1</h1>
<h1>标题1</h1>
<p>学习</p>
</body>
</html>
- 类 选择器 class:选择所有class属性一致的标签,跨标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*类选择器的格式:.class的名称{}
好处:可以多个标签归类,同一个class可以复用
*/
.num1{
color: chocolate;
}
.num3{
color: aqua;
}
</style>
</head>
<body>
<h1 class="num1">标题1</h1>
<h1 class="num1">标题2</h1>
<h1 class="num3">标题3</h1>
<p class="num1">p标签</p>
</body>
</html>
- Id 选择器:全局唯一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*id选择器:id必须保证全局唯一
#id名称{}
不遵循就近原则,固定的
id选择器> class 选择器> 标签选择器
*/
#01{
color: aqua;
}
.num1{
color: antiquewhite;
}
</style>
</head>
<body>
<h1 id="01" class="num1">biaoti1</h1>
<h1 id="02">biaoti1</h1>
<h1 class="num1">biaoti1</h1>
</body>
</html>
2.2 层次选择器
- 后代选择器:某个元素后面的所有元素
body p{
background: aqua;
}
- 子选择器:只选中某个元素后的一代
body>p{
background: chocolate;
}
- 相邻兄弟选择器:只选中同辈
.active + p{
background: azure;
}
- 通用选择器:选中当前元素向下的所有兄弟选择器
.active~{
background: cornflowerblue;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*后代选择器*/
body p{
background: aqua;
}
/*子选择器*/
body>p{
background: chocolate;
}
/*相邻兄弟选择器*/
.active + p{
background: azure;
}
/*通用选择器*/
.active~{
background: cornflowerblue;
}
</style>
</head>
<body>
<p>p0</p>
<p>p1</p>
<p class="active">p2</p>
<p>p3</p>
<ul>
<li>
<p>p4</p>
</li>
<li>
<p>p5</p>
</li>
<li>
<p>p6</p>
</li>
</ul>
</body>
</html>
2.3 层次选择器
伪类:条件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*选中li的第一个元素*/
ul li:first-child{
background: chocolate;
}
/*选中li的最后一个元素*/
ul li:last-child{
background: cornflowerblue;
}
/*选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素的父级元素,选中父级元素的第一个p元素才能生效
*/
p:nth-child(1){
background: blueviolet;
}
/*选中父元素下的第2个类型*/
p:nth-of-type(2){
background: chartreuse;
}
</style>
</head>
<body>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
2.4 属性选择器(常用)
id+class 结合
=
*=
^=
$=
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>属性选择器</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: blueviolet;
text-align: center;/*文本左右居中*/
color: azure;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;/*字体大小/行高*/
}
/*属性名
= 绝对等于
*= 包含这个元素判断
^= 以这个元素开头
$= 以这个元素结尾
*/
/*存在id的元素*/
a[id]{
background: cornflowerblue;
}
/*id=first的元素*/
a[id=first]{
background: cornflowerblue;
}
/*class中有link的元素*/
a[class*=links]{
background: darkgreen;
}
/*选中href中以http开头的元素*/
a[href^=http]{
background: aqua;
}
/*选中href中以docx结尾的元素*/
a[href$=docx]{
background: chartreuse;
}
</style>
</head>
<body>
<p class="demo">
<a href="https://www.baidu.com" class="links iteam first" id="first">1</a>
<a href="https://www.gole.com" class="links iteam ">2</a>
<a href="resouce/1.jpg" class=" iteam">3</a>
<a href="resouce/1.docx" class=" iteam docx">4</a>
</p>
</body>
</html>