简体   繁体   English

在div中输出字体真棒svg图标

[英]Output font awesome svg icon inside div

I need to display a font awesome icon inside a div. 我需要在div中显示一个很棒的字体图标。 I've downloaded this icon from here https://fontawesome.com/icons/share-alt-square?style=solid , then I opened the downloaded svg file with a text editor, copied data of d attribute and placed into my markup. 我已经从这里https://fontawesome.com/icons/share-alt-square?style=solid下载了此图标,然后使用文本编辑器打开了下载的svg文件,复制了d属性的数据并放入了我的标记中。

Final markup should look like this: 最终标记应如下所示:

<div style="width: 32px; height: 32px;">
  <svg viewBox="0 0 64 64" width="32" height="32">
    <g>
      <rect width="64" height="64" fill=""></rect>
    </g>
    <g>
    <path d="M448 80v352c0 26.51-21.49 48-48 48H48c-26.51 0-48-21.49-48-48V80c0-26.51 21.49-48 48-48h352c26.51 0 48 21.49 48 48zM304 296c-14.562 0-27.823 5.561-37.783 14.671l-67.958-40.775a56.339 56.339 0 0 0 0-27.793l67.958-40.775C276.177 210.439 289.438 216 304 216c30.928 0 56-25.072 56-56s-25.072-56-56-56-56 25.072-56 56c0 4.797.605 9.453 1.74 13.897l-67.958 40.775C171.823 205.561 158.562 200 144 200c-30.928 0-56 25.072-56 56s25.072 56 56 56c14.562 0 27.823-5.561 37.783-14.671l67.958 40.775a56.088 56.088 0 0 0-1.74 13.897c0 30.928 25.072 56 56 56s56-25.072 56-56C360 321.072 334.928 296 304 296z" fill="white"></path>
    </g>
  </svg>
</div>

I prefer such approach. 我更喜欢这种方法。 Please correct me if I use it in a wrong way and it will not work. 如果我以错误的方式使用它,请纠正我,它将不起作用。 In that case what would be the most appropriate solution? 在这种情况下,最合适的解决方案是什么?

The problem is that the path you are using is from an svg with a much bigger viewBox . 问题是您正在使用的路径来自具有更大viewBox的svg。 So in order to adapt it to what you have you will need to change the viewBox of your svg. 因此,为了使其适应您的需求,您需要更改svg的viewBox

In order to know what viewBox to use, you may use the viewBox of the original icon. 为了知道使用什么viewBox ,您可以使用原始图标的viewBox An other way is getting the size and position of the bounding box (javascript method: thePath.getBBox() ) of the path and use it to build the new value of the viewBox . 另一种方法是获取路径的边界框的大小和位置(javascript方法: thePath.getBBox() ),并使用它来构建viewBox的新值。

 svg{border:1px solid} 
 <div style="width: 32px; height: 32px;"> <svg viewBox="0 32 448 448"> <g> <rect y="32" width="448" height="448" fill=""></rect> </g> <g> <path id="thePath" d="M448 80v352c0 26.51-21.49 48-48 48H48c-26.51 0-48-21.49-48-48V80c0-26.51 21.49-48 48-48h352c26.51 0 48 21.49 48 48zM304 296c-14.562 0-27.823 5.561-37.783 14.671l-67.958-40.775a56.339 56.339 0 0 0 0-27.793l67.958-40.775C276.177 210.439 289.438 216 304 216c30.928 0 56-25.072 56-56s-25.072-56-56-56-56 25.072-56 56c0 4.797.605 9.453 1.74 13.897l-67.958 40.775C171.823 205.561 158.562 200 144 200c-30.928 0-56 25.072-56 56s25.072 56 56 56c14.562 0 27.823-5.561 37.783-14.671l67.958 40.775a56.088 56.088 0 0 0-1.74 13.897c0 30.928 25.072 56 56 56s56-25.072 56-56C360 321.072 334.928 296 304 296z" fill="white"></path> </g> </svg> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM