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

如何让border-radius区域显示对应元素背景色/图(不修改transform)

解决方案:让.next圆角区域显示.txt的背景内容(不修改.txt的transform)

问题说明

需要实现.next元素顶部的圆角区域显示.txt元素的背景色/图片,且不能改动.txt的transform属性。当前代码中,圆角区域显示的是body的红色,不符合预期。

修改后的完整代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    body {
      background-color: #f00;
    }
    .wrap {
      position: relative;
      overflow: hidden;
    }
    .txt {
      background-color: #00f;
      height: 300px;
      translate: none;
      rotate: none;
      scale: none;
      transform: translate3d(0px, 150px, 0px);
    }
    .txt img {
      width: 100%;
    }
    .next {
      position: relative;
      height: 1000px;
      background-color: #ccc;
      border-radius: 20px 20px 0 0;
    }
    /* 新增伪元素填充圆角区域的背景 */
    .next::before {
      content: '';
      position: absolute;
      top: -150px;
      left: 0;
      right: 0;
      height: 300px;
      background: url("https://picsum.photos/640/360") no-repeat center/cover;
      /* 若需显示背景色则替换为 background-color: #00f; */
      transform: translate3d(0px, 150px, 0px);
      z-index: -1;
    }
  </style>
</head>
<body>
  <div class="wrap">
    <div class="txt"><img src="https://picsum.photos/640/360" alt=""></div>
  </div>
  <div class="next">next</div>
</body>
</html>

实现原理

  1. 给.next添加伪元素::before,复制.txt的背景资源,并设置和.txt完全一致的transform值,确保伪元素位置和.txt完全重合。
  2. 通过top: -150px将伪元素向上偏移,让它刚好覆盖.next圆角区域的下方位置。
  3. 设置z-index: -1让伪元素处于.next下方,这样.next的圆角裁剪后,就能显示出下方伪元素的背景内容,同时不遮挡.next自身的文字。
  4. 全程未修改.txt的transform属性,完全满足需求限制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 12:41:07