【问题标题】:Fit Path to SVG适合 SVG 的路径
【发布时间】:2020-02-19 04:35:15
【问题描述】:

我有一个来自 fontawesome 的 SVG,但 SVG 窗口内的路径内容周围有空格。

<svg aria-hidden="true" focusable="false" data-prefix="fab" data-icon="youtube" class="svg-inline--fa fa-youtube fa-w-18" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512">
<path fill="currentColor" d="M549.655 124.083c-6.281-23.65-24.787-42.276-48.284-48.597C458.781 64 288 64 288 64S117.22 64 74.629 75.486c-23.497 6.322-42.003 24.947-48.284 48.597-11.412 42.867-11.412 132.305-11.412 132.305s0 89.438 11.412 132.305c6.281 23.65 24.787 41.5 48.284 47.821C117.22 448 288 448 288 448s170.78 0 213.371-11.486c23.497-6.321 42.003-24.171 48.284-47.821 11.412-42.867 11.412-132.305 11.412-132.305s0-89.438-11.412-132.305zm-317.51 213.508V175.185l142.739 81.205-142.739 81.201z"></path>
</svg>

如何删除这个空间并让路径边缘接触 SVG 窗口? CSS 更可取,但对其他解决方案开放。

【问题讨论】:

  • 这不是你必须拥有的唯一代码,因为我试过了,但我没有看到任何间距,可能你正在使用display: inline-block;,它通常会在内联上水平抛出4px间距元素。在我看到你所有的代码之前帮不了你
  • 为了通过 css 设置它,github.com/w3c/fxtf-drafts/issues/7 在几年内没有太大变化。因此,唯一的其他 CSS 解决方案是缩放 ,但这需要为每个字形设置硬编码值。
  • @Mr.Alien 抱歉,示例代码有误,svg 已更新为您在此处的图片中看到的内容。谢谢。

标签: css svg


【解决方案1】:

我还没有找到 CSS 解决方案,但是您可以在 JavaScript 中通过将 SVG 的视图框尺寸设置为边界框的尺寸来实现:

const svg = document.querySelector("svg");
const {x, y, width, height} = svg.getBBox();

svg.setAttribute("viewBox", `${x} ${y} ${width} ${height}`);
body {
  margin: 0;
  padding: 0;
}
<svg aria-hidden="true" focusable="false" class="svg-inline--fa fa-instagram fa-w-14" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
<path
fill="currentColor"
d="M224.1 141c-63.6 0-114.9 51.3-114.9 114.9s51.3 114.9 114.9 114.9S339 319.5 339 255.9 287.7 141 224.1 141zm0 189.6c-41.1 0-74.7-33.5-74.7-74.7s33.5-74.7 74.7-74.7 74.7 33.5 74.7 74.7-33.6 74.7-74.7 74.7zm146.4-194.3c0 14.9-12 26.8-26.8 26.8-14.9 0-26.8-12-26.8-26.8s12-26.8 26.8-26.8 26.8 12 26.8 26.8zm76.1 27.2c-1.7-35.9-9.9-67.7-36.2-93.9-26.2-26.2-58-34.4-93.9-36.2-37-2.1-147.9-2.1-184.9 0-35.8 1.7-67.6 9.9-93.9 36.1s-34.4 58-36.2 93.9c-2.1 37-2.1 147.9 0 184.9 1.7 35.9 9.9 67.7 36.2 93.9s58 34.4 93.9 36.2c37 2.1 147.9 2.1 184.9 0 35.9-1.7 67.7-9.9 93.9-36.2 26.2-26.2 34.4-58 36.2-93.9 2.1-37 2.1-147.8 0-184.8zM398.8 388c-7.8 19.6-22.9 34.7-42.6 42.6-29.5 11.7-99.5 9-132.1 9s-102.7 2.6-132.1-9c-19.6-7.8-34.7-22.9-42.6-42.6-11.7-29.5-9-99.5-9-132.1s-2.6-102.7 9-132.1c7.8-19.6 22.9-34.7 42.6-42.6 29.5-11.7 99.5-9 132.1-9s102.7-2.6 132.1 9c19.6 7.8 34.7 22.9 42.6 42.6 11.7 29.5 9 99.5 9 132.1s2.7 102.7-9 132.1z">
</path>
</svg>

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2013-04-12
    • 2013-07-11
    • 2017-07-04
    相关资源
    最近更新 更多