I have two implementations of consuming the same SVG content: one through a <use> element and one through an <img>. Despite the same content, viewbox, and sizing on both, the <use> element comes up shorter -- whereas the <img> is 160x160, <use> is 150x122. For the life of me I cannot seem to find an answer as to why this is.

我希望解决的问题是将通过<img>使用的大约10个单独的SVG文件合并到一个单独的精灵工作表中,以避免不必要的网络请求(它们总是一起使用,使这成为精灵的完美使用).然而,当切换到精灵工作表时,大小不匹配.

Index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
            div {
                display: flex;
                flex-direction: column;
                max-width: 10rem;
            }
            img, svg {
                width: 100%;
            }
        </style>
    </head>
    <body>
        <div>
            <svg>
                <use href="./Icon-Sprite.svg#icon" />
            </svg>
            <img src="./Icon.svg" />
        </div>
    </body>
</html>

Icon-Sprite.svg

<svg xmlns="http://www.w3.org/2000/svg">
    <symbol id="icon" viewBox="0 0 54 54">
        <path d="M52 5H2C.9 5 0 5.9 0 7v40c0 1.1.9 2 2 2h50c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zM1 7c0-.55.45-1 1-1h50c.55 0 1 .45 1 1v9H1zm52 40c0 .55-.45 1-1 1H2c-.55 0-1-.45-1-1V17h52z" fill="#37474f" />
        <path d="M6 8.5a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5S5.17 9.5 6 9.5s1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm6-4a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm6-4a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zM48 10c0-.55-.45-1-1-1H35c-.55 0-1 .45-1 1v2c0 .55.45 1 1 1h12c.55 0 1-.45 1-1zm-1 2H35v-2h12zM29.8 29.58l3.2 2.28V44c0 1.65 1.35 3 3 3s3-1.35 3-3V31.86l3.2-2.28c.44-.32.8-1.03.8-1.58v-5c0-.55-.32-1.32-.7-1.7L39 18v6l-3 2-3-2v-6l-3.3 3.3c-.38.38-.7 1.15-.7 1.7v5c0 .55.36 1.26.8 1.58zM30 23c0-.28.2-.8.4-1l1.6-1.6v4.15l.45.3 3 2 .55.36.56-.37 3-2 .44-.3V20.2l1.6 1.7c.2.2.4.82.4 1.1v5c0 .23-.2.63-.4.77l-3.18 2.27-.42.3V44c0 1.1-.9 2-2 2s-2-.9-2-2V31.34l-.42-.3-3.18-2.27c-.2-.14-.4-.54-.4-.77z" fill="#37474f" />
        <path stroke-miterlimit="10" d="M36 44V33" fill="#b2ff59" stroke="#37474f" stroke-linecap="round" />
        <path d="M27 36h-4V19h-8v17h-4l8 11zm-12 1h1V20h6v17h3.04L19 45.3 12.96 37z" fill="#37474f" />
    </symbol>
</svg>

Icon.svg

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 54 54">
    <path d="M52 5H2C.9 5 0 5.9 0 7v40c0 1.1.9 2 2 2h50c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zM1 7c0-.55.45-1 1-1h50c.55 0 1 .45 1 1v9H1zm52 40c0 .55-.45 1-1 1H2c-.55 0-1-.45-1-1V17h52z" fill="#37474f" />
    <path d="M6 8.5a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5S5.17 9.5 6 9.5s1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm6-4a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm6-4a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zM48 10c0-.55-.45-1-1-1H35c-.55 0-1 .45-1 1v2c0 .55.45 1 1 1h12c.55 0 1-.45 1-1zm-1 2H35v-2h12zM29.8 29.58l3.2 2.28V44c0 1.65 1.35 3 3 3s3-1.35 3-3V31.86l3.2-2.28c.44-.32.8-1.03.8-1.58v-5c0-.55-.32-1.32-.7-1.7L39 18v6l-3 2-3-2v-6l-3.3 3.3c-.38.38-.7 1.15-.7 1.7v5c0 .55.36 1.26.8 1.58zM30 23c0-.28.2-.8.4-1l1.6-1.6v4.15l.45.3 3 2 .55.36.56-.37 3-2 .44-.3V20.2l1.6 1.7c.2.2.4.82.4 1.1v5c0 .23-.2.63-.4.77l-3.18 2.27-.42.3V44c0 1.1-.9 2-2 2s-2-.9-2-2V31.34l-.42-.3-3.18-2.27c-.2-.14-.4-.54-.4-.77z" fill="#37474f" />
    <path stroke-miterlimit="10" d="M36 44V33" fill="#b2ff59" stroke="#37474f" stroke-linecap="round" />
    <path d="M27 36h-4V19h-8v17h-4l8 11zm-12 1h1V20h6v17h3.04L19 45.3 12.96 37z" fill="#37474f" />
