简体   繁体   English

如何在CSS中使用图标作为“背景图像”而不是图像

[英]How to use an icon as 'background-image' instead of an image in CSS

I want to use an icon as a background image instead of using any image. 我想使用图标作为背景图像,而不是使用任何图像。 Is there any way to do this? 有什么办法吗?

Normally we write 'background-image' as: 通常我们将“背景图像”写为:

background-image: url(...)

But I want to use a fontawesome icon as background. 但是我想使用fontawesome图标作为背景。 Is there any way to write the Unicode of fontawesome icon inside the 有什么办法可以在字体内写入fontawesome图标的Unicode?

url()

Please help me, CSS experts. CSS专家,请帮助我。

One way to do it is to download the SVG of the icon, and just use it how you use other images, you can find the SVG file of every icon in font awesome website. 一种实现方法是下载图标的SVG,然后像使用其他图像一样使用它,就可以在真棒字体网站中找到每个图标的SVG文件。

The good thing about SVGs is that they are vector, so there is no quality loss no matter how much you zoom in. SVG的优点是它们是矢量,因此无论您放大多少,都不会造成质量损失。

You can get any SVG or PNG icon file and use it for background. 您可以获取任何SVG或PNG图标文件,并将其用作背景。

For example 例如

https://codepen.io/hisbvdis/pen/XWrBKNp https://codepen.io/hisbvdis/pen/XWrBKNp

 body { background: url("https://image.flaticon.com/icons/svg/60/60993.svg") 0 0 / 20px 30px repeat; } 

在此处输入图片说明

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

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