<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
overflow: hidden;
position: relative;
width: 100px;
height: 100px;
background-color: red;
}
.son {
position: absolute;
top: 10px;
left: 10px;
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
</div>
</body>
</html>
放上结果
可以看到两个父子级别的div用定位时overflow:hidden是有效果的
(注意要给box加相对定位)
但是我今天遇到的不是两个div而是a包含span
如果单纯把div换成a和span是没效果的就是这样
在这里插入代码片<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
a {
overflow: hidden;
position: relative;
width: 100px;
height: 100px;
background-color: red;
}
span {
position: absolute;
top: 10px;
left: 10px;
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<a class="box">
<span class="son"></span>
</a>
</body>
</html>
放上结果:
其实原因很简单啦
因为a是行内元素直接加width和height没效果,但是span可以因为他加了绝对定位从而具有行内块元素的特点,相对定位不可以哦
只需要给a加一句display:block;