代码之家  ›  专栏  ›  技术社区  ›  SHRUBBY

有没有什么方法可以轻松地给SVG一个嵌入框阴影?

  •  1
  • SHRUBBY  · 技术社区  · 2 年前

    我希望能够为这个房子形状的SVG添加一个微妙的嵌入阴影。

    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"><path 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>
    

    CSS:

    * {
      background-color: lightcoral;
      text-align: center;
    }
    
    .homeButton {
      fill: white;
      margin-top: 12em;
    }
    

    我试着从互联网上复制粘贴代码,这是我所能做的,因为我对这类事情的经验非常有限。似乎什么都不起作用。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Lashen Dharmadasa    2 年前

    由于过滤器不是直接插入svg,请考虑使用自定义过滤器标签:

    <svg class="homeButton" xmlns="http://www.w3.org/2000/svg" height="10em" viewBox="0 0 576 512" stroke="transparent" stroke-width="1">
            <filter id="inset-shadow" x="-50%" y="-50%" width="200%" height="200%">
                <feComponentTransfer in=SourceAlpha>
                    <feFuncA type="table" tableValues="1 0" />
                </feComponentTransfer>
                <feGaussianBlur stdDeviation="40" />
                <feOffset dx="0" dy="50" result="offsetblur" />
                <feFlood flood-color="rgb(20, 0, 0)" result="color" />
                <feComposite in2="offsetblur" operator="in" />
                <feComposite in2="SourceAlpha" operator="in" />
                <feMerge>
                    <feMergeNode in="SourceGraphic" />
                    <feMergeNode />
                </feMerge>
            </filter>
            <path filter="url(#inset-shadow)"
                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" />
    
    推荐文章