You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

伪元素无法100%填充overflow:hidden容器的问题求助

Fixing the Gap Between Button Border and Pseudo-element on Hover

I get it—those tiny, persistent gaps between the red border and expanding pseudo-element on Mac browsers are super frustrating. They’re caused by rendering precision issues with transforms and how the pseudo-element is sized relative to the button’s border. Let’s walk through two reliable fixes that keep that slick hover effect intact.

Solution 1: Adjust Pseudo-element to Cover the Entire Button (Border Included)

The core problem is your original pseudo-element is sized relative to the button’s padding box, not accounting for the 9px border. When rotated, its edges don’t quite reach the inner border edge, leaving a visible gap. Here’s the adjusted code:

.button {
  position: relative;
  display: inline-block;
  padding: 20px;
  line-height: 10px;
  font-size: 12px;
  font-weight: bold;
  text-transform: uppercase;
  border: 9px solid black;
  border-radius: 55px;
  transition: all 0.35s ease-in-out;
  color: black;
  cursor: pointer;
  overflow: hidden;
  z-index: 1;
  background-color: transparent;
}

.button:before {
  content: '';
  display: block;
  position: absolute;
  background-color: red;
  /* Align with button's outer edge (matches border width) */
  left: -9px;
  top: -9px;
  transform: rotate(-30deg);
  transform-origin: center;
  /* Cover full width including both border sides */
  width: calc(100% + 18px);
  height: 0;
  padding-top: 0%;
  transition: all 0.35s ease-in-out;
  z-index: -1;
  border-radius: inherit; /* Keep rounded corners consistent */
}

.button:hover {
  color: #fff;
  border-color: red;
}

.button:hover:before {
  padding-top: 200%;
}

Key tweaks:

  • left: -9px and top: -9px position the pseudo-element to match the button’s outer border edge
  • width: calc(100% + 18px) ensures it covers the full button width including both 9px borders
  • Added border-radius: inherit to maintain the button’s rounded corners
  • Removed translate(-50%, -50%) since we’re now covering the entire button from the start

This guarantees the pseudo-element fully covers the border area when expanded, eliminating the gap.

Solution 2: Replace Pseudo-element with Background Clip-path Transition

If you want to avoid pseudo-element sizing headaches entirely, using a clip-path transition on the button’s background is a cleaner, more performant alternative. It creates a similar expanding effect without rendering gaps:

.button {
  position: relative;
  display: inline-block;
  padding: 20px;
  line-height: 10px;
  font-size: 12px;
  font-weight: bold;
  text-transform: uppercase;
  border: 9px solid black;
  border-radius: 55px;
  transition: all 0.35s ease-in-out;
  color: black;
  cursor: pointer;
  background-color: red;
  /* Start with a tiny centered clip-path (hidden background) */
  clip-path: polygon(50% 50%, 50% 50%, 50% 50%, 50% 50%);
}

.button:hover {
  color: #fff;
  border-color: red;
  /* Expand clip-path to cover the entire button */
  clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
}

This works by starting with a tiny, centered clip-path that hides the red background, then transitioning to a full polygon that covers the entire button on hover. No transforms, no pseudo-element sizing issues—just smooth, gap-free animation.

Both solutions will fix the cross-browser gap issue on Mac while preserving the original hover effect’s style.

内容的提问来源于stack exchange,提问作者ichti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:58:16