2 回答
TA贡献1942条经验 获得超3个赞
viewBox 的工作原理
其实很简单。
为了让浏览器缩放 SVG 内容以适应您指定的宽度和高度,它需要知道该内容有多大。
您可以通过属性来告诉它该内容有多大viewBox
。a 中的值为viewBox
:
<minimum X> <minimum Y> <width> <height>
假设您的内容是从 (10,20) 到 (80,80) 的正方形。完全符合该正方形的定义viewBox
是:
viewBox="10 20 70 60"
这表示内容的左上角位于 10,20,宽度为 70,高度为 60。
这是一个图表来说明我的意思。
<svg width="300" height="300">
<g transform="translate(50 50) scale(2)">
>!-- axes -->
<line x1="0.25" y1="-100" y1="0.25" y2="300" stroke="grey" stroke-width="0.5"/>
<line x1="-100" y1="0.25" x2="300" y2="0.25" stroke="grey" stroke-width="0.5"/>
<!-- bounds -->
<line x1="10.25" y1="-100" x2="10.25" y2="300" stroke="grey" stroke-width="0.5" stroke-dasharray="3 3"/>
<line x1="-100" y1="20.25" x2="300" y2="20.25" stroke="grey" stroke-width="0.5" stroke-dasharray="3 3"/>
<line x1="80.25" y1="-100" x2="80.25" y2="300" stroke="grey" stroke-width="0.5" stroke-dasharray="3 3"/>
<line x1="-100" y1="80.25" x2="300" y2="80.25" stroke="grey" stroke-width="0.5" stroke-dasharray="3 3"/>
<!-- rectangle -->
<rect x="10" y="20" width="70" height="60" fill="rebeccapurple"/>
<!-- labels -->
<g fill="grey" font-size="6px" font-family="sans-serif">
<text x="-2" y="-2" text-anchor="end">0,0</text>
<text x="12" y="-2">10</text>
<text x="82" y="-2">80</text>
<text x="-2" y="27" text-anchor="end">20</text>
<text x="-2" y="87" text-anchor="end">80</text>
<text x="45" y="87" text-anchor="middle">width = 70</text>
<text x="82" y="50">height = 60</text>
</g>
</g>
</svg>
现在浏览器知道 SVG 内容的边界,它可以计算如何缩放和定位该内容,使其适合您的视口。视口只是指页面上 SVG 绘制到的矩形。您可以使用width
和来定义它height
。
你的握手 SVG
显然,viewBox
握手图标对于内容来说太大了。正因为如此,它的规模还不够大。我们需要找到与握手图标形状紧密配合的 viewBox。
但坚持住...
但我们有一个问题。您保留了之前图标中的方形形状。如果我们放大手的形状,矩形就会变得太大。解决您的问题的最简单的方法就是放大并重新定位其余内容以适合您的正方形:
.svg-icon__border {
fill: none;
stroke: red;
}
<svg class="c-section-heading__icon svg-icon" xmlns="http://www.w3.org/2000/svg" width="91" height="91" viewBox="0 0 91 91" >
<rect class="svg-icon__border" x="0.5" y="0.5" width="90" height="90"/>
<g id="test" transform="translate(25,25) scale(1.3)">
<path class="svg-icon__item" d="M25.267 19.713c-0.628-1.309-2.875-3.127-5.045-4.883-1.211-0.979-2.355-1.904-3.084-2.633-0.183-0.184-0.456-0.243-0.697-0.156-0.449 0.163-0.727 0.288-0.945 0.385-0.333 0.149-0.445 0.2-0.895 0.245-0.199 0.020-0.377 0.127-0.488 0.292-0.943 1.409-1.919 1.289-2.571 1.071-0.208-0.069-0.245-0.159-0.265-0.244-0.14-0.585 0.563-1.948 1.473-2.859 2.167-2.168 3.284-2.711 5.644-1.656 2.677 1.197 5.36 2.135 5.387 2.144 0.351 0.121 0.728-0.063 0.849-0.411 0.12-0.348-0.063-0.728-0.411-0.849-0.027-0.009-2.656-0.928-5.28-2.103-3.057-1.367-4.735-0.467-7.131 1.931-0.912 0.912-2.151 2.757-1.831 4.111 0.137 0.576 0.543 1.003 1.145 1.201 1.511 0.499 2.889 0.021 3.916-1.341 0.424-0.065 0.655-0.161 1.001-0.317 0.125-0.056 0.272-0.121 0.467-0.2 0.763 0.716 1.792 1.549 2.876 2.425 1.96 1.585 4.183 3.383 4.683 4.423 0.247 0.513-0.019 0.848-0.199 1.001-0.264 0.227-0.625 0.299-0.821 0.161-0.216-0.148-0.497-0.157-0.72-0.024-0.224 0.133-0.349 0.385-0.321 0.644 0.045 0.424-0.343 0.667-0.511 0.751-0.427 0.216-0.872 0.179-1.039 0.024-0.187-0.173-0.455-0.224-0.692-0.136-0.237 0.089-0.403 0.308-0.427 0.561-0.040 0.437-0.364 0.857-0.787 1.021-0.204 0.077-0.5 0.124-0.765-0.119-0.165-0.149-0.395-0.207-0.609-0.155-0.217 0.053-0.392 0.211-0.468 0.42-0.025 0.067-0.083 0.227-0.707 0.227-0.444 0-1.243-0.3-1.633-0.559-0.468-0.308-3.403-2.497-5.937-4.62-0.356-0.3-0.972-0.943-1.516-1.511-0.483-0.504-0.924-0.961-1.151-1.153-0.284-0.24-0.704-0.204-0.94 0.079-0.237 0.281-0.203 0.703 0.079 0.94 0.207 0.175 0.607 0.597 1.048 1.057 0.595 0.621 1.209 1.264 1.623 1.611 2.483 2.079 5.467 4.323 6.061 4.713 0.491 0.323 1.548 0.776 2.367 0.776 0.657 0 1.163-0.151 1.513-0.445 0.469 0.183 1.003 0.184 1.516-0.016 0.607-0.235 1.105-0.708 1.388-1.281 0.525 0.112 1.127 0.033 1.673-0.241 0.535-0.269 0.921-0.681 1.113-1.163 0.531 0.028 1.077-0.16 1.529-0.548 0.765-0.655 0.976-1.673 0.533-2.592z"></path>
<path class="svg-icon__item" d="M13.333 8.666h-6c-0.368 0-0.667 0.299-0.667 0.667s0.299 0.667 0.667 0.667h6c0.368 0 0.667-0.299 0.667-0.667s-0.299-0.667-0.667-0.667z"></path>
<path class="svg-icon__item" d="M27.22 18.294c-0.207-0.305-0.62-0.389-0.925-0.181l-1.949 1.309c-0.305 0.205-0.387 0.62-0.181 0.925 0.129 0.191 0.34 0.295 0.555 0.295 0.127 0 0.256-0.036 0.371-0.113l1.949-1.309c0.305-0.205 0.387-0.62 0.181-0.925z"></path>
<path class="svg-icon__item" d="M23.081 21.476c-0.477-0.376-2.612-2.561-3.932-3.937-0.255-0.267-0.677-0.276-0.943-0.020-0.267 0.255-0.275 0.677-0.020 0.943 0.343 0.357 3.365 3.508 4.068 4.063 0.121 0.096 0.268 0.143 0.412 0.143 0.196 0 0.392-0.088 0.525-0.255 0.228-0.288 0.179-0.708-0.111-0.936z"></path>
<path class="svg-icon__item" d="M20.417 22.813c-0.799-0.639-2.805-2.771-3.259-3.264-0.251-0.272-0.671-0.288-0.943-0.040-0.271 0.249-0.289 0.672-0.040 0.943 0.024 0.025 2.419 2.611 3.408 3.403 0.123 0.097 0.271 0.145 0.416 0.145 0.195 0 0.389-0.087 0.521-0.249 0.229-0.288 0.183-0.708-0.104-0.937z"></path>
<path class="svg-icon__item" d="M17.764 24.156c-0.951-0.801-2.896-2.872-3.276-3.279-0.252-0.269-0.675-0.284-0.943-0.032-0.269 0.252-0.283 0.673-0.032 0.943 0.547 0.585 2.408 2.559 3.391 3.388 0.125 0.105 0.277 0.157 0.429 0.157 0.189 0 0.379-0.081 0.511-0.237 0.237-0.283 0.201-0.703-0.080-0.94z"></path>
<path class="svg-icon__item" d="M7.792 7.516c-1.143-1.083-5.712-1.433-7.085-1.515-0.189-0.009-0.364 0.053-0.497 0.18-0.133 0.125-0.209 0.301-0.209 0.485v12c0 0.368 0.299 0.667 0.667 0.667h4c0.288 0 0.544-0.185 0.633-0.46 0.097-0.299 2.395-7.349 2.697-10.816 0.017-0.203-0.057-0.403-0.205-0.541zM4.18 18h-2.847v-10.619c2.143 0.169 4.455 0.537 5.295 0.945-0.353 2.92-1.952 8.108-2.448 9.673z"></path>
<path class="svg-icon__item" d="M31.333 7.333c-5.235 0-8.139 1.34-8.26 1.396-0.173 0.081-0.303 0.232-0.356 0.415s-0.027 0.379 0.073 0.541c0.824 1.327 3.404 8.695 3.9 10.492 0.080 0.289 0.343 0.489 0.643 0.489h4c0.368 0 0.667-0.299 0.667-0.667v-12c0-0.369-0.299-0.667-0.667-0.667zM30.667 19.333h-2.835c-0.632-2.059-2.499-7.427-3.54-9.645 1.017-0.345 3.203-0.939 6.375-1.013v10.659z"></path>
</g>
</svg>
调整viewBox
另一种选择是去掉正方形,这样你就不必担心它。计算一个比较合适的viewBox
。然后您可以按照自己的喜好调整图标的大小和位置。如果您希望稍后恢复正方形,可以向其父容器添加边框样式。
那么我们如何确定正确的viewBox
应该是什么?嗯,有几种方法可以做到这一点。
您可以通过手动更改值来进行实验
您可以将其加载到矢量或 SVG 编辑器中并使用其标尺
getBBox()
您可以在 SVG 上使用 Javascript 方法。该getBBox()
函数返回内容的边界。这些将有助于告知viewBox
值应该是什么。
如果我们从 SVG 中删除正方形并使用最后一个方法,我们将得到边界框值:
{
"x": 0.0009997636079788208,
"y": 6.000142574310303,
"width": 31.998998641967773,
"height": 19.99785614013672
}
console.log(document.getElementById("test-svg").getBBox());
.svg-icon__border {
fill: none;
stroke: red;
}
<svg id="test-svg" class="c-section-heading__icon svg-icon" xmlns="http://www.w3.org/2000/svg" width="91" height="91" viewBox="0 0 91 91" >
<path class="svg-icon__item" d="M25.267 19.713c-0.628-1.309-2.875-3.127-5.045-4.883-1.211-0.979-2.355-1.904-3.084-2.633-0.183-0.184-0.456-0.243-0.697-0.156-0.449 0.163-0.727 0.288-0.945 0.385-0.333 0.149-0.445 0.2-0.895 0.245-0.199 0.020-0.377 0.127-0.488 0.292-0.943 1.409-1.919 1.289-2.571 1.071-0.208-0.069-0.245-0.159-0.265-0.244-0.14-0.585 0.563-1.948 1.473-2.859 2.167-2.168 3.284-2.711 5.644-1.656 2.677 1.197 5.36 2.135 5.387 2.144 0.351 0.121 0.728-0.063 0.849-0.411 0.12-0.348-0.063-0.728-0.411-0.849-0.027-0.009-2.656-0.928-5.28-2.103-3.057-1.367-4.735-0.467-7.131 1.931-0.912 0.912-2.151 2.757-1.831 4.111 0.137 0.576 0.543 1.003 1.145 1.201 1.511 0.499 2.889 0.021 3.916-1.341 0.424-0.065 0.655-0.161 1.001-0.317 0.125-0.056 0.272-0.121 0.467-0.2 0.763 0.716 1.792 1.549 2.876 2.425 1.96 1.585 4.183 3.383 4.683 4.423 0.247 0.513-0.019 0.848-0.199 1.001-0.264 0.227-0.625 0.299-0.821 0.161-0.216-0.148-0.497-0.157-0.72-0.024-0.224 0.133-0.349 0.385-0.321 0.644 0.045 0.424-0.343 0.667-0.511 0.751-0.427 0.216-0.872 0.179-1.039 0.024-0.187-0.173-0.455-0.224-0.692-0.136-0.237 0.089-0.403 0.308-0.427 0.561-0.040 0.437-0.364 0.857-0.787 1.021-0.204 0.077-0.5 0.124-0.765-0.119-0.165-0.149-0.395-0.207-0.609-0.155-0.217 0.053-0.392 0.211-0.468 0.42-0.025 0.067-0.083 0.227-0.707 0.227-0.444 0-1.243-0.3-1.633-0.559-0.468-0.308-3.403-2.497-5.937-4.62-0.356-0.3-0.972-0.943-1.516-1.511-0.483-0.504-0.924-0.961-1.151-1.153-0.284-0.24-0.704-0.204-0.94 0.079-0.237 0.281-0.203 0.703 0.079 0.94 0.207 0.175 0.607 0.597 1.048 1.057 0.595 0.621 1.209 1.264 1.623 1.611 2.483 2.079 5.467 4.323 6.061 4.713 0.491 0.323 1.548 0.776 2.367 0.776 0.657 0 1.163-0.151 1.513-0.445 0.469 0.183 1.003 0.184 1.516-0.016 0.607-0.235 1.105-0.708 1.388-1.281 0.525 0.112 1.127 0.033 1.673-0.241 0.535-0.269 0.921-0.681 1.113-1.163 0.531 0.028 1.077-0.16 1.529-0.548 0.765-0.655 0.976-1.673 0.533-2.592z"></path>
<path class="svg-icon__item" d="M13.333 8.666h-6c-0.368 0-0.667 0.299-0.667 0.667s0.299 0.667 0.667 0.667h6c0.368 0 0.667-0.299 0.667-0.667s-0.299-0.667-0.667-0.667z"></path>
<path class="svg-icon__item" d="M27.22 18.294c-0.207-0.305-0.62-0.389-0.925-0.181l-1.949 1.309c-0.305 0.205-0.387 0.62-0.181 0.925 0.129 0.191 0.34 0.295 0.555 0.295 0.127 0 0.256-0.036 0.371-0.113l1.949-1.309c0.305-0.205 0.387-0.62 0.181-0.925z"></path>
<path class="svg-icon__item" d="M23.081 21.476c-0.477-0.376-2.612-2.561-3.932-3.937-0.255-0.267-0.677-0.276-0.943-0.020-0.267 0.255-0.275 0.677-0.020 0.943 0.343 0.357 3.365 3.508 4.068 4.063 0.121 0.096 0.268 0.143 0.412 0.143 0.196 0 0.392-0.088 0.525-0.255 0.228-0.288 0.179-0.708-0.111-0.936z"></path>
<path class="svg-icon__item" d="M20.417 22.813c-0.799-0.639-2.805-2.771-3.259-3.264-0.251-0.272-0.671-0.288-0.943-0.040-0.271 0.249-0.289 0.672-0.040 0.943 0.024 0.025 2.419 2.611 3.408 3.403 0.123 0.097 0.271 0.145 0.416 0.145 0.195 0 0.389-0.087 0.521-0.249 0.229-0.288 0.183-0.708-0.104-0.937z"></path>
<path class="svg-icon__item" d="M17.764 24.156c-0.951-0.801-2.896-2.872-3.276-3.279-0.252-0.269-0.675-0.284-0.943-0.032-0.269 0.252-0.283 0.673-0.032 0.943 0.547 0.585 2.408 2.559 3.391 3.388 0.125 0.105 0.277 0.157 0.429 0.157 0.189 0 0.379-0.081 0.511-0.237 0.237-0.283 0.201-0.703-0.080-0.94z"></path>
<path class="svg-icon__item" d="M7.792 7.516c-1.143-1.083-5.712-1.433-7.085-1.515-0.189-0.009-0.364 0.053-0.497 0.18-0.133 0.125-0.209 0.301-0.209 0.485v12c0 0.368 0.299 0.667 0.667 0.667h4c0.288 0 0.544-0.185 0.633-0.46 0.097-0.299 2.395-7.349 2.697-10.816 0.017-0.203-0.057-0.403-0.205-0.541zM4.18 18h-2.847v-10.619c2.143 0.169 4.455 0.537 5.295 0.945-0.353 2.92-1.952 8.108-2.448 9.673z"></path>
<path class="svg-icon__item" d="M31.333 7.333c-5.235 0-8.139 1.34-8.26 1.396-0.173 0.081-0.303 0.232-0.356 0.415s-0.027 0.379 0.073 0.541c0.824 1.327 3.404 8.695 3.9 10.492 0.080 0.289 0.343 0.489 0.643 0.489h4c0.368 0 0.667-0.299 0.667-0.667v-12c0-0.369-0.299-0.667-0.667-0.667zM30.667 19.333h-2.835c-0.632-2.059-2.499-7.427-3.54-9.645 1.017-0.345 3.203-0.939 6.375-1.013v10.659z"></path>
</svg>
从这些值中我们可以看到我们的图标在 X 方向上覆盖了(大约)0 到 32,在 Y 方向上覆盖了 6 到 20。
由此可见,该图标的设计适合 32x32 SVG。所以让我们将其设置viewBox
为:
viewBox="0 0 32 32"
这就是看起来的样子,仍然是width="91" height="91"
. 我还在 SVG 周围添加了绿色边框,作为我们删除的矩形的替代。
svg {
border: solid 1px green;
}
<svg id="test-svg" class="c-section-heading__icon svg-icon" width="91" height="91" viewBox="0 0 32 32" >
<path class="svg-icon__item" d="M25.267 19.713c-0.628-1.309-2.875-3.127-5.045-4.883-1.211-0.979-2.355-1.904-3.084-2.633-0.183-0.184-0.456-0.243-0.697-0.156-0.449 0.163-0.727 0.288-0.945 0.385-0.333 0.149-0.445 0.2-0.895 0.245-0.199 0.020-0.377 0.127-0.488 0.292-0.943 1.409-1.919 1.289-2.571 1.071-0.208-0.069-0.245-0.159-0.265-0.244-0.14-0.585 0.563-1.948 1.473-2.859 2.167-2.168 3.284-2.711 5.644-1.656 2.677 1.197 5.36 2.135 5.387 2.144 0.351 0.121 0.728-0.063 0.849-0.411 0.12-0.348-0.063-0.728-0.411-0.849-0.027-0.009-2.656-0.928-5.28-2.103-3.057-1.367-4.735-0.467-7.131 1.931-0.912 0.912-2.151 2.757-1.831 4.111 0.137 0.576 0.543 1.003 1.145 1.201 1.511 0.499 2.889 0.021 3.916-1.341 0.424-0.065 0.655-0.161 1.001-0.317 0.125-0.056 0.272-0.121 0.467-0.2 0.763 0.716 1.792 1.549 2.876 2.425 1.96 1.585 4.183 3.383 4.683 4.423 0.247 0.513-0.019 0.848-0.199 1.001-0.264 0.227-0.625 0.299-0.821 0.161-0.216-0.148-0.497-0.157-0.72-0.024-0.224 0.133-0.349 0.385-0.321 0.644 0.045 0.424-0.343 0.667-0.511 0.751-0.427 0.216-0.872 0.179-1.039 0.024-0.187-0.173-0.455-0.224-0.692-0.136-0.237 0.089-0.403 0.308-0.427 0.561-0.040 0.437-0.364 0.857-0.787 1.021-0.204 0.077-0.5 0.124-0.765-0.119-0.165-0.149-0.395-0.207-0.609-0.155-0.217 0.053-0.392 0.211-0.468 0.42-0.025 0.067-0.083 0.227-0.707 0.227-0.444 0-1.243-0.3-1.633-0.559-0.468-0.308-3.403-2.497-5.937-4.62-0.356-0.3-0.972-0.943-1.516-1.511-0.483-0.504-0.924-0.961-1.151-1.153-0.284-0.24-0.704-0.204-0.94 0.079-0.237 0.281-0.203 0.703 0.079 0.94 0.207 0.175 0.607 0.597 1.048 1.057 0.595 0.621 1.209 1.264 1.623 1.611 2.483 2.079 5.467 4.323 6.061 4.713 0.491 0.323 1.548 0.776 2.367 0.776 0.657 0 1.163-0.151 1.513-0.445 0.469 0.183 1.003 0.184 1.516-0.016 0.607-0.235 1.105-0.708 1.388-1.281 0.525 0.112 1.127 0.033 1.673-0.241 0.535-0.269 0.921-0.681 1.113-1.163 0.531 0.028 1.077-0.16 1.529-0.548 0.765-0.655 0.976-1.673 0.533-2.592z"></path>
<path class="svg-icon__item" d="M13.333 8.666h-6c-0.368 0-0.667 0.299-0.667 0.667s0.299 0.667 0.667 0.667h6c0.368 0 0.667-0.299 0.667-0.667s-0.299-0.667-0.667-0.667z"></path>
<path class="svg-icon__item" d="M27.22 18.294c-0.207-0.305-0.62-0.389-0.925-0.181l-1.949 1.309c-0.305 0.205-0.387 0.62-0.181 0.925 0.129 0.191 0.34 0.295 0.555 0.295 0.127 0 0.256-0.036 0.371-0.113l1.949-1.309c0.305-0.205 0.387-0.62 0.181-0.925z"></path>
<path class="svg-icon__item" d="M23.081 21.476c-0.477-0.376-2.612-2.561-3.932-3.937-0.255-0.267-0.677-0.276-0.943-0.020-0.267 0.255-0.275 0.677-0.020 0.943 0.343 0.357 3.365 3.508 4.068 4.063 0.121 0.096 0.268 0.143 0.412 0.143 0.196 0 0.392-0.088 0.525-0.255 0.228-0.288 0.179-0.708-0.111-0.936z"></path>
<path class="svg-icon__item" d="M20.417 22.813c-0.799-0.639-2.805-2.771-3.259-3.264-0.251-0.272-0.671-0.288-0.943-0.040-0.271 0.249-0.289 0.672-0.040 0.943 0.024 0.025 2.419 2.611 3.408 3.403 0.123 0.097 0.271 0.145 0.416 0.145 0.195 0 0.389-0.087 0.521-0.249 0.229-0.288 0.183-0.708-0.104-0.937z"></path>
<path class="svg-icon__item" d="M17.764 24.156c-0.951-0.801-2.896-2.872-3.276-3.279-0.252-0.269-0.675-0.284-0.943-0.032-0.269 0.252-0.283 0.673-0.032 0.943 0.547 0.585 2.408 2.559 3.391 3.388 0.125 0.105 0.277 0.157 0.429 0.157 0.189 0 0.379-0.081 0.511-0.237 0.237-0.283 0.201-0.703-0.080-0.94z"></path>
<path class="svg-icon__item" d="M7.792 7.516c-1.143-1.083-5.712-1.433-7.085-1.515-0.189-0.009-0.364 0.053-0.497 0.18-0.133 0.125-0.209 0.301-0.209 0.485v12c0 0.368 0.299 0.667 0.667 0.667h4c0.288 0 0.544-0.185 0.633-0.46 0.097-0.299 2.395-7.349 2.697-10.816 0.017-0.203-0.057-0.403-0.205-0.541zM4.18 18h-2.847v-10.619c2.143 0.169 4.455 0.537 5.295 0.945-0.353 2.92-1.952 8.108-2.448 9.673z"></path>
<path class="svg-icon__item" d="M31.333 7.333c-5.235 0-8.139 1.34-8.26 1.396-0.173 0.081-0.303 0.232-0.356 0.415s-0.027 0.379 0.073 0.541c0.824 1.327 3.404 8.695 3.9 10.492 0.080 0.289 0.343 0.489 0.643 0.489h4c0.368 0 0.667-0.299 0.667-0.667v-12c0-0.369-0.299-0.667-0.667-0.667zM30.667 19.333h-2.835c-0.632-2.059-2.499-7.427-3.54-9.645 1.017-0.345 3.203-0.939 6.375-1.013v10.659z"></path>
</svg>
下一步
现在我们有了“正确的”,viewBox
您可以看到手正在按比例放大以填充我们的视口( 和width
)height
区域。但这确实意味着原始图标周围的填充消失了。
要恢复此填充,您有以下几种选择:
将填充合并到
viewBox
. 您可以通过从<minimum X>
和 中的<minimum Y>
值减去一个金额并向和中的值viewBox
添加额外的值来完成此操作。例如,如果我们在 32x32 形状周围添加 16 个单位的填充,则调整后的结果为:<width>
<height>
viewBox
视图框=“-16 -16 64 64”
演示:
svg {
border: solid 1px green;
}
<svg id="test-svg" class="c-section-heading__icon svg-icon" width="91" height="91" viewBox="-16 -16 64 64" >
<path class="svg-icon__item" d="M25.267 19.713c-0.628-1.309-2.875-3.127-5.045-4.883-1.211-0.979-2.355-1.904-3.084-2.633-0.183-0.184-0.456-0.243-0.697-0.156-0.449 0.163-0.727 0.288-0.945 0.385-0.333 0.149-0.445 0.2-0.895 0.245-0.199 0.020-0.377 0.127-0.488 0.292-0.943 1.409-1.919 1.289-2.571 1.071-0.208-0.069-0.245-0.159-0.265-0.244-0.14-0.585 0.563-1.948 1.473-2.859 2.167-2.168 3.284-2.711 5.644-1.656 2.677 1.197 5.36 2.135 5.387 2.144 0.351 0.121 0.728-0.063 0.849-0.411 0.12-0.348-0.063-0.728-0.411-0.849-0.027-0.009-2.656-0.928-5.28-2.103-3.057-1.367-4.735-0.467-7.131 1.931-0.912 0.912-2.151 2.757-1.831 4.111 0.137 0.576 0.543 1.003 1.145 1.201 1.511 0.499 2.889 0.021 3.916-1.341 0.424-0.065 0.655-0.161 1.001-0.317 0.125-0.056 0.272-0.121 0.467-0.2 0.763 0.716 1.792 1.549 2.876 2.425 1.96 1.585 4.183 3.383 4.683 4.423 0.247 0.513-0.019 0.848-0.199 1.001-0.264 0.227-0.625 0.299-0.821 0.161-0.216-0.148-0.497-0.157-0.72-0.024-0.224 0.133-0.349 0.385-0.321 0.644 0.045 0.424-0.343 0.667-0.511 0.751-0.427 0.216-0.872 0.179-1.039 0.024-0.187-0.173-0.455-0.224-0.692-0.136-0.237 0.089-0.403 0.308-0.427 0.561-0.040 0.437-0.364 0.857-0.787 1.021-0.204 0.077-0.5 0.124-0.765-0.119-0.165-0.149-0.395-0.207-0.609-0.155-0.217 0.053-0.392 0.211-0.468 0.42-0.025 0.067-0.083 0.227-0.707 0.227-0.444 0-1.243-0.3-1.633-0.559-0.468-0.308-3.403-2.497-5.937-4.62-0.356-0.3-0.972-0.943-1.516-1.511-0.483-0.504-0.924-0.961-1.151-1.153-0.284-0.24-0.704-0.204-0.94 0.079-0.237 0.281-0.203 0.703 0.079 0.94 0.207 0.175 0.607 0.597 1.048 1.057 0.595 0.621 1.209 1.264 1.623 1.611 2.483 2.079 5.467 4.323 6.061 4.713 0.491 0.323 1.548 0.776 2.367 0.776 0.657 0 1.163-0.151 1.513-0.445 0.469 0.183 1.003 0.184 1.516-0.016 0.607-0.235 1.105-0.708 1.388-1.281 0.525 0.112 1.127 0.033 1.673-0.241 0.535-0.269 0.921-0.681 1.113-1.163 0.531 0.028 1.077-0.16 1.529-0.548 0.765-0.655 0.976-1.673 0.533-2.592z"></path>
<path class="svg-icon__item" d="M13.333 8.666h-6c-0.368 0-0.667 0.299-0.667 0.667s0.299 0.667 0.667 0.667h6c0.368 0 0.667-0.299 0.667-0.667s-0.299-0.667-0.667-0.667z"></path>
<path class="svg-icon__item" d="M27.22 18.294c-0.207-0.305-0.62-0.389-0.925-0.181l-1.949 1.309c-0.305 0.205-0.387 0.62-0.181 0.925 0.129 0.191 0.34 0.295 0.555 0.295 0.127 0 0.256-0.036 0.371-0.113l1.949-1.309c0.305-0.205 0.387-0.62 0.181-0.925z"></path>
<path class="svg-icon__item" d="M23.081 21.476c-0.477-0.376-2.612-2.561-3.932-3.937-0.255-0.267-0.677-0.276-0.943-0.020-0.267 0.255-0.275 0.677-0.020 0.943 0.343 0.357 3.365 3.508 4.068 4.063 0.121 0.096 0.268 0.143 0.412 0.143 0.196 0 0.392-0.088 0.525-0.255 0.228-0.288 0.179-0.708-0.111-0.936z"></path>
<path class="svg-icon__item" d="M20.417 22.813c-0.799-0.639-2.805-2.771-3.259-3.264-0.251-0.272-0.671-0.288-0.943-0.040-0.271 0.249-0.289 0.672-0.040 0.943 0.024 0.025 2.419 2.611 3.408 3.403 0.123 0.097 0.271 0.145 0.416 0.145 0.195 0 0.389-0.087 0.521-0.249 0.229-0.288 0.183-0.708-0.104-0.937z"></path>
<path class="svg-icon__item" d="M17.764 24.156c-0.951-0.801-2.896-2.872-3.276-3.279-0.252-0.269-0.675-0.284-0.943-0.032-0.269 0.252-0.283 0.673-0.032 0.943 0.547 0.585 2.408 2.559 3.391 3.388 0.125 0.105 0.277 0.157 0.429 0.157 0.189 0 0.379-0.081 0.511-0.237 0.237-0.283 0.201-0.703-0.080-0.94z"></path>
<path class="svg-icon__item" d="M7.792 7.516c-1.143-1.083-5.712-1.433-7.085-1.515-0.189-0.009-0.364 0.053-0.497 0.18-0.133 0.125-0.209 0.301-0.209 0.485v12c0 0.368 0.299 0.667 0.667 0.667h4c0.288 0 0.544-0.185 0.633-0.46 0.097-0.299 2.395-7.349 2.697-10.816 0.017-0.203-0.057-0.403-0.205-0.541zM4.18 18h-2.847v-10.619c2.143 0.169 4.455 0.537 5.295 0.945-0.353 2.92-1.952 8.108-2.448 9.673z"></path>
<path class="svg-icon__item" d="M31.333 7.333c-5.235 0-8.139 1.34-8.26 1.396-0.173 0.081-0.303 0.232-0.356 0.415s-0.027 0.379 0.073 0.541c0.824 1.327 3.404 8.695 3.9 10.492 0.080 0.289 0.343 0.489 0.643 0.489h4c0.368 0 0.667-0.299 0.667-0.667v-12c0-0.369-0.299-0.667-0.667-0.667zM30.667 19.333h-2.835c-0.632-2.059-2.499-7.427-3.54-9.645 1.017-0.345 3.203-0.939 6.375-1.013v10.659z"></path>
</svg>
或者,您可以使用 CSS 样式来调整容器内新图标的大小和位置<div>
。
演示:
div.icon-container {
display: inline-block;
padding: 23px;
border: solid 1px green;
}
div.icon-container svg {
width: 45px;
height: 45px;
}
<div class="icon-container">
<svg id="test-svg" class="c-section-heading__icon svg-icon" viewBox=" 0 0 32 32" >
<path class="svg-icon__item" d="M25.267 19.713c-0.628-1.309-2.875-3.127-5.045-4.883-1.211-0.979-2.355-1.904-3.084-2.633-0.183-0.184-0.456-0.243-0.697-0.156-0.449 0.163-0.727 0.288-0.945 0.385-0.333 0.149-0.445 0.2-0.895 0.245-0.199 0.020-0.377 0.127-0.488 0.292-0.943 1.409-1.919 1.289-2.571 1.071-0.208-0.069-0.245-0.159-0.265-0.244-0.14-0.585 0.563-1.948 1.473-2.859 2.167-2.168 3.284-2.711 5.644-1.656 2.677 1.197 5.36 2.135 5.387 2.144 0.351 0.121 0.728-0.063 0.849-0.411 0.12-0.348-0.063-0.728-0.411-0.849-0.027-0.009-2.656-0.928-5.28-2.103-3.057-1.367-4.735-0.467-7.131 1.931-0.912 0.912-2.151 2.757-1.831 4.111 0.137 0.576 0.543 1.003 1.145 1.201 1.511 0.499 2.889 0.021 3.916-1.341 0.424-0.065 0.655-0.161 1.001-0.317 0.125-0.056 0.272-0.121 0.467-0.2 0.763 0.716 1.792 1.549 2.876 2.425 1.96 1.585 4.183 3.383 4.683 4.423 0.247 0.513-0.019 0.848-0.199 1.001-0.264 0.227-0.625 0.299-0.821 0.161-0.216-0.148-0.497-0.157-0.72-0.024-0.224 0.133-0.349 0.385-0.321 0.644 0.045 0.424-0.343 0.667-0.511 0.751-0.427 0.216-0.872 0.179-1.039 0.024-0.187-0.173-0.455-0.224-0.692-0.136-0.237 0.089-0.403 0.308-0.427 0.561-0.040 0.437-0.364 0.857-0.787 1.021-0.204 0.077-0.5 0.124-0.765-0.119-0.165-0.149-0.395-0.207-0.609-0.155-0.217 0.053-0.392 0.211-0.468 0.42-0.025 0.067-0.083 0.227-0.707 0.227-0.444 0-1.243-0.3-1.633-0.559-0.468-0.308-3.403-2.497-5.937-4.62-0.356-0.3-0.972-0.943-1.516-1.511-0.483-0.504-0.924-0.961-1.151-1.153-0.284-0.24-0.704-0.204-0.94 0.079-0.237 0.281-0.203 0.703 0.079 0.94 0.207 0.175 0.607 0.597 1.048 1.057 0.595 0.621 1.209 1.264 1.623 1.611 2.483 2.079 5.467 4.323 6.061 4.713 0.491 0.323 1.548 0.776 2.367 0.776 0.657 0 1.163-0.151 1.513-0.445 0.469 0.183 1.003 0.184 1.516-0.016 0.607-0.235 1.105-0.708 1.388-1.281 0.525 0.112 1.127 0.033 1.673-0.241 0.535-0.269 0.921-0.681 1.113-1.163 0.531 0.028 1.077-0.16 1.529-0.548 0.765-0.655 0.976-1.673 0.533-2.592z"></path>
<path class="svg-icon__item" d="M13.333 8.666h-6c-0.368 0-0.667 0.299-0.667 0.667s0.299 0.667 0.667 0.667h6c0.368 0 0.667-0.299 0.667-0.667s-0.299-0.667-0.667-0.667z"></path>
<path class="svg-icon__item" d="M27.22 18.294c-0.207-0.305-0.62-0.389-0.925-0.181l-1.949 1.309c-0.305 0.205-0.387 0.62-0.181 0.925 0.129 0.191 0.34 0.295 0.555 0.295 0.127 0 0.256-0.036 0.371-0.113l1.949-1.309c0.305-0.205 0.387-0.62 0.181-0.925z"></path>
<path class="svg-icon__item" d="M23.081 21.476c-0.477-0.376-2.612-2.561-3.932-3.937-0.255-0.267-0.677-0.276-0.943-0.020-0.267 0.255-0.275 0.677-0.020 0.943 0.343 0.357 3.365 3.508 4.068 4.063 0.121 0.096 0.268 0.143 0.412 0.143 0.196 0 0.392-0.088 0.525-0.255 0.228-0.288 0.179-0.708-0.111-0.936z"></path>
<path class="svg-icon__item" d="M20.417 22.813c-0.799-0.639-2.805-2.771-3.259-3.264-0.251-0.272-0.671-0.288-0.943-0.040-0.271 0.249-0.289 0.672-0.040 0.943 0.024 0.025 2.419 2.611 3.408 3.403 0.123 0.097 0.271 0.145 0.416 0.145 0.195 0 0.389-0.087 0.521-0.249 0.229-0.288 0.183-0.708-0.104-0.937z"></path>
<path class="svg-icon__item" d="M17.764 24.156c-0.951-0.801-2.896-2.872-3.276-3.279-0.252-0.269-0.675-0.284-0.943-0.032-0.269 0.252-0.283 0.673-0.032 0.943 0.547 0.585 2.408 2.559 3.391 3.388 0.125 0.105 0.277 0.157 0.429 0.157 0.189 0 0.379-0.081 0.511-0.237 0.237-0.283 0.201-0.703-0.080-0.94z"></path>
<path class="svg-icon__item" d="M7.792 7.516c-1.143-1.083-5.712-1.433-7.085-1.515-0.189-0.009-0.364 0.053-0.497 0.18-0.133 0.125-0.209 0.301-0.209 0.485v12c0 0.368 0.299 0.667 0.667 0.667h4c0.288 0 0.544-0.185 0.633-0.46 0.097-0.299 2.395-7.349 2.697-10.816 0.017-0.203-0.057-0.403-0.205-0.541zM4.18 18h-2.847v-10.619c2.143 0.169 4.455 0.537 5.295 0.945-0.353 2.92-1.952 8.108-2.448 9.673z"></path>
<path class="svg-icon__item" d="M31.333 7.333c-5.235 0-8.139 1.34-8.26 1.396-0.173 0.081-0.303 0.232-0.356 0.415s-0.027 0.379 0.073 0.541c0.824 1.327 3.404 8.695 3.9 10.492 0.080 0.289 0.343 0.489 0.643 0.489h4c0.368 0 0.667-0.299 0.667-0.667v-12c0-0.369-0.299-0.667-0.667-0.667zM30.667 19.333h-2.835c-0.632-2.059-2.499-7.427-3.54-9.645 1.017-0.345 3.203-0.939 6.375-1.013v10.659z"></path>
</svg>
</div>
TA贡献1966条经验 获得超4个赞
您需要将握手包装在符号中,并为该符号提供 viewBox 属性。接下来,您使用该符号并为元素提供use所需的位置(x,y)和大小(宽度,高度)。
<use xlink:href="#handshake" x=".5" y=".5" width="90" height="90" />
为了知道 viewBox 的值,我首先将握手包装在一个g元素中,并使用了该getBBox()方法。
<svg class="c-section-heading__icon svg-icon" xmlns="http://www.w3.org/2000/svg" width="91" height="91" viewBox="0 0 91 91" >
<rect class="svg-icon__border" x="0.5" y="0.5" width="90" height="90" fill="gold"/>
<symbol id="handshake" viewBox="0 0 32 32">
<path class="svg-icon__item" d="M25.267 19.713c-0.628-1.309-2.875-3.127-5.045-4.883-1.211-0.979-2.355-1.904-3.084-2.633-0.183-0.184-0.456-0.243-0.697-0.156-0.449 0.163-0.727 0.288-0.945 0.385-0.333 0.149-0.445 0.2-0.895 0.245-0.199 0.020-0.377 0.127-0.488 0.292-0.943 1.409-1.919 1.289-2.571 1.071-0.208-0.069-0.245-0.159-0.265-0.244-0.14-0.585 0.563-1.948 1.473-2.859 2.167-2.168 3.284-2.711 5.644-1.656 2.677 1.197 5.36 2.135 5.387 2.144 0.351 0.121 0.728-0.063 0.849-0.411 0.12-0.348-0.063-0.728-0.411-0.849-0.027-0.009-2.656-0.928-5.28-2.103-3.057-1.367-4.735-0.467-7.131 1.931-0.912 0.912-2.151 2.757-1.831 4.111 0.137 0.576 0.543 1.003 1.145 1.201 1.511 0.499 2.889 0.021 3.916-1.341 0.424-0.065 0.655-0.161 1.001-0.317 0.125-0.056 0.272-0.121 0.467-0.2 0.763 0.716 1.792 1.549 2.876 2.425 1.96 1.585 4.183 3.383 4.683 4.423 0.247 0.513-0.019 0.848-0.199 1.001-0.264 0.227-0.625 0.299-0.821 0.161-0.216-0.148-0.497-0.157-0.72-0.024-0.224 0.133-0.349 0.385-0.321 0.644 0.045 0.424-0.343 0.667-0.511 0.751-0.427 0.216-0.872 0.179-1.039 0.024-0.187-0.173-0.455-0.224-0.692-0.136-0.237 0.089-0.403 0.308-0.427 0.561-0.040 0.437-0.364 0.857-0.787 1.021-0.204 0.077-0.5 0.124-0.765-0.119-0.165-0.149-0.395-0.207-0.609-0.155-0.217 0.053-0.392 0.211-0.468 0.42-0.025 0.067-0.083 0.227-0.707 0.227-0.444 0-1.243-0.3-1.633-0.559-0.468-0.308-3.403-2.497-5.937-4.62-0.356-0.3-0.972-0.943-1.516-1.511-0.483-0.504-0.924-0.961-1.151-1.153-0.284-0.24-0.704-0.204-0.94 0.079-0.237 0.281-0.203 0.703 0.079 0.94 0.207 0.175 0.607 0.597 1.048 1.057 0.595 0.621 1.209 1.264 1.623 1.611 2.483 2.079 5.467 4.323 6.061 4.713 0.491 0.323 1.548 0.776 2.367 0.776 0.657 0 1.163-0.151 1.513-0.445 0.469 0.183 1.003 0.184 1.516-0.016 0.607-0.235 1.105-0.708 1.388-1.281 0.525 0.112 1.127 0.033 1.673-0.241 0.535-0.269 0.921-0.681 1.113-1.163 0.531 0.028 1.077-0.16 1.529-0.548 0.765-0.655 0.976-1.673 0.533-2.592z"></path>
<path class="svg-icon__item" d="M13.333 8.666h-6c-0.368 0-0.667 0.299-0.667 0.667s0.299 0.667 0.667 0.667h6c0.368 0 0.667-0.299 0.667-0.667s-0.299-0.667-0.667-0.667z"></path>
<path class="svg-icon__item" d="M27.22 18.294c-0.207-0.305-0.62-0.389-0.925-0.181l-1.949 1.309c-0.305 0.205-0.387 0.62-0.181 0.925 0.129 0.191 0.34 0.295 0.555 0.295 0.127 0 0.256-0.036 0.371-0.113l1.949-1.309c0.305-0.205 0.387-0.62 0.181-0.925z"></path>
<path class="svg-icon__item" d="M23.081 21.476c-0.477-0.376-2.612-2.561-3.932-3.937-0.255-0.267-0.677-0.276-0.943-0.020-0.267 0.255-0.275 0.677-0.020 0.943 0.343 0.357 3.365 3.508 4.068 4.063 0.121 0.096 0.268 0.143 0.412 0.143 0.196 0 0.392-0.088 0.525-0.255 0.228-0.288 0.179-0.708-0.111-0.936z"></path>
<path class="svg-icon__item" d="M20.417 22.813c-0.799-0.639-2.805-2.771-3.259-3.264-0.251-0.272-0.671-0.288-0.943-0.040-0.271 0.249-0.289 0.672-0.040 0.943 0.024 0.025 2.419 2.611 3.408 3.403 0.123 0.097 0.271 0.145 0.416 0.145 0.195 0 0.389-0.087 0.521-0.249 0.229-0.288 0.183-0.708-0.104-0.937z"></path>
<path class="svg-icon__item" d="M17.764 24.156c-0.951-0.801-2.896-2.872-3.276-3.279-0.252-0.269-0.675-0.284-0.943-0.032-0.269 0.252-0.283 0.673-0.032 0.943 0.547 0.585 2.408 2.559 3.391 3.388 0.125 0.105 0.277 0.157 0.429 0.157 0.189 0 0.379-0.081 0.511-0.237 0.237-0.283 0.201-0.703-0.080-0.94z"></path>
<path class="svg-icon__item" d="M7.792 7.516c-1.143-1.083-5.712-1.433-7.085-1.515-0.189-0.009-0.364 0.053-0.497 0.18-0.133 0.125-0.209 0.301-0.209 0.485v12c0 0.368 0.299 0.667 0.667 0.667h4c0.288 0 0.544-0.185 0.633-0.46 0.097-0.299 2.395-7.349 2.697-10.816 0.017-0.203-0.057-0.403-0.205-0.541zM4.18 18h-2.847v-10.619c2.143 0.169 4.455 0.537 5.295 0.945-0.353 2.92-1.952 8.108-2.448 9.673z"></path>
<path class="svg-icon__item" d="M31.333 7.333c-5.235 0-8.139 1.34-8.26 1.396-0.173 0.081-0.303 0.232-0.356 0.415s-0.027 0.379 0.073 0.541c0.824 1.327 3.404 8.695 3.9 10.492 0.080 0.289 0.343 0.489 0.643 0.489h4c0.368 0 0.667-0.299 0.667-0.667v-12c0-0.369-0.299-0.667-0.667-0.667zM30.667 19.333h-2.835c-0.632-2.059-2.499-7.427-3.54-9.645 1.017-0.345 3.203-0.939 6.375-1.013v10.659z"></path>
</symbol>
<use xlink:href="#handshake" x=".5" y=".5" width="90" height="90" />
</svg>
- 2 回答
- 0 关注
- 117 浏览
添加回答
举报