英文:
Remove unwanted additional space around SVG?
问题
当我为SVG添加width
和height
时,出现了额外的空白空间。
以下是HTML代码:
<svg>
<use href="https://example.com/sprite-svg.svg#moneyback-icon"></use>
</svg>
以及SVG代码:
<svg width="0" height="0" class="hidden">
<symbol xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 1024 1024" id="moneyback-icon">
<path fill="#E69329" d="M249.6 460.8 358.4 672l202.7-83.2 93.8-271L339.2 335z"></path>
<path fill="#546E7A" d="M153.6 768a166.4 166.4 0 1 0 332.8 0 166.4 166.4 0 1 0-332.8 0Z"></path>
<path fill="#90A4AE" d="M320 576c-106.7 0-192 85.3-192 192s85.3 192 192 192 192-85.3 192-192-85.3-192-192-192zm0 341.3c-83.2 0-149.3-66.1-149.3-149.3S236.8 618.7 320 618.7 469.3 684.8 469.3 768 403.2 917.3 320 917.3z"></path>
<path fill="#90A4AE" d="M298.7 704h42.6v170.7h-42.6z"></path>
<path fill="#FFB74D" d="M275.2 768c21.3 40.5 68.3 57.6 108.8 36.3l352-181.4a127 127 0 0 0 47-40.5c36.2-68.3 119.4-228.3 174.9-367L569.6 401.2 467.2 554.7l-145 76.8c-55.5 27.7-72.6 89.6-47 136.5z"></path>
<path fill="#FFB74D" d="m644.3 64-352 134.4c-15 4.3-32 21.3-47 36.3L126 394.7a116 116 0 0 0-10.7 108.8c8.5 21.3 36.3 72.5 66.1 130.1A188 188 0 0 1 320 576c8.5 0 19.2 0 27.7 2.1L303 488.5l98.2-87.4h170.6S902.4 354 960 215.5L644.3 64z"></path>
<path fill="#FFCDD2" d="M388.3 768a51.6 51.6 0 0 1-70.4-27.7 51.6 51.6 0 0 1 27.7-70.4c25.6-12.8 68.3 85.3 42.7 98.1z"></path>
</symbol>
</svg>
希望这对你有所帮助。
英文:
I use sprite SVG, everything is ok until I add width
and height
to my SVG.
But when I add some width
and height
, additional space appears.
Here is HTML code:
<svg>
<use href="https://example.com/sprite-svg.svg#moneyback-icon"</use>
</svg>
And SVG:
<svg width="0" height="0" class="hidden">
<symbol xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 1024 1024" id="moneyback-icon">
<path fill="#E69329" d="M249.6 460.8 358.4 672l202.7-83.2 93.8-271L339.2 335z"></path>
<path fill="#546E7A" d="M153.6 768a166.4 166.4 0 1 0 332.8 0 166.4 166.4 0 1 0-332.8 0Z"></path>
<path fill="#90A4AE" d="M320 576c-106.7 0-192 85.3-192 192s85.3 192 192 192 192-85.3 192-192-85.3-192-192-192zm0 341.3c-83.2 0-149.3-66.1-149.3-149.3S236.8 618.7 320 618.7 469.3 684.8 469.3 768 403.2 917.3 320 917.3z"></path>
<path fill="#90A4AE" d="M298.7 704h42.6v170.7h-42.6z"></path>
<path fill="#FFB74D" d="M275.2 768c21.3 40.5 68.3 57.6 108.8 36.3l352-181.4a127 127 0 0 0 47-40.5c36.2-68.3 119.4-228.3 174.9-367L569.6 401.2 467.2 554.7l-145 76.8c-55.5 27.7-72.6 89.6-47 136.5z"></path>
<path fill="#FFB74D" d="m644.3 64-352 134.4c-15 4.3-32 21.3-47 36.3L126 394.7a116 116 0 0 0-10.7 108.8c8.5 21.3 36.3 72.5 66.1 130.1A188 188 0 0 1 320 576c8.5 0 19.2 0 27.7 2.1L303 488.5l98.2-87.4h170.6S902.4 354 960 215.5L644.3 64z"></path>
<path fill="#FFCDD2" d="M388.3 768a51.6 51.6 0 0 1-70.4-27.7 51.6 51.6 0 0 1 27.7-70.4c25.6-12.8 68.3 85.3 42.7 98.1z"></path>
</symbol>
</svg>
答案1
得分: 1
以下是您要翻译的内容:
"300x150"是您在开发工具中看到的SVG的默认大小,没有定义宽度、高度或viewBox属性。给SVG添加一个viewBox属性。只要符合内容的纵横比(在这种情况下,引用具有宽度1024和高度1024的<symbol>
元素的<use>
元素)。现在(在定义了viewBox之后),您可以删除<symbol>
元素上的宽度和高度属性。它们只会混淆事情。然后让CSS控制SVG的宽度和高度(这里宽度为120px),可以直接在SVG上或在父HTML元素上设置。
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<use href="#moneyback-icon"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0" class="hidden">
<symbol viewBox="0 0 1024 1024" id="moneyback-icon">
<path fill="#E69329" d="M249.6 460.8 358.4 672l202.7-83.2 93.8-271L339.2 335z"></path>
<path fill="#546E7A" d="M153.6 768a166.4 166.4 0 1 0 332.8 0 166.4 166.4 0 1 0-332.8 0Z"></path>
<path fill="#90A4AE" d="M320 576c-106.7 0-192 85.3-192 192s85.3 192 192 192 192-85.3 192-192-85.3-192-192-192zm0 341.3c-83.2 0-149.3-66.1-149.3-149.3S236.8 618.7 320 618.7 469.3 684.8 469.3 768 403.2 917.3 320 917.3z"></path>
<path fill="#90A4AE" d="M298.7 704h42.6v170.7h-42.6z"></path>
<path fill="#FFB74D" d="M275.2 768c21.3 40.5 68.3 57.6 108.8 36.3l352-181.4a127 127 0 0 0 47-40.5c36.2-68.3 119.4-228.3 174.9-367L569.6 401.2 467.2 554.7l-145 76.8c-55.5 27.7-72.6 89.6-47 136.5z"></path>
<path fill="#FFB74D" d="m644.3 64-352 134.4c-15 4.3-32 21.3-47 36.3L126 394.7a116 116 0 0 0-10.7 108.8c8.5 21.3 36.3 72.5 66.1 130.1A188 188 0 0 1 320 576c8.5 0 19.2 0 27.7 2.1L303 488.5l98.2-87.4h170.6S902.4 354 960 215.5L644.3 64z"></path>
<path fill="#FFCDD2" d="M388.3 768a51.6 51.6 0 0 1-70.4-27.7 51.6 51.6 0 0 1 27.7-70.4c25.6-12.8 68.3 85.3 42.7 98.1z"></path>
</symbol>
</svg>
注意:上述内容已根据您的要求进行翻译,只包含翻译的部分,没有其他内容。
英文:
The size 300x150 that you can see in the dev tools is the default size of an SVG, where no width, height or viewBox attribute has been defined. Give the SVG a viewBox attribute. The numbers are not important as long as if fits the aspect ratio of the content (in this case the <use>
element that refers to a <symbol>
elements that has a viewBox 1024 wide and 1024 high). Now (after defining the viewBox) you can remove the width and height attributes of the <symbol>
element. They will just confuse stuff. And then let the CSS control the width and height of the SVG (here width of 120px) either directly on the SVG or on a parent HTML element.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
svg.icon {
border: thin solid red;
width: 120px;
}
svg.hidden {
visibility: hidden;
}
<!-- language: lang-html -->
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<use href="#moneyback-icon"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0" class="hidden">
<symbol viewBox="0 0 1024 1024" id="moneyback-icon">
<path fill="#E69329" d="M249.6 460.8 358.4 672l202.7-83.2 93.8-271L339.2 335z"></path>
<path fill="#546E7A" d="M153.6 768a166.4 166.4 0 1 0 332.8 0 166.4 166.4 0 1 0-332.8 0Z"></path>
<path fill="#90A4AE" d="M320 576c-106.7 0-192 85.3-192 192s85.3 192 192 192 192-85.3 192-192-85.3-192-192-192zm0 341.3c-83.2 0-149.3-66.1-149.3-149.3S236.8 618.7 320 618.7 469.3 684.8 469.3 768 403.2 917.3 320 917.3z"></path>
<path fill="#90A4AE" d="M298.7 704h42.6v170.7h-42.6z"></path>
<path fill="#FFB74D" d="M275.2 768c21.3 40.5 68.3 57.6 108.8 36.3l352-181.4a127 127 0 0 0 47-40.5c36.2-68.3 119.4-228.3 174.9-367L569.6 401.2 467.2 554.7l-145 76.8c-55.5 27.7-72.6 89.6-47 136.5z"></path>
<path fill="#FFB74D" d="m644.3 64-352 134.4c-15 4.3-32 21.3-47 36.3L126 394.7a116 116 0 0 0-10.7 108.8c8.5 21.3 36.3 72.5 66.1 130.1A188 188 0 0 1 320 576c8.5 0 19.2 0 27.7 2.1L303 488.5l98.2-87.4h170.6S902.4 354 960 215.5L644.3 64z"></path>
<path fill="#FFCDD2" d="M388.3 768a51.6 51.6 0 0 1-70.4-27.7 51.6 51.6 0 0 1 27.7-70.4c25.6-12.8 68.3 85.3 42.7 98.1z"></path>
</symbol>
</svg>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论