使用CSS伪元素增加按钮的可点击范围,不影响美观,增加用户的体验

<!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>
#main{
height: 200px;
width: 200px;
background-color: #bfa;
position: absolute;
}
body{
margin: 0;
padding: 0;
}
#close{
position: absolute;
left: 180px;
}
#close::after{
/* background-color: blue; */
top: -10px;
left: -10px;
bottom: -20px;
right: -10px;
position: absolute;
content: '';
}
</style>
</head>
<body>
<div id="main">

<div id="close">X</div>
</div>

<script>
document.getElementById("close").onclick=function(){
alert(123)
}

</script>
</body>
</html>


增加按钮的点击范围_html

点击X aleart(123)

一开始范围很小

把伪元素的背景颜色代码放开后,可以看到可以点击的范围变大了,把颜色注掉,还是原来的样式不影响美观,但是可点击的范围变大了。

增加按钮的点击范围_伪元素_02