css扩大点击区域

2019-12-23  本文已影响0人  前端阿良古

问题

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

答案

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

理解

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

代码

<!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>
上一篇下一篇

猜你喜欢

热点阅读