首页 > Web开发 > 详细

CSS中的z-index属性如何使用

时间:2020-01-06 09:34:39      阅读:81      评论:0      收藏:0      [点我收藏+]
原文:CSS中的z-index属性如何使用

z-index属性介绍#

  • 只有设置了定位我们才会使用到该z-index属性,如:固定定位相对定位绝对定位
  • 定位元素默认的z-index属性值是0
  • 如果2个定位的元素都没有设置z-index属性,后者会覆盖到前者。
  • 如果2个定位的元素都设置了z-index属性,并且数值一样大还是后者会覆盖到前者。
  • z-index属性的属性值大的就会覆盖小,就是设置元素的层级。

z-index属性实践#

  • 用实践证明这句话,如果2个定位的元素都没有设置z-index属性,后者会覆盖到前者。

  • 代码块

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>z-index属性</title> <style> div{ width: 200px; height: 200px; } .div1{ background-color: red; position: relative; top: 50px; left: 50px; } .div2{ background-color: slateblue; position: relative; left: 100px; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> </body> </html>
  • 结果图

技术分享图片

  • 用实践来证明这句话,如果2个定位的元素都设置了z-index属性,并且数值一样大还是后者会覆盖到前者。
  • 代码块

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>z-index属性</title> <style> div{ width: 200px; height: 200px; } .div1{ background-color: red; position: relative; top: 50px; left: 50px; z-index: 2; } .div2{ background-color: slateblue; position: relative; left: 100px; z-index: 2; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> </body> </html>
  • 结果图

技术分享图片

  • 用实践来证明这句话,z-index属性的属性值大的就会覆盖小,就是设置元素的层级。

  • 代码块

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>z-index属性</title> <style> div{ width: 200px; height: 200px; } .div1{ background-color: red; position: relative; top: 50px; left: 50px; z-index: 3; } .div2{ background-color: slateblue; position: relative; left: 100px; z-index: 2; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> </body> </html>
  • 结果图

技术分享图片

CSS中的z-index属性如何使用

原文:https://www.cnblogs.com/lonelyxmas/p/12154655.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!