我正在try 使用border-radius来圆化鼠标悬停按钮的角点.然而,当我这样做时,角落不再在可点击/悬停区域,所以当你将鼠标悬停在任何角落上时,按钮可以预见地开始闪烁.

有什么办法可以绕过这件事吗?如果可能的话,我希望避免添加另一个HTML元素,并且只使用CSS来处理.

我用的是Tailwincss,我的分数是play set up here.

推荐答案

可以使用pseudo-elements(::before/::after)使可单击区域与元素的实际形状不匹配.

我的首选是使用伪元素作为可点击区域,在本例中可以看到半透明的青色:

button {
  position: relative;
  background: transparent;
  border: 0;
  color: white;
  background: red;
  height: 64px;
  padding: 0 64px;
  border-radius: 8px;
  transition: border-radius linear 300ms;
}

button:hover {
  border-radius: 32px;
}

button::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 255, 255, .125);
}
<button>Click me!</button>

但您也可以反过来使用伪元素制作border-radius的动画/过渡:

button {
  position: relative;
  background: transparent;
  border: 0;
  color: white;
  height: 64px;
  padding: 0 64px;
}

button::before {
  content: "";
  position: absolute;
  background: red;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: -1;
  border-radius: 8px;
  transition: border-radius linear 300ms;
}

button:hover::before {
  border-radius: 32px;
}
<button>Click me!</button>

Html相关问答推荐

Flexbox嵌套div溢出

自动字间距以适应行CSS

Chrome和Safari浏览器中的CSS3动画不同

如何使我的组织 struct 图的连接线响应?

浏览器是在调整大小还是在全屏上交换视频源?

正文不能填满浏览器100%的高度

在Quarto/Discrealjs演示文稿中只增加一个列表的填充

如何在垂直堆叠的表格中调整人造列的大小?

Css网格:两列,除非小于150px

如何用背景色填充边框半径创建的间隙

如何将tmap按钮和图例更改为位于页脚后面?

来自元标记的响应文本

我真的需要一个容器来让行和列正常工作吗? ( bootstrap 程序 v5)

从 Vue 应用程序的容器元素渲染 HTML

在 iPhone 上分隔 HTML 邮箱输入中的多个邮箱条目

将图像高度调整到容器 div 中而不使其高度增加

不同屏幕尺寸的显示问题

项目的水平列表:倒数第二个项目居中,而最后一个项目占据所有剩余空间

每次按下按钮时减小字体大小的 jquery 函数

在部分而不是正文中定义页面宽度