HTML DOM Style zIndex property
Definition and Usage
zIndex property sets or returns the stacking order of the positioning elements.
Element has a higher stacking order (1) will always be in front of the lower stacking order (0) element.
Tip: a positioning element is the element's position attribute is set: relative (relative), absolute (absolute) or fixed (fixed).
grammar
Setting zIndex properties:
Object.style.zIndex="auto|number|inherit"
Back zIndex properties:
Object.style.zIndex
值 | 描述 |
---|---|
auto | 默认。浏览器决定元素的堆叠顺序(基于元素在文档中的顺序)。 |
number | 一个定义元素堆叠顺序的整数。可使用负值。 |
inherit | zIndex 属性的值从父元素继承。 |
Browser Support
All major browsers support zIndex property.
Tips and Notes
Tip: If you want to create overlapping elements, this property is very useful.
Examples
Examples
Change <img> element in the stacking order:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<style type="text/css">
#img1{
position:absolute;
left:0px;
top:0px;
z-index:-1
}
</style>
<script>
function changeStackOrder(){
document.getElementById("img1").style.zIndex="1";
}
</script>
</head>
<body>
<h1>这是一个标题</h1>
<img id="img1" src="bulbon.gif" width="100" height="180">
<input type="button" onclick="changeStackOrder()" value="修改堆叠顺序">
<p>默认 z-坐标是 0,Z-坐标 -1优先级较低。 </p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<style type="text/css">
#img1{
position:absolute;
left:0px;
top:0px;
z-index:-1
}
</style>
<script>
function changeStackOrder(){
document.getElementById("img1").style.zIndex="1";
}
</script>
</head>
<body>
<h1>这是一个标题</h1>
<img id="img1" src="bulbon.gif" width="100" height="180">
<input type="button" onclick="changeStackOrder()" value="修改堆叠顺序">
<p>默认 z-坐标是 0,Z-坐标 -1优先级较低。 </p>
</body>
</html>
try it"
Style Objects