css z-index的层级关系
2024-08-25 03:49:05
定义和用法
z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。
注释:元素可拥有负的 z-index 属性值。
注释:Z-index 仅能在定位元素上奏效(例如 position:absolute;)
说明
该属性设置一个定位元素沿 z 轴的位置,z 轴定义为垂直延伸到显示区的轴。如果为正数,则离用户更近,为负数则表示离用户更远。
优先关系
如果同时出现两个相同的z-index值,那么在后面的元素层级会高于前面的元素。例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>z-index</title>
</head>
<style type="text/css">
.first{width:200px; height: 500px; position: absolute; background:#00f; z-index: 100;}
.second{width:250px; height: 250px; position: absolute; background:#f33; z-index: 100;}
</style>
<body>
<div class="first">first</div>
<div class="second">second</div>
</body>
</html>
类名为second的div会在类名为first的div上面。
z-index属性是区分所有兄弟元素的高度,并不是所有页面元素的高度;例如:
<style type="text/css">
.first{width:200px; height: 500px; position: absolute; top: 0; background:#00f; z-index: 80;}
.second{width:250px; height: 250px; position: absolute; top: 50px; background:#f33; z-index: 100;}
.first-child{width:250px; height: 250px; position: absolute; top: 0; background:#333; z-index: 101;}
</style>
<body>
<div class="first">first
<div class="first-child">first-child
</div>
</div>
<div class="second">second</div>
</body>
类名为first-child的div元素的z-index大于类名为second的div元素的z-index;但页面显示效果first-child位于second下面
最新文章
- sprint3冲刺第一天
- c#面向对象基础 封装、继承
- DMSFrame 之SqlCacheDependency(一)
- android学习笔记24——事件处理
- Java开发的一个简单截屏工具
- WCF大数据量传输解决方案
- 【转】通用分页用户控件(DataGrid,DataList,Repeater都可以用它来分页)
- oracle 12c 学习系列(1)–12c初体验
- QML Flipable、Flickable和状态与动画 上篇
- 39. leetcode 326. Power of Three
- C-图文上边对齐
- Pythonh中的zip()与*zip()函数详解
- 设置eclipse全局编码格式
- JavaScript调试总结
- SpringCloud(一)Eureka注册中心
- board_led.h/board_led.c
- 从github checkout子文件夹
- Oracle使用par文件进行全库导入导出
- style标签下的CSS代码的显示与实时编辑
- php中array_map和array_walk的使用对比_php技巧