How to add icons in WeChat Mini Programs
Table of Contents 目录
Background:
I’ve been working on my big project recently! I’m using WeChat Cloud Development. Since I’d never built a mini program before, I thought I’d start by practicing with an existing project. While following a video tutorial, I noticed that all the examples used images. I wondered if I could use iconfont instead — it takes up less memory.
Implementation:
I tried several methods, but none of them worked the way I wanted — or flat out didn’t work. I finally got it working after finding one article.
-
Go to the
iconfontwebsite: https://www.iconfont.cn/. Add the icons you want to your cart, then add the cart icons to a project. -
Open the target project where the
iconfontis stored, download its code to your local machine — you’ll get a zip file. -
Extract the zip file and rename it. I named the folder
iconfont. -
Open
cmdand navigate to theiconfontdirectory. Then we need to convert this iconfont into code that WeChat mini program can recognize — i.e., generate aniconfont.wxssfile we want. Do this in the terminal:
npm install -g iconfont-toolsthen pressEnter
iconfont-toolsthen pressEnter
A prompt will appear asking for input, just like when initializing a Vue/React project. -
Open the
iconfont-wxfolder, and you’ll see the generated target fileiconfont-wxIcon.wxss. -
Now we can paste this target file into the corresponding location in our mini program project.
-
In the mini program project’s
app.wxssfile, import ouriconfont. -
Use it:
Note:
You can open the
iconfot-wxIcon.wxssfile to take a look. You’ll notice thatt-iconhere is equivalent toiconfont. And when using a specific icon, we writet-icon-bangzhu— i.e., adding at-prefix to the official name.