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

实现文字叠加图片及响应式重叠文字变色效果的技术求助

实现文字随屏幕尺寸动态变色的图片叠加效果

修改后代码实现

HTML

<div class="container">
  <div class="text-box">
    <h1>Complete<br>Remodeling</h1>
    <p>With 30 years of experience and a track record of successful projects, we have the skills and expertise to remodel your house with precision, efficiency, and minimal stress for you.</p>
    <a class="custom-cta">Contact Us</a>
  </div>
</div>

CSS

.container {
  max-width: 1350px;
  margin: 0 auto;
  background-image: url(https://houniqueconstruction.com/wp-content/uploads/2023/02/kam-idris-_HqHX3LBN18-unsplash-scaled.jpg);
  background-position: bottom left;
  background-repeat: no-repeat;
  background-size: cover;
  padding: 15em 2rem;
  position: relative;
  overflow: hidden;
}

.text-box {
  position: relative;
  z-index: 1;
  max-width: 600px;
}

.text-box::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #fff 30%, transparent 0%);
  z-index: -1;
  transition: background 0.3s ease;
}

h1 {
  font-size: clamp(2.5rem, 5vw, 4rem);
  letter-spacing: 9px;
  text-transform: uppercase;
  background: linear-gradient(90deg, #000 30%, #fff 0%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.custom-cta {
  display: block;
  max-width: 100px;
  margin-top: 10px;
  text-align: center;
  background: gold;
  padding: 20px 40px;
  text-decoration: none;
  color: #000;
}

@media (max-width: 900px) {
  .text-box::before {
    background: linear-gradient(90deg, #fff 50%, transparent 0%);
  }
  h1 {
    background: linear-gradient(90deg, #000 50%, #fff 0%);
    -webkit-background-clip: text;
    background-clip: text;
  }
}

@media (max-width: 600px) {
  .container {
    padding: 8em 2rem;
  }
  .text-box::before {
    background: #fff;
  }
  h1 {
    background: #000;
    -webkit-background-clip: text;
    background-clip: text;
    letter-spacing: 5px;
  }
}

关键改动说明

  • 动态文字颜色:利用background-clip: text让H1文字颜色跟随渐变背景变化,替代手动拆分文字加span的硬编码方式,实现屏幕缩小时重叠区域自动变白。
  • 响应式适配:
    • 用clamp()设置H1字体大小,随屏幕宽度自动缩放,避免溢出。
    • 媒体查询分阶段调整渐变遮罩的白色占比,同步匹配文字颜色渐变,保证视觉一致性。
  • 小屏优化:屏幕足够小时,遮罩层切换为全白色背景,文字变回纯黑色,确保内容完全清晰显示在容器内,同时缩减上下内边距、缩小字间距提升移动端体验。
  • 布局稳定性:添加overflow: hidden防止内容溢出,给容器加左右内边距避免文字贴边。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 16:10:26