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

如何轻松为房屋形状SVG添加内阴影(inset box shadow)

给SVG房屋图标添加柔和内阴影的解决方案

CSS的box-shadow: inset无法直接作用于SVG的<path>元素实现内阴影,必须使用SVG的<filter>滤镜来实现。以下是修改后的完整代码,以及参数调整说明:

修改后的代码

CSS 部分

* {
  background-color: lightcoral;
  text-align: center;
}

.homeButton {
  fill: white;
  margin-top: 12em;
}

HTML 部分

<!DOCTYPE html>
<html>
  <head>
    <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <svg class="homeButton" xmlns="http://www.w3.org/2000/svg" height="10em" viewBox="0 0 576 512">
      <!-- 定义可复用的内阴影滤镜 -->
      <defs>
        <filter id="softInnerShadow">
          <feOffset dx="1.5" dy="1.5" />
          <feGaussianBlur stdDeviation="2.5" result="offset-blur" />
          <feComposite operator="out" in="SourceGraphic" in2="offset-blur" result="inverse" />
          <feFlood flood-color="rgba(0, 0, 0, 0.25)" result="shadow-color" />
          <feComposite operator="in" in="shadow-color" in2="inverse" result="shadow" />
          <feComposite operator="over" in="shadow" in2="SourceGraphic" />
        </filter>
      </defs>
      <!-- 给房屋路径应用滤镜 -->
      <path filter="url(#softInnerShadow)" d="M575.8 255.5c0 18-15 32.1-32 32.1h-32l.7 160.2c0 2.7-.2 5.4-.5 8.1V472c0 22.1-17.9 40-40 40H456c-1.1 0-2.2 0-3.3-.1c-1.4 .1-2.8 .1-4.2 .1H416 392c-22.1 0-40-17.9-40-40V448 384c0-17.7-14.3-32-32-32H256c-17.7 0-32 14.3-32 32v64 24c0 22.1-17.9 40-40 40H160 128.1c-1.5 0-3-.1-4.5-.2c-1.2 .1-2.4 .2-3.6 .2H104c-22.1 0-40-17.9-40-40V360c0-.9 0-1.9 .1-2.8V287.6H32c-18 0-32-14-32-32.1c0-9 3-17 10-24L266.4 8c7-7 15-8 22-8s15 2 21 7L564.8 231.5c8 7 12 15 11 24z"/>
    </svg>
  </body>
</html>

参数调整说明

你可以通过修改滤镜里的参数来调整内阴影的效果:

  • dx/dy:控制阴影的偏移距离,数值越小,阴影越贴近图形边缘
  • stdDeviation:控制阴影的模糊程度,数值越大,阴影越柔和
  • flood-color:设置阴影的颜色和透明度,rgba的最后一位数值越小,阴影越淡

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 05:45:32