CSS(5)
来源:互联网 发布:mysql编程题及答案 编辑:程序博客网 时间:2024/06/08 04:11
相对定位
定位有三种,分别是相对定位、绝对定位、固定定位。
相对定位: position:relative;
绝对定位: position:absolute;
固定定位: position:fixed;
认识相对定位
相对定位,就是微调元素位置的。让元素相对自己原来的位置,进行位置调整。
也就是说,如果一个盒子想进行位置调整,那么就要使用相对定位
position:relative; → 必须先声明,自己要相对定位了, left:100px; → 然后进行调整。 top:150px; → 然后进行调整。
不脱标,老家留坑,形影分离
相对定位不脱标,真实位置是在老家,只不过影子出去了,可以到处飘。
相对定位用途
相对定位有坑,所以一般不用于做“压盖”效果。页面中,效果极小。就两个作用:
1) 微调元素
2) 做绝对定位的参考,子绝父相(讲绝对定位的时候说)
相对定位的定位值
可以用left、right来描述盒子右、左的移动;
可以用top、bottom来描述盒子的下、上的移动。
可以发现描述与盒子的位置刚好相反,因为描述是盒子的相对位置。
↘:
position: relative;top: 10px;left: 40px;
↙:
position: relative;right: 100px; → 往左边移动top: 100px;
↖:
position: relative; right: 100px;bottom: 100px; → 移动方向是向上。
↗:
position: relative;top: -200px; → 负数就是相反的方向,如果是正,就是下边,如果是负数就是上边right: -200px;
↗:
position: relative;right: -300px;bottom: 300px;
完全等价于:
position: relative;left: 300px;bottom: 300px;
如图,有几种相对定位的移动方法?
方法1:
position:relative;top:100px;left:200px;
方法2:
position:relative;bottom:-100px;right:-200px;
方法3:
position:relative;top:100px;right:-200px;
方法4:
position:relative;bottom:-100px;left:200px;
绝对定位
绝对定位比相对定位更灵活。
绝对定位脱标
绝对定位的盒子,是脱离标准文档流的。所以,所有的标准文档流的性质,绝对定位之后都不遵守了。
绝对定位之后,标签就不区分所谓的行内元素、块级元素了,不需要display:block;就可以设置宽、高了:
1 span{2 position: absolute;3 top: 100px;4 left: 100px;5 width: 100px;6 height: 100px;7 background-color: pink;8 }
参考点
绝对定位的参考点,如果用top描述,那么定位参考点就是页面的左上角,而不是浏览器的左上角:
如果用bottom描述,那么就是浏览器首屏窗口尺寸,对应的页面的左下角:
面试题:
答案:
用bottom的定位的时候,参考的是浏览器首屏大小对应的页面左下角。
以盒子为参考点
一个绝对定位的元素,如果父辈元素中出现了也定位了的元素,那么将以父辈这个元素,为参考点。
要听最近的已经定位的祖先元素的,不一定是父亲,可能是爷爷:
<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>
● 不一定是相对定位,任何定位,都可以作为参考点
<div> → 绝对定位 <p></p> → 绝对定位,将以div作为参考点。因为父亲定位了。</div>
子绝父绝、子绝父相、子绝父固,都是可以给儿子定位的。但是,工程上子绝、父绝,没有一个盒子在标准流里面了,所以页面就不稳固,没有任何实战用途。工程上,“子绝父相”有意义,父亲没有脱标,儿子脱标在父亲的范围里面移动。
<div class=”box1”> → 绝对定位 <div class=”box2”> → 相对定位 <div class=”box3”> → 没有定位 <p></p> → 绝对定位,以box2为参考定位。 </div> </div></div>
● 绝对定位的儿子,无视参考的那个盒子的padding。
下图中,绿色部分是div的padding,蓝色部分是div的内容区域。那么此时,div相对定位,p绝对定位。
p将无视父亲的padding,在border内侧为参考点,进行定位:
绝对定位的盒子居中
绝对定位之后,所有标准流的规则,都不适用了。所以margin:0 auto;失效。
width: 600px;height: 60px;position: absolute;left: 50%;top: 0;margin-left: -300px; → 宽度的一半
非常简单,当做公式记忆下来。就是left:50%;
margin-left:负的宽度的一半
。
固定定位
固定定位,就是相对浏览器窗口定位。页面如何滚动,这个盒子显示的位置不变。
固定定位脱标!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <style type="text/css"> p{ width: 100px; height: 100px; background-color: orange; position: fixed; top: 100px; left: 100px; } </style></head><body> <p></p> <img src="images/2.jpg" alt="" /></body></html>
z-index
● z-index 值表示谁压着谁。数值大的压盖住数值小的。
● 只有定位了的元素,才能有z-index值。也就是说,不管相对定位、绝对定位、固定定位,都可以使用z-index值。而浮动的东西不能用。
● z-index值没有单位,就是一个正整数。默认的z-index值是0。
● 如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。定位了的元素,永远能够压住没有定位的元素。
● 从父现象:父亲怂了,儿子再牛逼也没用。
没有单位: z-index: 988;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <style type="text/css"> *{ 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; } </style></head><body> <div class="linzhiying"> <p class="kimi"></p> </div> <div class="tianliang"> <p class="cindy"></p> </div></body></html>
- CSS-5
- css-5
- CSS(5)
- HTML+CSS关于CSS选择器 (5)
- CSS入门(5)-CSS+DIV
- CSS基础学习5-CSS设置链接
- CSS入门(5)--CSS中的超链接a
- HTML+CSS关于CSS选择器 (5)
- CSS速成手册(5)
- CSS速成手册(5)
- 5个CSS样式
- div+css (5)
- 5天精通css
- CSS声音IE5.5
- 5个CSS选择器
- 学习CSS(5)
- CSS学习<5>
- 前端笔记 CSS 5
- C++中typedef、const、预处理等相关知识总结
- 关于从客户端中检测到有潜在危险的 Request.Form 值
- linux下 如何切换到root用户
- Android 5.1 Material Design 中Color 设置
- CF854C planning 贪心题,维护堆; 送棵线段树
- CSS(5)
- 【CUGBACM15级BC第32场 A】hdu 5182 PM2.5
- 1094:多输入输出练习2
- Linux 技巧:让进程在后台可靠运行的几种方法
- 深度卷积网络CNN与图像语义分割
- DrawerLayout 侧滑
- 51Nod- 1006 最长公共子序列Lcs(动态规划)
- 关于ffmpeg的一个bug长期求解答
- Elasticsearch&logstash&filebeat&kibana&x-pack搭建