CSS(4)__CSS基本用法<三种定位>
一、相对定位
定位有三种,分别是相对定位、绝对定位、固定定位。
相对定位:
1 position:relative;
绝对定位:
1 position:absolute;
固定定位:
1 position:fixed;
每一种定位,都暗藏玄机,所以我们分别讲解。
相对定位:
1 position:relative;
绝对定位:
1 position:absolute;
固定定位:
1 position:fixed;
每一种定位,都暗藏玄机,所以我们分别讲解。
2.1 认识相对定位
相对定位,就是微调元素位置的。让元素相对自己原来的位置,进行位置调整。
div {
width: 200px;
height: 200px;
}
.box1 {
background-color: yellowgreen;
}
.box2 {
background-color: skyblue;
position: relative;
top: 150px;
left: 100px;
}
.box3 {
background-color: orange;
也就是说,如果一个盒子想进行位置调整,那么就要使用相对定位
1 position:relative; → 必须先声明,自己要相对定位了,
2 left:100px; → 然后进行调整。
3 top:150px; → 然后进行调整。
width: 200px;
height: 200px;
}
.box1 {
background-color: yellowgreen;
}
.box2 {
background-color: skyblue;
position: relative;
top: 150px;
left: 100px;
}
.box3 {
background-color: orange;
也就是说,如果一个盒子想进行位置调整,那么就要使用相对定位
1 position:relative; → 必须先声明,自己要相对定位了,
2 left:100px; → 然后进行调整。
3 top:150px; → 然后进行调整。
2.2 不脱标,老家留坑,形影分离
相对定位不脱标,真实位置是在老家,只不过影子出去了,可以到处飘。它原本的位置会一直存在,不会被其他盒子占据。
2.3 相对定位用途
相对定位有坑,所以一般不用于做“压盖”效果。页面中,效果极小。就两个作用:1) 微调元素
2) 做绝对定位的参考,子绝父相(讲绝对定位的时候说)
.txt{
font-size: 30px;
}
.btn{
position: relative;
top: 4px;
left: 0;
}
<p>
<input type="text" class="txt"/>
<input type="button" class="btn" value="我是一个小按钮" />
</p>
2.4 相对定位的定位值
可以用left、right来描述盒子右、左的移动;可以用top、bottom来描述盒子的下、上的移动。
↘:
1 position: relative;
2 top: 10px;
3 left: 40px;
↙:
1 position: relative;
2 right: 100px; → 往左边移动
3 top: 100px;
↖:
1 position: relative;
2 right: 100px;
3 bottom: 100px; → 移动方向是向上。
↗:
1 position: relative;
2 top: -200px; → 负数就是相反的方向,如果是正,就是下边,如果是负数就是上边
3 right: -200px;
↗:
1 position: relative;
2 right: -300px;
3 bottom: 300px;
完全等价于:
4 position: relative;
5 left: 300px;
1 bottom: 300px;
在原本的位置上,距top/bottom/left/right的距离增大100px;
二、绝对定位
绝对定位比相对定位更灵活。(相对于(0,0)点)
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 200px;
}
.box1{
background-color: yellowgreen;
}
.box2{
background-color: skyblue;
position: absolute;
top: 100px;
left: 140px;
}
.box3{
background-color: gold;
}
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 200px;
}
.box1{
background-color: yellowgreen;
}
.box2{
background-color: skyblue;
position: absolute;
top: 100px;
left: 140px;
}
.box3{
background-color: gold;
}
3.1 绝对定位脱标
绝对定位的盒子,是脱离标准文档流的。所以,所有的标准文档流的性质,绝对定位之后都不遵守了。
绝对定位之后,标签就不区分所谓的行内元素、块级元素了,不需要display:block;就可以设置宽、高了:
1 span{
2 position: absolute;
3 top: 100px;
4 left: 100px;
5 width: 100px;
6 height: 100px;
7 background-color: pink;
8 }
绝对定位之后,标签就不区分所谓的行内元素、块级元素了,不需要display:block;就可以设置宽、高了:
1 span{
2 position: absolute;
3 top: 100px;
4 left: 100px;
5 width: 100px;
6 height: 100px;
7 background-color: pink;
8 }
3.2 参考点
绝对定位的参考点,如果用top描述,那么定位参考点就是页面的左上角,而不是浏览器的左上角:
3.3 以盒子为参考点(因为浏览器的大小是不一样的,在浏览器中漂浮在任意位置没有任何意义)
一个绝对定位的元素,如果父辈元素中出现了也定位了的元素,那么将以父辈这个元素,为参考点。
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 10px solid red;
margin: 100px;
position: relative;
}
p{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
top: 40px;
left: 40px;
}
<div>
<p></p>
</div>
● 要听最近的已经定位的祖先元素的,不一定是父亲,可能是爷爷:
*{
margin: 0;
padding: 0;
}
.box1{
width: 400px;
height: 400px;
padding: 100px;
border: 10px solid red;
margin: 100px;
position: relative;
}
.box2{
width: 300px;
height: 300px;
border: 50px solid blue;
}
p{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
top: 40px;
left: 40px;
}
<div class="box1">
<div class="box2">
<p></p>
</div>
</div>
<div class="box1"> → 相对定位
<div class="box2"> → 没有定位
<p></p> → 绝对定位,将以box1为参考,因为box2没有定位,box1就是最近的父辈元素
</div>
</div>
<div class="box1"> → 相对定位
<div class="box2"> → 相对定位
<p></p> → 绝对定位,将以box2为参考,因为box2是自己最近的父辈元素
</div>
</div>
● 不一定是相对定位,任何定位,都可以作为参考点
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 10px solid red;
position: absolute;
top: 100px;
left: 100px;
}
p{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
top: 40px;
left: 40px;
}
1 <div> → 绝对定位
2 <p></p> → 绝对定位,将以div作为参考点。因为父亲定位了。
3 </div>
子绝父绝、子绝父相、子绝父固,都是可以给儿子定位的。但是,工程上子绝、父绝,没有一个盒子在标准流里面了,所以页面就不稳固,没有任何实战用途。工程上,“子绝父相”有意义,父亲没有脱标,儿子脱标在父亲的范围里面移动。
1 <div class=”box1”> → 绝对定位
2 <div class=”box2”> → 相对定位
3 <div class=”box3”> → 没有定位
4 <p></p> → 绝对定位,以box2为参考定位。
5 </div>
6 </div>
7 </div>
● 绝对定位的儿子,无视参考的那个盒子的padding。
下图中,绿色部分是div的padding,蓝色部分是div的内容区域。那么此时,div相对定位,p绝对定位。
p将无视父亲的padding,在border内侧为参考点,进行定位:
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 10px solid red;
margin: 100px;
position: relative;
}
p{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
top: 40px;
left: 40px;
}
<div>
<p></p>
</div>
● 要听最近的已经定位的祖先元素的,不一定是父亲,可能是爷爷:
*{
margin: 0;
padding: 0;
}
.box1{
width: 400px;
height: 400px;
padding: 100px;
border: 10px solid red;
margin: 100px;
position: relative;
}
.box2{
width: 300px;
height: 300px;
border: 50px solid blue;
}
p{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
top: 40px;
left: 40px;
}
<div class="box1">
<div class="box2">
<p></p>
</div>
</div>
<div class="box1"> → 相对定位
<div class="box2"> → 没有定位
<p></p> → 绝对定位,将以box1为参考,因为box2没有定位,box1就是最近的父辈元素
</div>
</div>
<div class="box1"> → 相对定位
<div class="box2"> → 相对定位
<p></p> → 绝对定位,将以box2为参考,因为box2是自己最近的父辈元素
</div>
</div>
● 不一定是相对定位,任何定位,都可以作为参考点
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 400px;
border: 10px solid red;
position: absolute;
top: 100px;
left: 100px;
}
p{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
top: 40px;
left: 40px;
}
1 <div> → 绝对定位
2 <p></p> → 绝对定位,将以div作为参考点。因为父亲定位了。
3 </div>
子绝父绝、子绝父相、子绝父固,都是可以给儿子定位的。但是,工程上子绝、父绝,没有一个盒子在标准流里面了,所以页面就不稳固,没有任何实战用途。工程上,“子绝父相”有意义,父亲没有脱标,儿子脱标在父亲的范围里面移动。
1 <div class=”box1”> → 绝对定位
2 <div class=”box2”> → 相对定位
3 <div class=”box3”> → 没有定位
4 <p></p> → 绝对定位,以box2为参考定位。
5 </div>
6 </div>
7 </div>
● 绝对定位的儿子,无视参考的那个盒子的padding。
下图中,绿色部分是div的padding,蓝色部分是div的内容区域。那么此时,div相对定位,p绝对定位。
p将无视父亲的padding,在border内侧为参考点,进行定位:
* {
margin: 0;
padding: 0;
}
div {
width: 200px;
height: 200px;
border: 10px solid red;
padding: 150px;
position: relative;
margin: 100px;
}
p {
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
top: 40px;
left: 40px;
}
<div>
字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字
<p></p>
</div>
3.4 绝对定位的盒子居中
绝对定位之后,所有标准流的规则,都不适用了。所以margin:0 auto;失效。1 width: 600px;
2 height: 60px;
position: absolute;
3 left: 50%;
4 top: 0;
5 margin-left: -300px; → 宽度的一半
非常简单,当做公式记忆下来。就是left:50%; margin-left:负的宽度的一半。
2 height: 60px;
position: absolute;
3 left: 50%;
4 top: 0;
5 margin-left: -300px; → 宽度的一半
非常简单,当做公式记忆下来。就是left:50%; margin-left:负的宽度的一半。
三、固定定位
固定定位,就是相对浏览器窗口定位。页面如何滚动,这个盒子显示的位置不变。
固定定位脱标!
p{
width: 100px;
height: 100px;
background-color: orange;
position: fixed;
top: 100px;
left: 100px;
}
固定定位脱标!
p{
width: 100px;
height: 100px;
background-color: orange;
position: fixed;
top: 100px;
left: 100px;
}
4.1 固定定位的用途:返回顶部按钮
.backtop{position: fixed;
bottom: 100px;
right: 30px;
width: 60px;
height: 60px;
background-color: gray;
text-align: center;
line-height:30px;
color:white;
text-decoration: none;
}
<a class="backtop">返回<br />顶部</a>
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
4.2 固定定位的用途:顶部导航条的固定
* {
margin: 0;
padding: 0;
}
.nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #333;
z-index: 99999999;
}
.inner_c {
width: 1000px;
height: 60px;
margin: 0 auto;
}
.inner_c ul {
list-style: none;
}
.inner_c ul li {
float: left;
width: 100px;
height: 60px;
text-align: center;
line-height: 60px;
}
.inner_c ul li a {
display: block;
width: 100px;
height: 60px;
color: white;
text-decoration: none;
}
.inner_c ul li a:hover {
background-color: gold;
}
<div class="nav">
<div class="inner_c">
<ul>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
</ul>
</div>
</div>
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
margin: 0;
padding: 0;
}
.nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #333;
z-index: 99999999;
}
.inner_c {
width: 1000px;
height: 60px;
margin: 0 auto;
}
.inner_c ul {
list-style: none;
}
.inner_c ul li {
float: left;
width: 100px;
height: 60px;
text-align: center;
line-height: 60px;
}
.inner_c ul li a {
display: block;
width: 100px;
height: 60px;
color: white;
text-decoration: none;
}
.inner_c ul li a:hover {
background-color: gold;
}
<div class="nav">
<div class="inner_c">
<ul>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
<li><a href="#">网页栏目</a></li>
</ul>
</div>
</div>
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
<img src="images/2.jpg" alt="" />
四、z-index
● z-index值表示谁压着谁。数值大的压盖住数值小的。
● 只有定位了的元素,才能有z-index值。也就是说,不管相对定位、绝对定位、固定定位,都可以使用z-index值。而浮动的东西不能用。
● z-index值没有单位,就是一个正整数。默认的z-index值是0。
● 如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。定位了的元素,永远能够压住没有定位的元素。
● 从父现象:父亲怂了,儿子再牛逼也没用。
没有单位:
1 z-index: 988;
Z-index值大的压住小的。
*{
margin: 0;
padding: 0;
}
.box1{
width: 200px;
height: 200px;
background: yellowgreen;
position: absolute;
top: 100px;
left: 100px;
z-index: 444;
}
.box2{
width: 200px;
height: 200px;
background: skyblue;
position: absolute;
top: 180px;
left: 180px;
z-index: 333;
}
<div class="box1">绿</div>
<div class="box2">蓝</div>
如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。
* {
margin: 0;
padding: 0;
}
.box1 {
width: 200px;
height: 200px;
background: yellowgreen;
}
.box2 {
width: 200px;
height: 200px;
background: skyblue;
position: relative;
top: 100px;
left: 30px;
z-index: 999;
}
.box3 {
width: 200px;
height: 200px;
background: pink;
/*为了z-index值生效,必须加上一个定位:*/
position: relative;
top: 0;
left: 0;
}
从父现象:父亲怂了,儿子再牛逼也没用。
* {
margin: 0;
padding: 0;
}
.shayi {
width: 200px;
height: 200px;
background-color: blue;
position: relative;
z-index: 10;
}
.tianliang {
width: 200px;
height: 200px;
background-color: orange;
position: relative;
z-index: 9;
}
.anji {
width: 60px;
height: 60px;
background-color: green;
position: absolute;
top: 300px;
left: 450px;
z-index: 454;
}
.liangzai {
width: 60px;
height: 60px;
background-color: pink;
position: absolute;
top: 130px;
left: 490px;
z-index: 45454;
}
<div class="shayi">沙溢
<p class="anji">安吉</p>
</div>
<div class="tianliang">田亮
<p class="liangzai">亮仔</p>
</div>
● 只有定位了的元素,才能有z-index值。也就是说,不管相对定位、绝对定位、固定定位,都可以使用z-index值。而浮动的东西不能用。
● z-index值没有单位,就是一个正整数。默认的z-index值是0。
● 如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。定位了的元素,永远能够压住没有定位的元素。
● 从父现象:父亲怂了,儿子再牛逼也没用。
没有单位:
1 z-index: 988;
Z-index值大的压住小的。
*{
margin: 0;
padding: 0;
}
.box1{
width: 200px;
height: 200px;
background: yellowgreen;
position: absolute;
top: 100px;
left: 100px;
z-index: 444;
}
.box2{
width: 200px;
height: 200px;
background: skyblue;
position: absolute;
top: 180px;
left: 180px;
z-index: 333;
}
<div class="box1">绿</div>
<div class="box2">蓝</div>
如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。
* {
margin: 0;
padding: 0;
}
.box1 {
width: 200px;
height: 200px;
background: yellowgreen;
}
.box2 {
width: 200px;
height: 200px;
background: skyblue;
position: relative;
top: 100px;
left: 30px;
z-index: 999;
}
.box3 {
width: 200px;
height: 200px;
background: pink;
/*为了z-index值生效,必须加上一个定位:*/
position: relative;
top: 0;
left: 0;
}
从父现象:父亲怂了,儿子再牛逼也没用。
* {
margin: 0;
padding: 0;
}
.shayi {
width: 200px;
height: 200px;
background-color: blue;
position: relative;
z-index: 10;
}
.tianliang {
width: 200px;
height: 200px;
background-color: orange;
position: relative;
z-index: 9;
}
.anji {
width: 60px;
height: 60px;
background-color: green;
position: absolute;
top: 300px;
left: 450px;
z-index: 454;
}
.liangzai {
width: 60px;
height: 60px;
background-color: pink;
position: absolute;
top: 130px;
left: 490px;
z-index: 45454;
}
<div class="shayi">沙溢
<p class="anji">安吉</p>
</div>
<div class="tianliang">田亮
<p class="liangzai">亮仔</p>
</div>
总结:
1:margin的塌陷现象:标准文档流中,竖直方向的margin不会重叠,以较大的为准
2:当两个盒子浮动后,margin就会叠加(面试题)
3:标准文档流,盒子居中:margin:0 auto
4:定位:
1:相对定位:标准文档流,老家留坑,形影分离,参考点是本身 positon:relative
2:绝对定位:position:absolute
A:脱标,如果是top
B:参考点是页面左上角 如果是bottom,首屏的左下角
C:以盒子为参考点,盒子必须定位
子绝父相?
父亲需要占位子,所以是相对定位,儿子需要到处飘,压盖现象,所以需要绝对定位
D:定位的儿子会无视父亲的padding
3:固定定位 position:fixed,固定在页面的某个位置
用途:返回顶部按钮,顶部导航栏
4:z-index
A:值大的压住小的
B:值没有单位
C:只有定位的元素才可以设置z-index
D:从父现象,父亲的值如果小,儿子再大也没用