英文:
Remove unwanted additional space around SVG?
问题
我使用了sprite SVG,一切都很好,直到我给SVG添加了width
和height
属性。
但是当我添加了一些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 335"></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的默认大小,当没有定义width、height或viewBox属性时。给SVG添加一个viewBox属性。只要它符合内容的纵横比(在这种情况下,是引用了一个具有1024宽和1024高的<symbol>
元素的<use>
元素)。现在(在定义了viewBox之后),你可以删除<symbol>
元素的width和height属性。它们只会混淆事情。然后让CSS控制SVG的宽度和高度(这里是120px),可以直接在SVG上或父HTML元素上设置。
<!-- 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 335"></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 -->
英文:
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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论