如果你不是一名csser新手,想必你对z-index的用法应该有个大致的了解了吧,z-index可以控制定位元素在垂直于显示屏方向(Z 轴)上的堆叠顺序,本文不去讲述基本的API如何使用,而是去更深入的了解z-index是如何工作的,使用z-index的时候有哪些问题,以及z-index在日常开发中的使用。 下面我们通过一个例子来引入今天的正文,代码示例:

<style type="text/css">
.red, .green, .blue {
position: absolute;
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
color: #fff;
}
.red {
background-color: red;
z-index: 1;
}
.green {
background-color: green;
top: 70px;
left: 70px;
}
.blue {
background-color: blue;
top: 140px;
left: 140px;
}
</style>
<div>
<span class="red">Red box</span>
</div>
<div>
<span class="green">Green box</span>
</div>
<div>
<span class="blue">Blue box</span>
</div>

如下图:

上述代码通俗易懂,下面有个问题请大家思考: 在遵循下述规则的情况下,如何使用红色span元素在green和blue元素后面?

1) 不能以任何方式更改html标记;

2) 不能增加或改变任何元素的z-index属性;

3) 不恩增加或改变任何元素的position属性;

请大家思考,这个问题改如何解决?说明其原因? ----------------------------------- 分割线 ----------------------------------------------

一、z-index 黄金法则及stack context

1) 一个box和它的父亲有相同的堆叠级别(stack level),除非该box被通过z-index属性赋予了不同的stack level;

2) z-index属性只适应于position属性为relative、absolute、fixed的元素对象;

3) 给一个被定位(positioned)元素设置小于1的opacity属性值,意味着创建了一个堆叠上下文(stack context),就像给该元素增加了一个z-index值;

4) 对于一个被positioned box,如果指定了z-index属性,意味着:

->该box的stack level 在当前的stack context中;

->该box建立了个本地stack context;

5) 如果box没有指定z-index,元素将被按下面的顺序堆叠(stacked)(从后到前):

-> 正常流中的boxes,根据在源代码中的序列;

-> 浮动boxes;

-> computed后display属性值为inline/inline-block/inline-table的boxes;

-> positioned boxes 和boxes 设置opacity值小于1,根据在源代码中的序列;

因此,当我们给一个positioned元素设置了z-index时,我们做了两件事:

1) 该元素与在它前面或者后面的元素共享着相同的stack context,这也就是我们改变z-index的值,元素会移动其他元素前后者后的原因。

2) 为该元素内的任何元素创建了一个新的stack context,一旦你创建了一个stack context,内部的任何有(stack context)的任何层都会停留在这个stack context。 通过上述的黄金法则,也许你已经知道上面那个问题的答案了。在黄金法则里,我们提到了个新名词“stack context”,下面我们通过一个实例来介绍它:

<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>z-index example</title>
</head>
<body>
<h1>Header</h1>
<p>I am paragraph. <em> I am em</em></p>
</body>
</html>

一个很特殊的情况是,在一个document中,没有任何定位,document有且只有一个堆叠环境 - 通过HTML创建。 下面我们给上例添加如下样式:

		h1, p {
position: relative;
}
h1 {
z-index: 2;
}
p {
z-index: 1;
}

在这种情况下,h1,p都创建了一个stack context,这两个stack context都在document的stack context内。增加样式后h1在p元素之上。如果我们给em元素增加如下样式结果又会怎样:

		h1, p, em {
position: relative;
}
h1 {
z-index: 2;
background-color: #f0f;
}
p {
z-index: 1;
background-color: #00f;
line-height: 40px;
}
em {
z-index: 1;
background-color: #f00;
}

增加此样式后em创建了stack context,由于em的z-index属性,它的内部的text比p标签中的其它text更接近用户。因为它是在p的stack context内部,它是一直比h1中的text低的。 注意:如果你增加z-index的值,是不能使用em位于h1之上的。如果你想一个context的元素位于另一个context中的元素之上,你必须提升整个context或者设置它们为相同的context。 下面是两种解决方案: 方案一:

		h1, p, em {
position: relative;
}
h1 {
z-index: 2;
background-color: #f0f;
}
p {
/* raise the entire context,p and em 都在h1 之上了*/
z-index: 3;
background-color: #00f;
line-height: 40px;
margin-top: -40px;
}
em {
z-index: 1;
background-color: #f00;
}

方案二:

		h1, p, em {
position: relative;
}
h1 {
z-index: 2;
background-color: #f0f;
}
p {
background-color: #00f;
line-height: 40px;
margin-top: -40px;
}
em {
/* put them into the same context */
z-index: 2;
background-color: #f00;
}

二、创建stack context及注意事项

那么创建stack context的方式有哪些?

1) When an element is the root element of a document (theelement)

2) When an element has a position value other than static and a z-index value other than auto

3) When an element has an opacity value less than 1

Update: In addition to opacity, several newer CSS properties also create stacking contexts. These include: transforms, filters, css-regions, paged media, and possibly others. As a general rule, it seems that if a CSS property requires rendering in an offscreen context, it must create a new stacking context.

In WebKit, styling a box with position:fixed or -webkit-overflow-scrolling:touch implicitly creates a stacking context, just like adding a z-index value.

Also, be aware of these CSS3 “triggers”:

transform != none

transform-style: preserve-3d

filter != none clip-path, mask

Lastly, even though a relatively positioned element without a z-index set does not establish a stacking context… A common IE bug, often seen in drop-down menus, is that any relatively positioned element that has haslayout set to true establishes a stacking context. One may visualize this bug by setting [A] and [B] to position:relative, while [a] gets position:relative; z-index:1. Now, dragging [A] under [B] hides [a] - in Internet Explorer, that is. Any positioned child with a z-index is caught by this wrong stacking context of its parent.

