写哪里:
css写在style标签中,style标签一般写在head标签里面,title标签下面
怎么写:pData\Roaming\Typora\typora-user-images\image-20240116104941407.png" alt="image-20240116104941407" style="zoom:33%;" />
注:选择器——找标签用的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* css的注释 */
/* 选择器 {css属性} */
/* 选择器的作用:查找标签 */
</style>
</head>
<body>
</body>
</html>
css引入方式:
内嵌式:css写在style标签中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p {
color:red;
font-size: 30px;
background-color: aquamarine;
width:400px;
height: 400px;
}
</style>
</head>
<body>
<p>这是p标签</p>
</body>
</html>
外联式:css写在一个单独的.css文件中;需要通过link标签在网页中引入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 关系:样式表 ; 找路径 -->
<link rel="stylesheet" href="./my.css">
</head>
<body>
<p>这是p标签</p>
</body>
</html>
p{
color:red;
}
行内式:css写在标签的style属性中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>网页的标题</title>
</head>
<body>
<p>这是p标签</p>
<div style="color: red; font-size:30px">这是div标签</div>
</body>
</html>
选择器
标签选择器
以标签名命名的选择器
结构:标签名 { css属性名:属性值;}
类选择器
结构:.类名 { csss属性名:属性值;}
注:
-
所有标签上都有class属性,class属性的属性值称为类名(类似于名字)
-
类名可以重复,一个类选择器可以同时选中多个标签
-
类名可以由数字、字母、下划线、中划线组成,但不能以数字或中划线开头
-
一个标签可以同时有多个类名,类名之间以空格隔开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.red{
color: red;
}
.size{
font-size: 30px;
}
</style>
</head>
<body>
<p>111</p>
<p class="red size">222</p>
</body>
</html>
id选择器
结构:#id属性 { css属性:属性值; }
注:
-
所有标签上都有id属性
-
id属性值类似于身份证号码,在一个页面中是唯一的,不可重复的!
-
一个标签上只能有一个id属性值
-
一个id选择器只能选中一个标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#blue{
color: skyblue;
}
</style>
</head>
<body>
<div id="blue">这个div文字是蓝色的</div>
</body>
</html>
通配符选择器
结构:* { css属性名: 属性值;}
能选中所有标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
color:red;
}
</style>
</head>
<body>
<div>div</div>
<p>pppp</p>
<h1>h1</h1>
<span>span</span>
</body>
</html>
字体和文本样式
字体样式
字体大小:font-size
取值:数字 + px(像素单位)
浏览器中默认字号:16
字体粗细:font-weight
取值:关键字 normal(正常) ;bold(加粗)
数字 400 (正常) ;700(加粗)
字体样式:font-style (控制文字变倾斜)
取值:normal(正常) ; italic(倾斜)
字体类型:font-family
取值:微软雅黑、宋体、黑体 ......
字体类型:font属性连写
取值:font : style weight size family
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p{
font:normal 700 66px 宋体
}
</style>
</head>
<body>
<p>这是p标签</p>
</body>
</html>
只能省略前两个
文本样式
文本缩进:text-indent
取值:数字 + px
或 数字+em ; em为一个字的大小
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p{
text-indent:32px;
/* 首行缩进2个字的大小 ; 默认字号:16px ; 32 */
}
</style>
</head>
<body>
<p>2009年,事件视界望远镜团队让世界首次看到了黑洞的样子</p>
</body>
</html>
文本水平对齐方式 :text-align
取值:
属性值 | 效果 |
---|---|
left | 左对齐 |
center | 居中对齐 |
right | 右对齐 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
h1{
text-align: center;
}
body{
text-align: center;
}
/* 图在body里面 */
img{
width: 300px;
}
</style>
</head>
<body>
<h1>新闻标题</h1>
<img src="./向日葵.jpg" alt="" >
</body>
</html>
text-align : center 能让哪些元素水平居中
文本
span标签、a标签
input标签、img标签
注:如果需要让以上元素水平居中,text-align: center需要给以上元素的父元素设置
文本修饰:text-decoration
取值:
属性值 | 效果 |
---|---|
underline | 下划线(常用) |
line-through | 删除线(不常用) |
overline | 上划线(几乎不用) |
none | 无装饰线(常用) |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
a{
text-decoration: none;
}
/* 超链接去下划线 */
</style>
</head>
<body>
<a href="#">我是超链接,点呀</a>
</body>
</html>
行高
作用:控制一行的上下行间距
属性名:line-height
取值:数字 + px
或 倍数(当前标签font-size的倍数)
注:font : style weight size/line-height family;
选择器的进阶
复合选择器
后代选择器
作用:选择父元素后代中满足条件的元素
选择器语法:选择器1 选择器2 {css}
注意:
后代包括:儿子、孙子、重孙子.......
选择器与选择器之间通过空格隔开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div p{
color:red;
}
</style>
</head>
<body>
<p>这是一个p标签</p>
<div>
<p>这是div的儿子p</p>
</div>
</body>
</html>
父选择器 后代选择器 { }
子代选择器:>
作用:选择父元素子代中满足条件的元素
选择器语法: 选择器1 > 选择器2 { css }
注:子代只包括:儿子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 只想选中儿子 */
div>a{
color:red;
}
</style>
</head>
<body>
<div>
父级
<a href="#">这是div里面的a</a>
<p>
<a href="#">这是div里面的p里面的a</a>
</p>
</div>
</body>
</html>
并集选择器: ,
作用:同时选择多组标签,设置相同的样式
选择器语法:选择器1 ,选择器2 { css }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p,
div,
span,
h1{
color:red;
}
</style>
</head>
<body>
<p>ppp</p>
<div>div</div>
<span>span</span>
<h1>h1</h1>
<h2>h2</h2>
</body>
</html>
交集选择器
作用:选中页面中同时满足多个选择器的标签
选择器语法:选择器1选择器2 { css }
注:交集选择器中的选择器是紧挨着的,没有东西分隔
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p.box{
color:red;
}
</style>
</head>
<body>
<p class="box">这是p标签: box</p>
<p>pppppppp</p>
<div class="box">这是div标签: box</div>
</body>
</html>
hover伪类选择器
作用:选中鼠标悬停在元素上的状态,设置样式
选择器语法: 选择器:hover {css}
注:任何标签都可以添加伪类,任何一个标签都可以悬停
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
a:hover{
color:red;
}
</style>
</head>
<body>
<a href="#">这是超链接</a>
</body>
</html>
emmet语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- div 同级 p + -->
<div></div>
<p></p>
<!-- 父子 > -->
<div>
<p></p>
</div>
<ul>
<li></li>
</ul>
<!-- ul有3个li *乘号 ul>li*3-->
<ul>
<li></li>
<li></li>
<li></li>
</ul>
<!-- ul有3个li ,li里有文字111 {文字} ul>li{111}*3-->
<ul>
<li>111</li>
<li>111</li>
<li>111</li>
</ul>
<!-- ul有3个li,li文字1,2,3 ul>li{$}*3-->
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
背景相关属性
px像素
背景颜色
属性名:background-color
背景图片
属性名:background-image
背景平铺
属性名:background-repeat
属性值:
取值 | 效果 |
---|---|
repeat | (默认值)水平和垂直方向都平铺 |
no-repeat | 不平铺 |
repeat-x | 沿水平方向(x轴)平铺 |
repeat-y | 沿垂直方向(y轴)平铺 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 400px;
height:400px;
background-color: pink;
background-image: url(./向日葵.jpg);
background-repeat: repeat-y;
}
</style>
</head>
<body>
<div>文字</div>
</body>
</html>
背景位置
属性名:background-position
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:400px;
height:400px;
background-color: pink;
background-image: url(./向日葵.jpg);
background-repeat: no-repeat;
/* background-position: right 0; */
/* background-position: right bottom; */
background-position: center center;
}
</style>
</head>
<body>
<div>文字</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:400px;
height:400px;
background-color: pink;
background-image: url(./向日葵.jpg);
background-repeat: no-repeat;
/* background-position: right 0; */
/* background-position: right bottom; */
/* background-position: center center; */
/* 正数:向右向下移动 ; 负数:向左向上移动 */
/* background-position: 50px 50px; */
background-position: -50px -100px;
}
</style>
</head>
<body>
<div>文字</div>
</body>
</html>
背景色和背景图只显示在盒子里面
背景相关属性连写
属性名:background
书写顺序推荐:background:color image repeat position
不分先后顺序;背景图位置如果是英文单词可以颠倒顺序,数值不可以颠倒顺序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:400px;
height: 400px;
background: pink url(./向日葵.jpg) no-repeat center center;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
扩展:
背景图的缩放
背景相关属性连写:background:color image repeat position/size
元素显示模式
块级元素
特点:
-
独占一行
-
宽度默认是父级的100%
-
添加宽高都生效
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 300px;
height: 300px;
background-color: pink;
}
</style>
</head>
<body>
<div>111</div>
<div>222</div>
</body>
</html>
行内元素
显示特点:
-
一行可以显示多个
-
宽度和高度默认由内容撑开
-
不可以设置宽高;设置宽高不生效
代表标签:a、span
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
span{
width:300px;
height:300px;
background-color: pink;
}
</style>
</head>
<body>
<span>span</span>
<span>span</span>
</body>
</html>
行内块元素
因为它们具有块元素和行内元素的特点;故称为行内块元素
显示特点:
-
一行可以显示多个
-
可以设置宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width, initial-scale=1.0">
<title>Document</title>
<style>
img{
width:100px;
height:100px;
}
</style>
</head>
<body>
<img src="./向日葵.jpg" alt="">
<img src="./向日葵.jpg" alt="">
</body>
</html>
代表标签:input、textarea
元素显示模式转换
语法:
属性 | 效果 |
---|---|
display:block | 转换成块级元素 |
display:inline-block | 转换成行内块元素 |
display:inline | 转换成行内元素 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:300px;
height:300px;
background-color:pink;
display:inline;
}
</style>
</head>
<body>
<div>1111</div>
<div>2222</div>
</body>
</html>
标签的嵌套扩展:
-
p和h标题不能相互嵌套
-
p里不能包含div
-
a标签不能嵌套a标签
css特性
继承性
所有控制文字的属性(color、font-style、font-weight、font-size、font-family、text-indent、text-align、line-height...00)都能继承;不是控制文字的都不能继承
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
color:red;
}
</style>
</head>
<body>
<div>
这是div标签里面的文字
<span>这是div里面的span</span>
</div>
</body>
</html>
注:没有就继承爹的,有就听自己的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
color:red;
font-size: 12px;
}
</style>
</head>
<body>
<div>
<a href="#">超链接</a>
<h1>一级标题</h1>
</div>
</body>
</html>
层叠性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
color:red;
color:pink;
}
.box{
font-size: 30px;
}
</style>
</head>
<body>
<div class="box">文字</div>
</body>
</html>
优先级
继承 < 通配符选择器 < 标签选择器 < 类选择器 < id选择器 < 行内样式 < !important
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
color:red;
}
div{
color:green !important;
}
</style>
</head>
<body>
<div style="color:pink;">测试优先级</div>
</body>
</html>
权重叠加计算
盒子模型
CSS 中规定每个盒子分别由:内容区域(content)、内边距区域(padding)、边框区(border)、外边距区域(margin)构成,这就是盒子模型。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:300px;
height:300px;
background-color: pink;
/* 边框线 == 纸箱子 */
border:1px solid #000;
/* 内边距 == 填充泡沫 :出现在内容和盒子边缘之间*/
padding: 20px;
/* 外边距 :出现在两个盒子之间,出现在盒子的外面*/
margin:50px;
}
</style>
</head>
<body>
<div>内容电脑</div>
<div>内容电脑</div>
</body>
</html>
内容区域(content)的宽度和高度
作用:利用width和height属性默认设置盒子内容区域的大小
属性:width / height
常见取值:数字+px
边框(border)
属性值:边框线粗细像素 线条种类 线条颜色
如:border:10px solid red;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:200px;
height:200px;
background-color: pink;
/* border:实线 dashed:虚线 dotted:点线 */
border:2px solid #000;
}
</style>
</head>
<body>
<div>内容</div>
</body>
</html>
边框——单方向设置
场景:只给盒子的某个方向单独设置边框
属性名:border - 方位名词
border-top border-bottom border-left border-right
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:200px;
height:200px;
background-color: pink;
/* border:实线 dashed:虚线 dotted:点线 */
border-left:2px solid #000;
}
</style>
</head>
<body>
<div>内容</div>
</body>
</html>
单个属性:border-width border-style border-color
border会撑大盒子的尺寸
新浪导航例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
height:40px;
border-top:3px solid #ff8500;
border-bottom: 1px solid #edeef0;
}
.box a{
width:80px;
height:40px;
/* 转行内块元素 */
display: inline-block;
/* 水平居中 */
text-align: center;
/* 垂直居中 */
line-height:40px;
/* 文字大小 */
font-size:12px;
color:#4c4c4c;
/* 去下划线 */
text-decoration: none;
}
.box a:hover{
background-color:#edeef0;
color:#ff8400;
}
</style>
</head>
<body>
<div class="box">
<a href="#">新浪导航</a>
<a href="#">新浪导航</a>
<a href="#">新浪导航</a>
<a href="#">新浪导航</a>
</div>
</body>
</html>
内边距(padding)
padding 属性可以当做复合属性使用,表示单独设置某个方向的内边距;padding最多取4个值
四值:上 右 下 左 (从最上开始顺时针转一圈)
三值:上 左右 下
两值:上、下 左、右 (上下相同;左右相同)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 300px;
height: 300px;
background-color: pink;
/* 添加了4个方向的内边距 */
padding:50px;
}
</style>
</head>
<body>
<div>文字</div>
</body>
</html>
灵活 => 用padding撑大距离
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
height:40px;
border-top:3px solid #ff8500;
border-bottom: 1px solid #edeef0;
}
.box a{
padding:0 16px;
/* width:80px; */
height:40px;
/* 转行内块元素 */
display: inline-block;
/* 水平居中 */
text-align: center;
/* 垂直居中 */
line-height:40px;
/* 文字大小 */
font-size:12px;
color:#4c4c4c;
/* 去下划线 */
text-decoration: none;
}
.box a:hover{
background-color:#edeef0;
color:#ff8400;
}
</style>
</head>
<body>
<div class="box">
<a href="#">新浪</a>
<a href="#">新浪导航新浪导航</a>
<a href="#">新浪导航</a>
<a href="#">新浪导航</a>
</div>
</body>
</html>
css3自动内减:减padding border
加属性box-sizing:border-box;
外边距(margin)
清除默认内外边距
*{ margin:0; padding:0; }
版心居中
网页有效内容居中;水平居中auto自适应,上下无所谓如:0
margin:0 auto;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:1000px;
height: 100px;
background-color: pink;
margin:0 auto;
}
</style>
</head>
<body>
<div>版心</div>
</body>
</html>
结构伪类选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 选中第一个 */
/* li:first-child{
background-color: green;
}*/
/* 最后一个 */
/* li:last-child{
background-color: green;
} */
/* 任意一个 */
/* li:nth-child(5){
background-color: green;
} */
/* 倒数第xx个 */
li:nth-last-child(1){
background-color:blue;
}
</style>
</head>
<body>
<!-- ul>li{这是第$个li}*8 -->
<ul>
<li>这是第1个li</li>
<li>这是第2个li</li>
<li>这是第3个li</li>
<li>这是第4个li</li>
<li>这是第5个li</li>
<li>这是第6个li</li>
<li>这是第7个li</li>
<li>这是第8个li</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 偶数 */
li:nth-child(2n){
background-color: pink;
}
/* 奇数 */
li:nth-child(2n+1){
background-color: red;
}
/* 前三个 */
li:nth-child(-n+3){
background-color: blue;
}
</style>
</head>
<body>
<ul>
<li>这是第1个li</li>
<li>这是第2个li</li>
<li>这是第3个li</li>
<li>这是第4个li</li>
<li>这是第5个li</li>
<li>这是第6个li</li>
<li>这是第7个li</li>
<li>这是第8个li</li>
</ul>
</body>
</html>
伪元素
通过css创建标签,装饰一些不重要的小图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
width:300px;
height:300px;
background-color: pink;
}
.father::before{
content:'老鼠';
color:green;
width:100px;
height:100px;
background-color: blue;
/* 默认是行内元素,宽高不生效,转块级元素 */
display:block;
}
.father::after{
content:'大米';
}
</style>
</head>
<body>
<div class="father">爱</div>
</body>
</html>
标准流
浮动
浏览器执行速度快
css属性书写顺序:1.浮动/display 2.盒子模型相关属性 margin border padding 宽度高度背景色 3.文字样式
浮动的作用
网页布局;块标签完美的在一行排列
浏览器解析行内块或行内标签的时候,如果标签换行书写就会产生一个空格的距离
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img{
float:left;
}
div{
width: 100px;
height: 100px;
}
.one{
background-color: pink;
float:left;
}
.two{
background-color: skyblue;
/* right */
float:left;
}
</style>
</head>
<body>
<!-- 1.图文环绕 -->
<!-- <img src="./向日葵.jpg" alt="">
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈 -->
<!-- 2.网页布局:块在一行排列 -->
<div class="one">one</div>
<div class="two">two</div>
</body>
</html>
浮动的特点
1.浮动元素会脱离标准流,在标准流中不占位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.one{
width: 100px;
height: 100px;
background-color: pink;
float:left;
}
.two{
width: 200px;
height: 200px;
background-color: skyblue;
}
.three{
width: 300px;
height: 300px;
background-color: orange;
}
</style>
</head>
<body>
<div class="one">one</div>
<div class="two">two</div>
<div class="three">three</div>
</body>
</html>
2.浮动元素比较准流高半个级别,可以覆盖标准流中的元素
3.浮动找浮动 下一个浮动元素会在上一个浮动元素后面左右浮动
浮动的标签 顶对齐
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.one{
width: 100px;
height: 100px;
background-color: pink;
float:left;
}
.two{
width: 200px;
height: 200px;
background-color: skyblue;
float:left;
}
.three{
width: 300px;
height: 300px;
background-color: orange;
}
</style>
</head>
<body>
<div class="one">one</div>
<div class="two">two</div>
<div class="three">three</div>
</body>
</html>
不想盒子顶对齐 加margin外边距把盒子往下挪
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.one{
width: 100px;
height: 100px;
background-color: pink;
float:left;
margin-top: 50px;
}
.two{
width: 200px;
height: 200px;
background-color: skyblue;
float:left;
}
.three{
width: 300px;
height: 300px;
background-color: orange;
}
</style>
</head>
<body>
<div class="one">one</div>
<div class="two">two</div>
<div class="three">three</div>
</body>
</html>
4.一行可显示多个且可设置宽高
去掉列表的符号:list-style:none;
清除浮动
清除浮动给别的标签带来的影响
受浮动影响的情况:父子级标签,子级浮动,父级没有高度,后面的标准流盒子会受影响,会显示到上面的位置。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.top{
margin:0 auto;
width:1000px;
/* height:300px; */
background-color: pink;
}
.bottom{
height:100px;
background-color: green;
}
.left{
float:left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right{
float:right;
width: 790px;
height: 300px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="top">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
父级加高度会规避该问题
对于不方便加高;如何清除浮动:
格外标签法
-
在父元素内容的最后添加一个块级元素
-
给添加的块级元素设置clear:both
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.top{
margin:0 auto;
width:1000px;
/* height:300px; */
background-color: pink;
}
.bottom{
height:100px;
background-color: green;
}
.left{
float:left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right{
float:right;
width: 790px;
height: 300px;
background-color: skyblue;
}
.clearfix{
/*清除左右两侧浮动的影响*/
clear: both;
}
</style>
</head>
<body>
<div class="top">
<div class="left"></div>
<div class="right"></div>
<div class="clearfix"></div>
</div>
<div class="bottom"></div>
</body>
</html>
单伪元素清除法
.clearfix::after{ content:''; /* 伪元素添加的标签是行内的,要求块 */ display:block; clear:both; }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.top{
margin:0 auto;
width:1000px;
/* height:300px; */
background-color: pink;
}
.bottom{
height:100px;
background-color: green;
}
.left{
float:left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right{
float:right;
width: 790px;
height: 300px;
background-color: skyblue;
}
.clearfix::after{
content:'';
/* 伪元素添加的标签是行内的,要求块 */
display:block;
clear:both;
}
</style>
</head>
<body>
<div class="top clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
双伪元素清除法
清外边距塌陷也能清浮动
.clearfix::before, .clearfix::after{ content:''; display:table; } .clearfix::after{ clear:both; }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.top{
margin:0 auto;
width:1000px;
/* height:300px; */
background-color: pink;
}
.bottom{
height:100px;
background-color: green;
}
.left{
float:left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right{
float:right;
width: 790px;
height: 300px;
background-color: skyblue;
}
.clearfix::before,
.clearfix::after{
content:'';
display:table;
}
.clearfix::after{
clear:both;
}
</style>
</head>
<body>
<div class="top clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
给父元素设置overflow:hidden
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.top{
margin:0 auto;
width:1000px;
/* height:300px; */
background-color: pink;
overflow:hidden;
}
.bottom{
height:100px;
background-color: green;
}
.left{
float:left;
width: 200px;
height: 300px;
background-color: #ccc;
}
.right{
float:right;
width: 790px;
height: 300px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="top">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="bottom"></div>
</body>
</html>
调节图片垂直对齐方式middle居中:vertical-align: middle;
CSS布局
CSS有哪些布局方式:标准流+浮动+定位
定位
一般用于 盒子之间的层叠情况;可以让盒子始终固定在屏幕中的某个位置
定位方式
1.属性名:position
常见属性值:
定位方式 | 属性值 |
---|---|
静态定位 | static |
相对定位 | relative |
绝对定位 | absolute |
固定定位 | fixed |
2.设置偏移值
静态定位
就是不定位
相对定位
相对于自己之前的位置进行移动
position: relative;
快捷:por
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: relative;
left:100px;
top:200px;
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box">box</div>
</body>
</html>
特点:
-
占有原来的位置—— 即没有脱离标准流的控制
-
仍然具有标签原有的显示模式特点
-
改变位置参照自己原来的位置
如果left和right都有,以left为准;top和bottom都有,以top为准
绝对定位
相对于非静态定位(static)的父元素进行定位移动
position: absolute;
poa
1.先找已经定位的父级,如果有这样的父级就以这个父级为参照物进行定位;
2.有父级,但父级没有定位,以浏览器窗口为参照物进行定位
特点:
1.脱离标准流,不占位
2.改变标签的显示模式特点:具备行内块的特点(在一行共存,宽高生效)
子绝父相
父级相对定位;子级绝对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
width: 400px;
height: 400px;
background-color: pink;
}
.son{
position: relative;
width: 300px;
height: 300px;
background-color: skyblue;
}
.sun{
position: absolute;
right:20px;
bottom:50px;
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<div class="father">
<div class="son">
<div class="sun"></div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
position: relative;
width: 400px;
height: 400px;
background-color: pink;
}
.son{
width: 300px;
height: 300px;
background-color: skyblue;
}
.sun{
position: absolute;
right:20px;
bottom:50px;
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<div class="father">
<div class="son">
<div class="sun"></div>
</div>
</div>
</body>
</html>
绝对定位查找父级的方式:就近找定位的父级,如果逐层查找不到这样的父级,就以浏览器窗口为参照进行定位
绝对定位居中
标准流盒子居中margin:0 auto;绝对定位的盒子不能使用左右margin auto居中
水平居中;left:50%,整个盒子移动到浏览器中间偏右的位置;magin-left把盒子向左侧移动:移动自己宽度的一半
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=+, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: absolute;
/* margin:0 auto */
left:50%;
margin-left:-150px;
width:300px;
height:300px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=+, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: absolute;
/* margin:0 auto */
left:50%;
margin-left:-150px;
top:50%;
margin-top:-150px;
width:300px;
height:300px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
水平垂直都居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=+, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
width:303px;
height:300px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
固定定位
滚动网页位置不受影响
相对于浏览器进行定位移动
position:fixed;
pof
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: fixed;
left:0;
top:0;
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<div class="box"></div>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
<p>测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位测试定位</p>
</body>
</html>
特点:
1.脱离标准流,不占位置
2.改变位置参考浏览器窗口
3.具备行内块特点
元素的层级关系 ,。
默认情况下,定位的盒子,后来者居上
装饰
基线:浏览器文字类型元素排版中存在用于对齐的基线
垂直对齐方式
属性名:vertical-align
属性值:
属性值 | 效果 |
---|---|
baseline | 默认,基线对齐 |
top | 顶部对齐 |
middle | 中部对齐 吧 |
bottom | 底部对齐 |
浏览器把行内和行内块标签当做文字处理,默认基线对齐;浏览器在处理行内标签和行内块标签时默认按照文字解析
光标类型
场景:设置鼠标光标在元素上时显示的样式
属性名:cursor
常见属性值:
属性值 | 效果 |
---|---|
default | 默认值,通常是箭头 |
pointer | 小手效果,提示用户可以点击 |
text | 工字型,提示用户可以选择文字,表示可以复制 |
move | 十字光标,提示用户可以移动 |
边框圆角
取值:半径px
边框圆角的常见应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.one{
width: 200px;
height: 200px;
background-color: pink;
border-radius: 100px;
/*border-radius: 50%;*/
}
</style>
</head>
<body>
<div class="one"></div>
</body>
</html>
overflow溢出部分显示效果
元素本身隐藏
场景:让某元素本身在屏幕中不可见。
常见属性:
visibility: hidden
display: none ----- 不占位的隐藏
元素整体透明度
场景:让某元素整体(包括内容)一起变透明
属性名:opacity
属性值:0~1的数字
1:完全不透明
0:完全透明
0.5: 半透明
项目样式补充
盒子阴影
过渡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 过渡配合hover使用,谁变化谁加过渡属性 */
.box{
width: 200px;
height: 200px;
background-color: pink;
/* 宽度200,悬停的时候宽度600,花费1秒钟 */
/* transition: width 1s , background-color 2s; */
transition: all 1s;
/* 如果变化的属性多,直接写all,表示所有 */
}
.box:hover{
width: 600px;
background-color: red;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>