首页 / CSS / 深入理解及应用Position
深入理解及应用Position
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了深入理解及应用Position,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2071字,纯文字阅读大概需要3分钟。
内容图文
![深入理解及应用Position](/upload/InfoBanner/zyjiaocheng/416/cfa5fbb67ad74628b246589c6cf109fc.jpg)
Position本不复杂,混淆应用比较难理解,主要规则如下:
脱离文档流
除 static属性值之外,其他值都会使元素脱离文档流(float也会导致元素脱离文档流)。
对 Width、height的影响
1) Absolute的参考点为最近可作为参考点的父元素(position为absolute、relative、fixed的元素)、fixed的参考点浏览器窗口、relative的参考点为元素正常位置。
2) 元素本身值为inherit时
a) 当父级元素的Width和height值为数值时,元素继承父级元素的完整高度,并以最近参考点作为参考。
.wrap{ position: relative; width: 500px; height: 300px; border: 1px solid red; } .cont{ background: gray; width: 150px; overflow: hidden; } .txt{ background: yellow; width: 230px; height: inherit; } .banner{ background: pink; width: 50%; height: inherit; } .txt-cont{ position: absolute; background: darkblue; width: inherit; color: white; }
conttxtxtxttxt-cont
b) 当父级元素的Width和height值为百分比时,以参考点元素的宽、高* 百分比来计算。
.wrap{ position: relative; width: 500px; height: 300px; border: 1px solid red; } .cont{ background: gray; width: 150px; overflow: hidden; } .txt{ background: yellow; width: 50%; height: inherit; } .banner{ background: pink; width: 50%; height: inherit; } .txt-cont{ position: absolute; background: darkblue; width: inherit; color: white; }
conttxttxt-cont
3) 元素本身为百分比时(50%)
此种情况下,无论父级元素的width和height是数值,还是百分比都不会造成对元素自身的影响,元素自身还是会以参考进行相应的计算。
.wrap{ position: relative; width: 500px; height: 300px; border: 1px solid red; } .cont{ background: gray; width: 150px; overflow: hidden; } .txt{ background: yellow; width: 50%; height: inherit; } .banner{ background: pink; width: 50%; height: inherit; } .txt-cont{ position: absolute; background: darkblue; width: 100%; color: white; }
conttxttxt-cont
定位后的默认位置
Fixed和absolute属性后的默认位置都是在原地,只是紧跟后面折正常文档流元素会顶上来,被定位元素盖住。
他与z-index无解的关系
z-index的详细介绍见后面章节,此处只指出position除static值外都会创建层叠上下文(z-index不为auto的时候)。
1) z-index为数值时,会创建层叠上下文,子元素层叠顺序以此做为参考。
2) z-index为auto时,不会创建层叠上下文,层叠顺序与z-index:0一致。
内容总结
以上是互联网集市为您收集整理的深入理解及应用Position全部内容,希望文章能够帮你解决深入理解及应用Position所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。