增加按钮的点击范围

使用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>

image-20210712095137112

点击X aleart(123)

一开始范围很小

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

image-20210712095310097

posted @ 2021-07-12 09:54  zcbj588  阅读(225)  评论(0)    收藏  举报