用于在盒子模型中,使用不同的方式对盒子的位置进行修改
static- 默认值 静态的 不进行定位操作 忽略left right top bottom 属性
2.1 绝对定位absolute
absolute- 绝对定位 元素相较于第一个未设置position为static的父元素左上角的位置
父边框的粗细不会影响位置的数值,因为绝对定位是在父元素内边距与边框左上角作为定位的起始,父盒子的内边距会影响子元素的原始位置不会影响绝对定位,如果子盒子拥有外边距,那么实际距离为外边距左上角距离父元素内边距与边框左上角作为定位的起始,如果所有父标签都没有设置定位,那么会寻找最初包裹的标签进行定位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>
body {
margin: 0;
}
.wrap {
height: 500px;
width: 500px;
background-color: bisque;
border: solid 10px black;
position: absolute;
padding-left: 100px;
margin-top: 100px;
}
.inner {
height: 100px;
width: 100px;
background-color: chartreuse;
}
.inner:hover {
position: absolute;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<div class="wrap">
<div class="inner"></div>
</div>
</body>
</html>
2.2 相对定位relative
relative- 相对定位 与父元素定位方式无关,是相对于当前元素初始位置进行定位
相对于当前元素初始位置进行位置移动,并且初始位置不会让出,如果不进行移动不会遮挡其他元素,但是如果进行移动并且存在其他元素会进行遮挡
<!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>
.wrap {
height: 500px;
width: 500px;
background-color: bisque;
border: solid 100px black;
padding-left: 100px;
position: relative;
margin-left: 100px;
}
.inner {
height: 100px;
width: 100px;
background-color: chartreuse;
margin-left: 100px;
position: relative;
}
.inner:hover {
left: 100px;
top: 100px;
}
.no {
height: 500px;
width: 500px;
background-color: red;
}
.top{
left: 100px;
}
.bottom{
}
</style>
</head>
<body>
<div class="wrap top">
<div class="inner"></div>
</div>
<div class="wrap bottom">
<span class="inner" >啥也没有</span>
</div>
<div class="no">
</div>
</body>
</html>
绝对定位与相对定位异同点
绝对定位也可以看出特殊的相对定位,只不过与相对定位不同的是,绝对定位相对的是设置定位的父标签,相对定位相对的是当前标签起始位置
绝对定位会标签转换为块元素,相对定位不会
绝对定位会使标签脱离文档流让出位置,相对定位虽然也会脱离文档流但是不会让出位置
绝对定位与相对定位无论父标签怎么移动定位标签位置相对固定
绝对定位设置起始点为父标签内边距与边框交集左上角,相对定位起始点为当前标签盒子左上角(包含外边距)
2.3 固定定位fixed
将标签以当前视口进行固定定位设置
fixed- 固定定位,将指定标签固定在浏览器视口指定位置,脱离文档流让出原位置
<!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>
body {
margin: 0;
height: 100vh;
width: 100%;
background-color: lightgray;
}
div {
width: 200px;
height: 200px;
background-color: lightsalmon;
}
.fixed {
width: 10vw;
height:20vh;
background-color: transparent;
position: fixed;
margin-left: 100px;
right: 0;
bottom: 0;
border-radius: 20vh;
border-bottom-style: solid;
border-bottom-color: black;
}
</style>
</head>
<body>
<div class="fixed"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
2.4 粘性定位sticky
是相对定位与固定定位结合动态定位的方式
sticky- 粘性定位 一本用于拥有多级导航(对二级导航栏设置)时的页面,为保证页面滚动仍然可以使用导航栏进行跳转功能操作,默认情况下为相对定位,在原位置不进行修改,只有达到峰值(设置值)时,使用设置值转换为固定定位
<!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>
div {
height: 100px;
background-color: turquoise;
}
.stickyDiv {
position: sticky;
top: 0px;
background-color: yellowgreen;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div class="stickyDiv">3</div>
<div>4</div>
<div class="stickyDiv">5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</body>
</html>
2.5 层叠属性
z-index:用于设置同层元素之间的层叠顺序,如果没有设置层叠顺序默认是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>
body {
margin: 0;
}
div {
height: 200px;
width: 200px;
top: 0;
}
.abs {
background-color: tomato;
position: absolute;
left: 20px;
}
.rel {
background-color: thistle;
position: relative;
left: 50px;
z-index: -1;
}
.fix {
background-color: aquamarine;
position: fixed;
left: 80px;
z-index: -1;
}
.left {
background-color: antiquewhite;
float: left;
z-index: 1000;
}
</style>
</head>
<body>
<div class="abs">绝对</div>
<div class="fix">固定</div>
<div class="left"> 浮动</div>
</body>
</html>
二、BFC
2.1 概念
BFC(Block formatting context)直译为"块级格式化上下文"。它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的Block-level Box如何布局,并且与这个区域外部毫不相干。
核心:BFC内的元素与外的元素没有关联
2.2 BFC的特点
内部的Box会在垂直方向,一个接一个地放置。
Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠。
每个盒子(块盒与行盒)的margin box的左边,与包含块border box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。
BFC的区域不会与float box重叠。
BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
计算BFC的高度时,浮动元素也参与计算。
2.3 BFC的创建
只要满足如下任一条件即可创建BFC
-
1、float的值不是none。
-
2、position的值不是static或者relative。
-
3、display的值是inline-block、table-cell、flex、table-caption或者inline-flex
-
4、overflow的值不是visible
2.4 BFC解决问题
2.4.1 解决margin重叠问题
因为属于同一个bfc的相邻box的margin会发生重叠,所以使用bfc解决重叠的方法很简单,只需要让其属于不同的BFC即可
<!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>
body {
margin: 0;
}
.wrap {
overflow: hidden;
background-color: crimson;
}
.inner {
height: 100px;
}
.inner1 {
background-color: darkgreen;
margin: 10px 0;
}
.inner2 {
background-color: darkkhaki;
margin: 20px 0;
}
</style>
</head>
<body>
<div class="wrap">
<div class="inner inner1"></div>
<!-- 将第二个使用 bfc包裹使其不在同一个bfc中 margin就不会重叠 -->
<div class="wrap">
<div class="inner inner2"></div>
</div>
</div>
</body>
</html>
2.4.2 解决文字环绕问题
浮动的元素不会覆盖文字信息,会造成文字环绕,当然有时我们需要环绕的效果但是,有时我们需要完整的文字与元素显示这个时候就可以使用bfc解决这一问题
没有添加overflow: hidden之前
添加overflow: hidden之后
2.4.3 实现页面多栏布局
BFC不会让浮动的块遮盖,本身为块级元素,所以独占一行,可以使用这一特性完成页面的多栏布局
两栏布局
左侧宽度固定右侧自适应浏览器
将左侧设置为浮动,右侧设置为BFC,运用BFC不会被浮动遮挡的特性实现布局
<!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>
body {
margin: 0;
}
.left {
float: left;
width: 30vw;
height: 100vh;
background-color: darkorange;
}
.right {
overflow: hidden;
height: 100vh;
background-color: darkturquoise;
}
</style>
</head>
<body>
<div class="left"></div>
<div class="right"></div>
</body>
</html>
三栏布局
左侧宽度固定右侧自适应浏览器
将左侧右侧设置为浮动,中间设置为BFC,运用BFC不会被浮动遮挡的特性实现布局
<!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>
body {
margin: 0;
}
.left {
float: left;
width: 30vw;
height: 100vh;
background-color: darkorange;
}
.center {
overflow: hidden;
height: 100vh;
background-color: darkturquoise;
}
.right {
float: right;
width: 30vw;
height: 100vh;
background-color: darkorange;
}
</style>
</head>
<body>
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</body>
</html>
2.4.4 清除浮动
overflow: hidden可以清除浮动,使在其内部的浮动块重新加入高度的计算,从而解决浮动导致的高度坍塌问题
<!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>
body {
margin: 0;
}
.left {
float: left;
width: 30vw;
height: 10px;
background-color: darkorange;
}
.center {
overflow: hidden;
height: 100vh;
background-color: darkturquoise;
}
.right {
float: right;
width: 30vw;
height: 10px;
background-color: darkorange;
}
</style>
</head>
<body>
<div class="left"></div>
<div class="right"></div>
<div class="center">
这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字这是文字
</div>
</body>
</html>