
做过页面布局的同学对z-index属性应该是很熟悉了,z-index是针对网页显示中的一个特殊属性。因为显示器是显示的图案是一个二维平面,拥有x轴和y轴来表示位置属性。为了表示三维立体的概念如显示元素的上下层的叠加顺序引入了z-index属性来表示z轴的区别。表示一个元素在叠加顺序上的上下立体关系。
z-index值较大的元素将叠加在z-index值较小的元素之上。对于未指定此属性的定位对象,z-index 值为正数的对象会在其之上,而 z-index 值为负数的对象在其之下。(推荐教程:CSS视频教程)
简单演示
<div></div> <div><div> <p>两个p,第二个向上移动50px,正常情况应该是这样的</p> <p><img style="max-width:90%" title="image" src="https://img.php.cn/upload/article/000/000/024/68d391c7a77b76da64f82671f8e22ded-0.png?x-oss-process=image/resize,p_40" alt="image" style="max-width:90%" style="max-width:90%" border="0"></p> <p>第二个p遮住了第一个p,对第二个添加z-index属性</p> <pre class="brush:js;toolbar:false;"><div></div> <div><div> <p>结果就会变成这个样子,z-index 最简单的应用就是这样</p> <p><img style="max-width:90%" title="image" src="https://img.php.cn/upload/article/000/000/024/68d391c7a77b76da64f82671f8e22ded-1.png?x-oss-process=image/resize,p_40" alt="image" style="max-width:90%" style="max-width:90%" border="0"> </p> <h3><strong>只对定位元素有效</strong></h3> <p>z-index属性适用于定位元素(position属性值为 relative 或 absolute 或 fixed的对象),用来确定定位元素在垂直于显示屏方向(称为Z轴)上的层叠顺序,也就是说如果元素是没有定位的,对其设置的z-index会是无效的。</p> <pre class="brush:php;toolbar:false;"><div></div> <div><div> <p>虽然第一个p的z-index比第二个p大,但是由于第一个p未定位,其z-index属性未起作用,所以仍然会被第二个p覆盖。</p> <p><img style="max-width:90%" title="image" src="https://img.php.cn/upload/article/000/000/024/1e4fe8e9378310c98376898c90b4454c-2.png?x-oss-process=image/resize,p_40" alt="image" style="max-width:90%" style="max-width:90%" border="0"></p> <h3><strong>相同z-index谁上谁下</strong></h3> <p>相同的z-index其实有两种情况</p> <p>1.如果两个元素都没有定位发生位置重合现象或者两个都已定位元素且z-index相同发生位置重合现象,那么按文档流顺序,后面的覆盖前面的。</p> <pre class="brush:js;toolbar:false;"><div></div> <div><div> <p><img style="max-width:90%" title="image" src="https://img.php.cn/upload/article/000/000/024/1e4fe8e9378310c98376898c90b4454c-2.png?x-oss-process=image/resize,p_40" alt="image" style="max-width:90%" style="max-width:90%" border="0"></p> <p>2.如果两个元素都没有设置z-index,使用默认值,一个定位一个没有定位,那么定位元素覆盖未定位元素</p> <pre class="brush:js;toolbar:false;"><div></div> <div><div> <p><img style="max-width:90%" title="image" src="https://img.php.cn/upload/article/000/000/024/1e4fe8e9378310c98376898c90b4454c-4.png?x-oss-process=image/resize,p_40" alt="image" style="max-width:90%" style="max-width:90%" border="0"></p> <h3><strong>父子关系处理</strong></h3> <p>如果父元素z-index有效,那么子元素无论是否设置z-index都和父元素一致,会在父元素上方</p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13837" title="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器"><img src="https://img.php.cn/upload/skill/001/246/273/6adaf20d3191a227.jpg" alt="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/xiazai/skill13837" title="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器" class="overflowclass">htmlq 命令行 HTML 内容提取器,支持 CSS 选择器</a> <p class="overflowclass">htmlq 是一个命令行工具,使用 CSS 选择器从 HTML 中提取内容,相当于 jq 的 HTML 版本。该工具使用 Rust 编写,允许你通过 CSS 选择器将 HTML 管道化,直接从终端提取文本、属性和结构化内容。</p> </div> <a rel="nofollow" href="/xiazai/skill13837" title="htmlq 命令行 HTML 内容提取器,支持 CSS 选择器" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div> <pre class="brush:php;toolbar:false;"><div> <div> <div> </div> <p>虽然子元素设置z-index比父元素小,但是子元素仍然出现在父元素上方</p> <p><img style="max-width:90%" title="image" src="https://img.php.cn/upload/article/000/000/024/1e4fe8e9378310c98376898c90b4454c-5.png?x-oss-process=image/resize,p_40" alt="image" style="max-width:90%" style="max-width:90%" border="0"></p> <p>如果父元素z-index失效(未定位或者使用默认值),那么定位子元素的z-index设置生效</p> <pre class="brush:js;toolbar:false;"><div> <div> <div> </div> <p>子元素z-index=-5生效,被父元素覆盖</p> <p><img style="max-width:90%" title="image" src="https://img.php.cn/upload/article/000/000/024/47690caef5778a54abbd38b793ffaefb-6.png?x-oss-process=image/resize,p_40" alt="image" style="max-width:90%" style="max-width:90%" border="0"></p> <h3><strong>兄弟之间子元素</strong></h3> <p>如果兄弟元素的z-index生效,那么其子元素覆盖关系有父元素决定</p> <pre class="brush:js;toolbar:false"><div> <div></div> </div> <div> <div></div> </div>
虽然第一个p的子元素的z-index比较高,但是由于其父元素z-index比第二个p低,所以第一个p子元素会被第二个p及其子元素覆盖

应用
经常会有这样一种错误在table中最后各行一个td放一个p,点击弹出子菜单做一些删除、修改什么的操作,但是每次弹出的菜单都会被下面各行的p覆盖,像下面这张图一样,弹出的菜单没有在页面最上方。

写个简单的例子看看
html>
<title>Test</title>
<style>
html,body
{
height:100%;
width:100%;
padding:0;
margin:0;
}
.menu
{
background-color:#0e0;
position:relative;
z-index:10;
}
.options
{
display:none;
position:absolute;
top:
z-index:30;
}
.options div
{
background-color:#00e;
}
</style>
| Name | Age | Options |
|---|---|---|
| Byron | 24 |
Options
Opion1
Opion2
Opion3
Opion4
|
| Byron | 24 |
Options
Opion1
Opion2
Opion3
Opion4
|
| Byron | 24 |
Options
Opion1
Opion2
Opion3
Opion4
|
| 期望样式 | @@##@@ | 实际样式 | @@##@@ |
这时候习惯于增大options 的z-index却发现于事无补,为什么呢?因为每个menu的z-index相同,它们的层叠顺序按文档流顺序,无论子元素z-index调到多大,上面menu的options还是会被下面menu遮盖。这时候我的做法一般是把options放到外面,所有的menu用一个,使menu与options没有父子关系,或者干脆在点击menu的时候把它的z-index调大,这样其子元素就不会被遮盖住了。
最后
本文的例子都是以符合W3C的Chrome浏览器做验证,但在IE6,7 z-index的默认值并不是auto而是0,这样会导致很多奇怪现象,这时候就需要考虑这点了。
更多编程相关知识,请访问:编程入门!!

前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!