三、z-index在某些浏览器中的问题

1) IE6中的 select元素是一个窗口控件,所以它总是出现在层叠顺序的顶部而不会顾及到自然层叠顺序、position属性或者是z-index。可以在div元素上添加一个iframe设置为position:absolute,并设置div的z-index比iframe的高。

2) 因父容器(元素)被定位的缘故,IE6/7会错误的对其stacking context进行重置。

3) 在Firefox2版本中,一个负的z-index值会使元素位于stacking context的后面,而不是位于公认的背景和边框这样的元素stacking context之前。 本文到此结束,最后附上本文开始时提出的问题的答案:

/* add this */
div:first-child {
opacity: .99;
}

感谢您的阅读,文中不妥之处,还望批评指正。

四、参考链接:

Find out how elements stack and start using low z-index values

The Z-Index CSS Property: A Comprehensive Look

Elaborate description of Stacking Contexts

Overlapping And ZIndex

CSS/Properties/z-index

Understanding CSS z-index(MDN)

What No One Told You About Z-Index

测试Demo:

原载于: Benjamin 
本文链接: http://www.zuojj.com/archives/904.html 
如需转载请以链接形式注明原载或原文地址。

深入CSS属性(九):z-index的更多相关文章

  1. 简明CSS属性:定位

    简明CSS属性:定位 第一话 定位 (Positioning) 关键词:position/z-index/top/bottom/right/left/clip POSITION 该属性用来决定元素在页 ...

  2. HTML CSS 属性大全

    CSS 属性大全 文字属性 「字体族科」(font-family),设定时,需考虑浏览器中有无该字体. 「字体大小」(font-size),注意度量单位.<绝对大小>|<相对大小&g ...

  3. Mozilla推荐的CSS属性书写顺序及命名规则

    传说中的Mozilla推荐 /* mozilla.org Base Styles * maintained by fantasai */ /* Suggested order: * display * ...

  4. CSS属性(常用的属性)

    CSS属性(常用的属性)http://www.w3school.com.cn/cssref/index.asp 一:文本与字体属性 1.字体属性 (1):font-size:字体的大小(例如:font ...

  5. 值得注意的CSS属性

    文本TEXT letter-spacing 字符间距 word-spacing 字间距 line-height 行高 text-decoration 修饰(下划线) text-indent 首行缩进 ...

  6. {前端CSS} 语法 Css的几种引入方式 css选择器 选择器的优先级 CSS属性相关 背景属性 边框 CSS盒子模型 清除浮动 overflow溢出属性  定位(position)z-index

    前端CSS CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素,给HTML设置样式,让它更加美观. 当浏览器读到一个样式表,它就会按照这个样式表来对文 ...

  7. CSS属性书写顺序及命名规则

    /* mozilla.org Base Styles * maintained by fantasai */ /* Suggested order: * display * list-style * ...

  8. CSS的未来:一些试验性CSS属性

    尽管现代浏览器已经支持了众多的CSS3属性,但是大部分设计师和开发人员貌似依然在关注于一些很“主流”的属性,如border-radius.box-shadow或者transform等.它们有良好的文档 ...

  9. web前端----css属性

    一.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的名称 ...

随机推荐

  1. 利用VMware在虚拟机上安装Zookeeper集群

    http://blog.csdn.net/u010246789/article/details/52101026 利用VMware在虚拟机上安装Zookeeper集群 pasting

  2. istio 配置https gateway

    沒有親手實驗,参考官方文档:  https://istio.io/docs/tasks/traffic-management/secure-ingress/

  3. 七.jQuery源码解析之.toArray()

    toArray()是将jQuery对象转换成数组 从源码中可以看到,这些常见的方法,都是直接从原生的 javascript中"借鉴"过来的.为什么这么说呢? 225行中,在运行时, ...

  4. Tomcat中部署网站和绑定域名

    在安装的tomcat的文件夹下有个conf文件夹 下面有个server.xml文件, 1. 使用80端口 默认tomcat用的是8080端口. <Connector port="808 ...

  5. [Python] numpy.Matrix

    import numpy as np np.matrix('1, 2; 3, 4') #1, 2 #3, 4 np.matrix([[1,2],[3,4]]) #1, 2 #3, 4

  6. redis相对关系型数据库的优势

    它是键值数据库(非关系),数据查询比关系型数据库快. ps:redis是树状结构,查询快 redis是基于内存的一个数据库,I/O的效率影响较小. ps: 备份数据同步是才进行I/O操作.这个数据同步 ...

  7. Django学习---Form组件认证

    Form组件认证 能够帮助我们做用户认证. 以前写我们自己写用户认证的时候,我们自己写HTML的form表单,点击提交,数据就被发送到后台,后台进行验证.在验证过程中我们就需要自己去写正则表达式去匹配 ...

  8. ASP.NET web 应用程序项目

    ASP.NET web  应用程序项目 .ashx .ashx.cs aspx包括前台一些代码要处理,ashx可以看作是没有aspx页面中前台代码的后台.cs文件. 没有了前台代码,服务器负担少一点, ...

  9. C# 6.0可能的新特性及C#发展历程[转]

      C# 6.0可能的新特性及C#发展历程[转] 年10月份发布了,对应的是.Net Franework 4.5.1. 或者3年,更新增加的东西会比较多,所以对于C# 6.0,还是有一些期待的. 下面 ...

  10. node.js中的匿名函数, 回调函数和嵌套函数

    定义一个函数相信大家已经很熟悉了, 在javascript里的函数也是非常重要的, 使用率非常高, 有几种函数不是很好理解 一, 匿名函数 var remove = function(num1) { ...