</svg>

推荐答案

要将其构建到代码片段中,我必须使用Sprite的内联SVG,以避免跨域安全问题.但我要做的就是让精灵像图片一样大小,只需加一个aspect-ratio: 1/1就行了.

请注意,您的icon.svgicon-sprite.svg都没有固有的大小,因此如果您不指定大小,则依赖于浏览器的默认行为,该行为可能因浏览器而异.你的CSS指定了SVG的宽度(icon.svg%的flexbox,10em宽),但不是高度.我相信规范确实说了,如果没有指定大小,用户代理应该为SVG使用默认大小.看起来,至少在我的浏览器中,当没有指定高度时,常规SVG默认为1:1的长宽比,而sprite默认为零高度.

不确定为什么会得到不同的结果,但这可能取决于浏览器.我在Mac上使用Safari.我也try 了一下Chrome,精灵不需要手动设置宽高比.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style>
            div {
                display: flex;
                flex-direction: column;
                max-width: 10rem;
                gap: 1em;
            }
            img, svg {
                width: 100%;
            }
        </style>
    </head>
    <body>
        <svg style="display: none">
          <defs>
            <symbol id="icon" viewBox="0 0 54 54">
              <path d="M52 5H2C.9 5 0 5.9 0 7v40c0 1.1.9 2 2 2h50c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zM1 7c0-.55.45-1 1-1h50c.55 0 1 .45 1 1v9H1zm52 40c0 .55-.45 1-1 1H2c-.55 0-1-.45-1-1V17h52z" fill="#37474f" />
              <path d="M6 8.5a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5S5.17 9.5 6 9.5s1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm6-4a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm6-4a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5zm0 4c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zM48 10c0-.55-.45-1-1-1H35c-.55 0-1 .45-1 1v2c0 .55.45 1 1 1h12c.55 0 1-.45 1-1zm-1 2H35v-2h12zM29.8 29.58l3.2 2.28V44c0 1.65 1.35 3 3 3s3-1.35 3-3V31.86l3.2-2.28c.44-.32.8-1.03.8-1.58v-5c0-.55-.32-1.32-.7-1.7L39 18v6l-3 2-3-2v-6l-3.3 3.3c-.38.38-.7 1.15-.7 1.7v5c0 .55.36 1.26.8 1.58zM30 23c0-.28.2-.8.4-1l1.6-1.6v4.15l.45.3 3 2 .55.36.56-.37 3-2 .44-.3V20.2l1.6 1.7c.2.2.4.82.4 1.1v5c0 .23-.2.63-.4.77l-3.18 2.27-.42.3V44c0 1.1-.9 2-2 2s-2-.9-2-2V31.34l-.42-.3-3.18-2.27c-.2-.14-.4-.54-.4-.77z" fill="#37474f" />
              <path stroke-miterlimit="10" d="M36 44V33" fill="#b2ff59" stroke="#37474f" stroke-linecap="round" />
              <path d="M27 36h-4V19h-8v17h-4l8 11zm-12 1h1V20h6v17h3.04L19 45.3 12.96 37z" fill="#37474f" />
            </symbol>
          </defs>
        </svg>

        <div>
            <svg style="aspect-ratio: 1/1; border: 2px solid lime;">
                <use href="#icon" />
            </svg>
            <img style="border: 2px solid cyan;" src="https://donald.au/bugs/so-77582685/icon.svg" />
        </div>
    </body>
</html>

Html相关问答推荐

卸载伪元素 destruct 了CSS

将弹性容器设置为内容宽度

仅在加载视频时显示描述(<;Video>;标签)

对称渐变作为背景

为什么在添加文本时网格区域会调整大小?

当光标悬停在(相同)父元素上方时,为多个子元素创建不同的过渡动画

SCSS动画错误:心脏在页面刷新时启动动画,原因是:Checked和:Not(:Checked) Select 器

类型';联系人组件';上不存在属性';name FormControl';

我正在try 向列表中的图像添加悬停,以放大图像并将div的不透明度设置为1而不是0

如何使div填充父项的剩余高度

仅 Select 悬停的级别,而不 Select 其父级或子级

我在桌面版 html css 代码上给出了位置 margin-left margin-top 标签,如何将其删除到手机上以使其响应?

如何使用CSS Select 表亲元素

绝对类在另一个绝对类之上

如何在 css 和 html 中创建花状 struct

如何制作一个空的网格模板行来填充剩余空间?

为什么图像会影响我的

如何在没有 JS 的情况下创建带搜索的 Select 菜单

如何使列表的第一个元素比 css 中的其他元素大?

使用 htmloutput 在shiny 的应用程序中呈现文本