css中的z-index(二)

如果都没有z-index,结果又会是怎样呢?

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>z-index</title>

<style type="text/css">

*{

margin: 0;

padding: 0;

}

.box1{

position: absolute;

width: 100px;

height: 100px;

background-color: red;

top: 100px;

left: 100px;

 

}

.box2{

position: absolute;

width: 100px;

height: 100px;

background-color: green;

top: 180px;

left: 180px;

}

</style>

</head>

<body>

<div class="box1">box1</div>

<div class="box2">box2</div>

</body>

</html>

运行结果:

006zipb5zy7aeXN2vHr21&690

由于box2在box1后面,在没有设置z-index的情况下,box2会压住box1。

接下来我们来看一下什么是从父现象

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Document</title>

<style type="text/css">

.div1{

position: relative;

width: 200px;

height: 200px;

background-color: blue;

z-index: 10

}

.div1 .child1{

position: absolute;

width: 100px;

height: 100px;

top: 240px;

left: 300px;

z-index: 56;

background-color: green;

}

.div2{

position: relative;

width: 200px;

height: 200px;

background-color:red;

z-index: 20;

}

.div2 .child2{

position: absolute;

width: 100px;

height: 100px;

top: 100px;

left: 350px;

z-index: 5;

background-color: pink;

}

 

 

</style>

</head>

<body>

<div class="div1">

<div class="child1"></div>

</div>

<div class="div2">

<div class="child2"></div>

</div>

</body>

</html>

运行结果:

006zipb5zy7aeXO6ifZ37&690 

这里我们设置div2的z-index小于div1的z-index,设置的div1的子元素child1的z-index大于div2的子元素child2的z-index。但最后运行的结果是child2压住child1。这就是从父现象。也就是说如果父元素被压住了。子元素也逃不了被压住的命运。无论子元素的z-index的大小。

 

 

 

转载于:https://my.oschina.net/u/2971691/blog/877491

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值