1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > html固定按钮相对位置 CSS基础之相对定位 绝对定位 固定定位 z-index

html固定按钮相对位置 CSS基础之相对定位 绝对定位 固定定位 z-index

时间:2018-07-31 03:11:39

相关推荐

html固定按钮相对位置 CSS基础之相对定位 绝对定位 固定定位 z-index

1.相对定位

1.1.相对定位,就是微调元素位置的。让元素相对自己原来的位置,进行位置调整。也就是说,如果一个盒子想进行位置调整,那么就要使用相对定位

a position:relative; → 必须先声明,自己要相对定位了

b left:100px; → 然后进行调整。

c top:150px; → 然后进行调整。

Document

div{

width: 200px;

height: 200px;

}

.box1{

background-color: yellowgreen;

}

.box2{

background-color: skyblue;

position: relative;

top: 150px;

left: 100px;

}

.box3{

background-color: orange;

}

图片.png

1.2.不脱标,老家留坑,形影分离,相对定位不脱标,真实位置是在老家,只不过影子出去了,可以到处飘。

图片.png

1.3.相对定位的用途:相对定位有坑,所以一般不用于做“压盖”效果。页面中,效果极小。就两个作用:

a:微调元素

b:做绝对定位的参考,子绝父相

1.4.相对定位的定位值

a:可以用left、right来描述盒子右、左的移动,相当于marginLeft ,marginRight

b:可以用top、bottom来描述盒子的下、上的移动,marginTop,marginBottom

图片.png

图片.png

图片.png

2.绝对定位

2.1.绝对定位的盒子,是脱离标准文档流的。所以,所有的标准文档流的性质,绝对定位之后都不遵守了。绝对定位之后,标签就不区分所谓的行内元素、块级元素了,不需要display:block;就可以设置宽、高了:

Document

*{

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;

}

图片.png

2.2.参考点,如果用top描述,那么定位参考点就是页面的左上角,而不是浏览器的左上角,如果用bottom描述,那么就是对应的页面的左下角,当页面滚动时,这个盒子显示的位置也跟者滚动。绝对定位脱标!:

Document

div{

width: 100px;

height: 100px;

background-color: blue;

position: absolute;

bottom: 100px;

left: 100px;

}

图片.png

2.3. 以盒子为参考点

a:一个绝对定位的元素,如果父辈元素中出现了也定位了的元素,那么将以父辈这个元素,为参考点。

Document

*{

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;

}

图片.png

b: 不一定是相对定位,任何定位,都可以作为参考点,子绝父绝、子绝父相、子绝父固,都是可以给儿子定位的。但是,工程上子绝、父绝,没有一个盒子在标准流里面了,所以页面就不稳固,没有任何实战用途。工程上,“子绝父相”有意义,父亲没有脱标,儿子脱标在父亲的范围里面移动。

Document

*{

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;

}

图片.png

2.4.绝对定位的盒子居中

Document

div{

width: 400px;

height: 60px;

background-color: green;

position: absolute;

left: 50%;

margin-left: -200px;

}

图片.png

3.固定定位

3.1.固定定位,就是相对浏览器窗口定位。页面如何滚动,这个盒子显示的位置不变。固定定位脱标!

Document

p{

width: 100px;

height: 100px;

background-color: orange;

position: fixed;

top: 100px;

left: 100px;

}

图片.png

Document

*{

margin: 0;

padding: 0;

}

body{

/*为什么要写这个?*/

/*不希望我们的页面被nav挡住*/

padding-top: 60px;

/*IE6不兼容固定定位,所以这个padding没有什么用,就去掉就行了*/

_padding-top: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;

}

p{

font-size: 30px;

}

.btn{

display: block;

width: 120px;

height: 30px;

background-color: orange;

position: relative;

top: 2px;

left: 1px;

}

网页栏目网页栏目网页栏目网页栏目网页栏目网页栏目网页栏目网页栏目网页栏目网页栏目

按钮

图片.png

4.z-index值

● z-index值表示谁压着谁。数值大的压盖住数值小的。

● 只有定位了的元素,才能有z-index值。也就是说,不管相对定位、绝对定位、固定定位,都可以使用z-index值。而浮动的东西不能用。

● z-index值没有单位,就是一个正整数。默认的z-index值是0。

● 如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。定位了的元素,永远能够压住没有定位的元素。

● 从父现象:父亲怂了,儿子再牛逼也没用。

Document

*{

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;

}

绿 蓝

图片.png

如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。

Document

*{

margin: 0;

padding: 0;

}

.box1{

width: 200px;

height: 200px;

background: yellowgreen;

}

.box2{

width: 200px;

height: 200px;

background: skyblue;

position: relative;

top: 100px;

left: 30px;

}

.box3{

width: 200px;

height: 200px;

background: pink;

/*为了z-index值生效,必须加上一个定位:*/

position: relative;

top: 0;

left: 0;

z-index: 999;

}

绿 蓝 粉

图片.png

从父现象:父亲怂了,儿子再牛逼也没用。

Document

*{

margin: 0;

padding: 0;

}

.linzhiying{

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;

}

.kimi{

width: 60px;

height: 60px;

background-color: green;

position: absolute;

top: 300px;

left: 450px;

z-index: 454;

}

.cindy{

width: 60px;

height: 60px;

background-color: pink;

position: absolute;

top: 130px;

left: 490px;

z-index: 45454;

}

图片.png

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。