美文网首页
css扩大点击区域

css扩大点击区域

作者: 前端阿良古 | 来源:发表于2019-12-23 22:20 被阅读0次

    问题

    把“按钮”的点击区域放大一些

    答案

    使用 ::before 或者 ::after`` 伪元素,利用 relative + absolute 定位,伪元素设为负数,给一个更大的区域

    理解

    通过添加伪元素可以伪造更大的点击区域原因:

    • 利用 relative + absolute 定位不会改变又来的文档流和布局
    • 伪元素继承了元素的 cursor 属性
    • 伪元素是原来点击节点的子节点,点击伪元素能触发事件并冒泡给父元素

    代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <style>
        .area {
          width: 100px;
          text-align: center;
          border: 1px solid #ccc;
          cursor: pointer;
          position: relative;
        }
        .area::before{
          content: "";
          position: absolute;
          border: 1px solid #fa0000;
          top: -10px;
          left: -10px;
          bottom: -10px;
          right: -10px;
        }
      </style>
    </head>
    <body style="margin: 30px">
      <div class="area">原点击区域</div>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:css扩大点击区域

          本文链接:https://www.haomeiwen.com/subject/whitoctx.html