2019.09.18
入门基础
定位
元素的定位属性
- 边偏移
定位要和这边偏移搭配使用了, 比如 top: 100px; left: 30px; 等等
边偏移属性 | 描述 |
---|---|
top | 顶端偏移量,定义元素相对于其父元素上边线的距离 |
bottom | 底部偏移量,定义元素相对于其父元素下边线的距离 |
left | 左侧偏移量,定义元素相对于其父元素左边线的距离 |
right | 右侧偏移量,定义元素相对于其父元素右边线的距离 |
- 定位模式(定位的分类)
在CSS中,position属性用于定义元素的定位模式,其基本语法格式如下:
选择器{position:属性值;}
position属性的常用值
值 | 描述 |
---|---|
static | 自动定位(默认定位方式) |
relative | 相对定位,相对于其原文档流的位置进行定位 |
absolute | 绝对定位,相对于其上一个已经定位的父元素进行定位 |
fixed | 固定定位,相对于浏览器窗口进行定位 |
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
div {
width: 100px;
height: 100px;
background-color: pink;
top: 100px; /* 边偏移 */
left: 100px;
position: absolute; /* 定位模式(绝对定位) */
/* 定位模式 + 边偏移 = 完整定位 */
}
</style>
</head>
<body>
<div></div>
</body>
</html>
静态定位
- 静态定位是所有元素的默认定位方式,当position属性的取值为static时,可以将元素定位于静态位置。
注意:
在静态定位状态下,无法通过边偏移属性(top、bottom、left或right)来改变元素的位置。
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
div {
width: 100px;
height: 100px;
background-color: pink;
position: static; /* 定位模式 -- 静态定位 */
left: 100px;
top: 100px;
/* 静态定位 -- 对于边偏移无效的。 */
/* 一般他用来 清除定位的。 一个原来有定位的盒子,不想加定位了,就写这句话 */
}
</style>
</head>
<body>
<div></div>
</body>
相对定位
- 相对定位是将元素相对于它在标准流中的位置进行定位,当position属性的取值为relative时,可以将元素定位于相对位置。
注意:
- 相对定位最重要的一点是,它可以通过边偏移移动位置,但是原来的所占的位置,继续占有。
- 其次,每次移动的位置,是以自己的左上角为基点移动(相对于自己来移动位置)
例:
<style>
div:first-child {
width: 100px;
height: 100px;
background-color: pink;
position: absolute; /* 定位模式 绝对定位 */
top:20px;
left: 20px;
/* 绝对定位完全脱标的, 它不占有位置 */
}
div:last-child {
width: 200px;
height: 150px;
background-color: purple;
}
</style>
</head>
<body>
<div></div>
<div></div>
绝对定位
- 如果文档可滚动,绝对定位元素会随着它滚动,因为元素最终会相对于正常流的某一部分定位。当position属性的取值为absolute时,可以将元素的定位模式设置为绝对定位。
注意:
绝对定位最重要的一点是,它可以通过边偏移移动位置,但是它完全脱标,完全不占位置。
父级没有定位
<style>
.father {
width: 300px;
height: 300px;
background-color: pink;
margin: 100px;
}
.son {
width: 100px;
height: 100px;
background-color: purple;
position: absolute;
top: 15px;
left: 15px;
/* 绝对定位 父亲没有定位, 孩子以浏览器为基准点对齐 */
}
</style>
</head>
<body>
<div class="father">
<div class="son">孩子跑走了</div>
</div>
</body>
若所有父元素都没有定位,以浏览器为准对齐(document文档)。
父级有定位
<style>
.father {
width: 300px;
height: 300px;
background-color: pink;
margin: 100px;
position: relative; /* 父级有定位 则以父亲为基准点对齐 还可以是 absolute */
}
.son {
width: 100px;
height: 100px;
background-color: purple;
position: absolute;
top: 15px;
left: 15px;
/* 绝对定位 父亲没有定位, 孩子以浏览器为基准点对齐 */
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
绝对定位是将元素依据最近的已经定位(绝对、固定或相对定位)的父元素(祖先)进行定位。
子绝父相
- 子级是绝对定位的话, 父级要用相对定位。
例:
哈根达斯
<style>
div {
width: 310px;
height: 190px;
border: 1px solid #ccc;
margin: 50px auto;
padding: 10px;
position: relative; /* 根据子绝父相最合适 */
}
.topIcon {
position: absolute; /* 绝对定位 不占有位置 */
top: 0;
left: 0; /* 孩子加了绝对定位, 如果父级没有定位,以浏览器为准 */
}
.bottomIcon {
position: absolute;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>
<img src="images/top_tu.gif" alt="" class="topIcon">
<img src="images/adv.jpg" alt="">
<img src="images/br.gif" height="54" width="60" alt="" class="bottomIcon">
</div>
</body>
绝对定位的盒子水平/垂直居中
- 普通的盒子是左右margin 改为 auto就可, 但是对于绝对定位就无效了
- 定位的盒子也可以水平或者垂直居中,有一个算法。
- 首先left 50% 父盒子的一半大小
- 然后走自己外边距负的一半值就可以了 margin-left。
例:
.father {
width: 1000px;
height: 400px;
background-color: pink;
margin: 40px auto;
position: relative;
}
.son {
width: 100px;
height: 40px;
background-color: purple;
position: absolute;
/* margin: 0 auto; 加了绝对定位的盒子,margin 左右auto 就无效了 */
/* left: 400px; */
left:50%; /* left 父盒子宽度的一半 */
margin-left: -50px; /* 左走自己宽度 的一半 */
top: 50%;
margin-top: -20px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
固定定位
- 当position属性的取值为fixed时,即可将元素的定位模式设置为固定定位。
- 当对元素设置固定定位后,它将脱离标准文档流的控制,始终依据浏览器窗口来定义自己的显示位置。不管浏览器滚动条如何滚动也不管浏览器窗口的大小如何变化,该元素都会始终显示在浏览器窗口的固定位置。
固定定位有两点:
- 固定定位的元素跟父亲没有任何关系,只认浏览器。
- 固定定位完全脱标,不占有位置,不随着滚动条滚动。
例:
<style>
body {
height: 3000px;
}
.father {
width: 200px;
height: 200px;
background-color: pink;
margin: 100px auto;
position: relative; /* 父级有定位 */
}
img {
position: fixed; /* 固定定位 */
top: 0;
right: 0;
}
</style>
</head>
<body>
<div class="father">
<img src="images/sun.jpg" width="100" alt="">
</div>
</body>
叠放次序
- 在CSS中,要想调整重叠定位元素的堆叠顺序,可以对定位元素应用z-index层叠等级属性,其取值可为正整数、负整数和0。
注意:
- z-index的默认属性值是0,取值越大,定位元素在层叠元素中越居上。
- 如果取值相同,则根据书写顺序,后来居上。
- 后面数字一定不能加单位。
- 只有相对定位,绝对定位,固定定位有此属性,其余标准流,浮动,静态定位都无此属性,亦不可指定此属性。
例:
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
position: absolute; /* 绝对定位 */
top: 0;
left: 0;
}
div:first-child {
z-index: 1; /* font-weight: 700 */
}
div:nth-child(2) {
background-color: purple;
top: 30px;
left: 30px;
z-index: 2;
}
div:last-child {
background-color: skyblue;
top: 60px;
left: 60px;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
定位模式转换
- 跟 浮动一样, 元素添加了 绝对定位和固定定位之后, 元素模式也会发生转换, 都转换为 行内块模式,
因此 比如 行内元素 如果添加了 绝对定位或者 固定定位后 浮动后,可以不用转换模式,直接给高度和宽度就可以了
例:
<style>
div {
height: 100px;
background-color: pink;
/*float: left; 没给盒子的宽度 浮动的盒子有模式转换的情况 转化为 行内块 宽度位内容的宽度*/
/*position: fixed; 元素添加了 绝对定位和固定定位之后, 元素模式也会发生转换, 都转换为 行内块模式 */
}
span {
background-color: purple;
/* display: block; */
width: 100px;
height: 100px;
/*float: left; 如果盒子本身就需要添加浮动后者绝对固定定位就不需要转换了 */
position: absolute;
}
</style>
</head>
<body>
<div> 稳住,定位不难,我们能赢</div>
<span>我是行内元素</span>
</body>
元素的显示与隐藏
在CSS中有三个显示和隐藏的单词比较常见,我们要区分开,他们分别是display visibility 和 overflow
例:
<style>
div {
width: 100px;
height: 100px;
background-color: pink;
}
div:first-child {
/* display: none;隐藏元素 不是删除 看不见了而已,但是元素一直存在页面中 但是不保留位置 block 显示*/
visibility: hidden; /* 隐藏元素 他和 display none 最大的区别是 他保留位置 visible显示*/
}
div:last-child {
background-color: purple;
}
</style>
</head>
<body>
<div></div>
<div></div>
</body>
display 显示
-
display 设置或检索对象是否及如何显示。
-
display : none 隐藏对象 与它相反的是 display:block 除了转换为块级元素之外,同时还有显示元素的意思。
特点: 隐藏之后,不再保留位置。
visibility 可见性
设置或检索是否显示对象。
- visible : 对象可视
- hidden : 对象隐藏
特点: 隐藏之后,继续保留原有位置。(停职留薪)
例:
<style>
div {
width: 100px;
height: 100px;
background-color: pink;
text-align: center;
line-height: 100px;
margin: 100px auto;
position: relative; /* 子绝父相 */
}
div img {
position: absolute;
left: 110px;
top: 0;
display: none; /* 隐藏二维码 */
}
div:hover img { /* 我们鼠标经过div 的时候, 里面的 那个 img图片会显示出来 */
display: block; /* 显示二维码 */
}
</style>
</head>
<body>
<div>
扫二维码
<img src="images/erweima.png" alt="">
</div>
</body>
overflow 溢出
检索或设置当对象的内容超过其指定高度及宽度时如何管理内容。
- visible : 不剪切内容也不添加滚动条。
- auto : 超出自动显示滚动条,不超出不显示滚动条
- hidden : 不显示超过对象尺寸的内容,超出的部分隐藏掉
- scroll : 不管超出内容否,总是显示滚动条
例:
<head>
<meta charset="utf-8">
<style>
div {
width: 100px;
height: 200px;
border: 1px solid red;
/* overflow: visible; 默认的,超出显示 */
/* overflow: auto; 自动 超出就显示滚动条,不超出不显示 */
/* overflow: scroll; 一直显示滚动条 */
overflow: hidden; /* 溢出隐藏 */
}
</style>
</head>
<body>
<div>
检索或设置当对象的内容超过其指定高度及宽度时如何管理内容
检索或设置当对象的内容超过其指定高度及宽度时如何管理内容
检索或设置当对象的内容超过其指定高度及宽度时如何管理内容
</div>
</body>