属性position:
属性position:relative;
- 参考物:元素本身
- 是否脱离文档流:不脱离
- 偏移距离:(向着中心点方向移动为正值)
- top:100px 顶部向下移动
- bottom:100px 底部向上移动
- left:100px 左侧向右移动
- right:100px 右侧向左移动
- 应用场景:
- 元素位置微调,初始位置空间还在
- 配合绝对定位使用
- 经典案例:鼠标移入元素向上进行移动
二、绝对定位
- 属性position:absolute;
- 参考物:外层具有position属性的元素,一层层向上查找,如果没有找到最终参考浏览器空白文档区域定位
- 是否脱离文档流:脱离
- 偏移距离:(向着中心点方向移动为正值)
- top 参考物的顶部距离元素的顶部
- bottom 参考物的底部距离元素的底部
- left 参考物的左侧距离元素的左侧
- right 参考物的右侧距离元素的右侧
- 应用场景:元素叠加效果,关闭按钮、二级菜单、轮播指示器
- 案例:
三、固定定位
- 属性position:fixed;
- 参考物:浏览器空白文档区域
- 是否脱离文档流:脱离
- 偏移距离:(向着中心点方向移动为正值)
- top 文档区域的顶部距离元素的顶部
- bottom 文档区域的底部距离元素的底部
- left 文档区域的左侧距离元素的左侧
- right 文档区域的右侧距离元素的右侧
- 应用场景:固定在浏览器某一位置,不随滚动条滚动
- 案例:返回顶部的空连接
<a href="#">返回顶部</a>
- 行内元素脱离文档流(绝对定位、固定定位、浮动)后就可以设置尺寸了
四、粘性定位
- 属性position:sticky;
- 参考物:浏览器空白文档区域顶部
- 是否脱离文档流:不脱离
- 偏移距离
- top 文档区域的顶部距离元素的顶部
- 应用场景:吸顶效果
五、元素堆叠顺序
- z-index改变元素的层叠顺序,可以取负值,数值越大越靠前,只有数值没有单位
六、元素居中办法 - 面试题
方法一:父元素尺寸未知,子元素尺寸已知,该方法同样适用于子元素比父元素尺寸大的情况
父元素{
width: 600px;
height: 600px;
border: 1px solid #000;
position: relative;
}
子元素{
width: 200px;
height: 100px;
background: yellow;
position: absolute;
/*向下移动父元素高度的一半*/
top: 50%;
/*向右移动父元素宽度的一半*/
left: 50%;
/*子元素向上移动高度的一半*/
margin-top: -50px;
/*子元素向左移动宽度的一半*/
margin-left: -100px;
}
方法二:父元素、子元素尺寸均未知,只适用于子元素比父元素尺寸小的情况
父元素{
width: 600px;
height: 600px;
border: 1px solid #000;
position: relative;
}
子元素{
width: 200px;
height: 100px;
background: yellow;
position: absolute;
margin:auto;
top: 0;
bottom: 0;
left: 0;
right: 0
}
七、定位、清除浮动综合案例
- 说明万能清除浮动法的优势
八、定位设置元素尺寸
- 子元素未设置宽高,指定子元素距离父元素四条边的距离即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.father {
width: 800px;
height: 600px;
border: 1px solid #000;
position: relative;
}
.son {
position: absolute;
top: 100px;
bottom: 100px;
left: 50px;
right: 50px;
background: yellow;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
九、面试题 - 一侧固定一侧自适应布局方案(未完待续)
方法一:固定定位方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } .left { width: 200px; height: 500px; background-color: red; position: fixed; top: 0; left: 0; } .auto { position: fixed; left: 200px; right: 0; top: 0; bottom: 0; background-color: yellow; } </style> </head> <body> <div class="left"></div> <div class="auto"></div> </body> </html>
方法二:浮动方法
- 重点要添加
html,body { height: 100%; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } /*不加这段代码,body的高度为0*/ html,body { height: 100%; } .left { width: 200px; height: 500px; background-color: red; float: left; } .auto { height: 100%; background: yellow; margin-left: 200px; } </style> </head> <body> <div class="left"></div> <div class="auto"></div> </body> </html>
- 重点要添加
方法三:BFC
- 方法四:calc()函数方法
- 方法四:弹性盒
十、知识点补充
- 鼠标移入属性 cursor:pointer小手;
- 如果无法设置尺寸的那一类元素(s,span),一旦设置了脱离文档流的属性(浮动、绝对定位、固定定位),那么就可以设置宽高了