4 Easy Ways to Add Icons In HTML CSS (Simple Examples)

Welcome to a beginner’s tutorial on how to add icons in HTML and CSS. Need to add some icons to your website? Make the contents a little easier to navigate?

There are various ways to add icons in HTML and CSS:

  1. The easiest way is to use HTML symbols, simply copy-and-paste the respective HTML entity code. For example, ★ represents a star symbol.
  2. Download icons images from websites such as FlatIcon, and use them as-it-is. E.G. <img src="ICON.PNG"/>
  3. Use a set of font icons, such as Webdings. E.G. <p style="font-family:Webdings">ABC</p>
  4. Lastly, load and use icon libraries such as Font Awesome and Material Icons.

But just how are these done exactly? Let us walk through some examples in this guide – Read on to find out!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Adding Icons Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

ADDING ICONS

All right, let us now get into the various methods on how to add icons in HTML and CSS.

 

METHOD 1) HTML SYMBOLS

1-html-symbols.html
<div>&#9733; STAR!</div>
<div>&#9730; Weather report.</div>
<div>&#9731; Do you want to build a snowman?</div>
<div>&#9787; Happy face.</div>
★ STAR!
☂ Weather report.
☃ Do you want to build a snowman?
☻ Happy face.

What kind of sorcery is this? Well, these are HTML symbols, and there are a ton of these – Everything from arrows, to symbols, to currencies, to smilies, and more. The best part is that they are baked straight into native HTML, and we don’t need to load 3rd party frameworks for these to work.

The usage is very simple, just define &#NUMBER; and it will “translate” into the respective HTML symbol. I will leave a link to the complete reference in the extras section below. Also, these symbols are treated just like text, we can control the size using font-size, even apply font-weight and text-decoration on it.

 

 

METHOD 2) ICON IMAGES

2A) FLAT IMAGE ICON

2a-icon-image.html
<img src="test-icon.png"/> So call me maybe?
So call me maybe?

This should be “Captain Obvious”, just insert an image icon. Do a search for “free icons” or “free clipart”, they are all over the Internet.

 

2B) IMAGE FONT SET

Following up on the above image icon method, it is inefficient to use one icon per image. This is the smarter way where we combine all the icons into one single image, but it requires a little bit of CSS to work.

2b-icon-set.html
<style>
.ico {
  display: inline-block;
  width: 32px; height: 30px;
  background-image: url('icon-set.png');
  background-repeat: no-repeat;
}
.ico-screen { background-position: 0 0; }
.ico-phone { background-position: -32px 0; }
.ico-pc { background-position: -64px 0; }
.ico-tablet { background-position: 0 -31px; }
.ico-camera { background-position: -32px -31px; }
</style>

<i class="ico ico-screen"></i> Screen
<i class="ico ico-phone"></i> Phone
<i class="ico ico-pc"></i> PC
<i class="ico ico-tablet"></i> Tablet
<i class="ico ico-camera"></i> Camera

That’s right. All we are doing here is to use the icon set as the background image, then “map” them using background-position.

 

 

METHOD 3) ICON FONT SET

3-icon-font.html
<style>
@font-face {
  font-family: Heydings;
  src: url(heydings_icons.ttf);
}
.icon {
  font-size: 3em;
  font-family: Heydings;
}
</style>
<p class="icon">ABCDEF</p>

Fonts usually contain alphabets, but there are some interesting ones that are sets of icons instead – All we have to do is to find these sets of icons, include them using CSS @font-face, then use it just as usual.

I will leave links to free font websites in the extras section below, just do a font search for “icons”, and there will be plenty more. But please do take care that not all fonts are “fully free”… Some are free for personal use, but not for commercial use.

 

METHOD 4) ICON LIBRARIES

4A) FONT AWESOME

4a-font-awesome.html
<!-- (A) LOAD FROM CDN -->
<!-- https://cdnjs.com/libraries/font-awesome -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/LATEST-VERSION">
 
<!-- (B) DEFINE ICONS -->
<!-- https://fontawesome.com/icons?d=gallery -->
<i class="fas fa-bath"></i> +
<i class="fas fa-bed"></i> =
<i class="fas fa-battery-full"></i>

Font Awesome is a free library of icons, the usage is pretty straightforward.

  • Load the Font Awesome library from the CDN. There are a lot of versions… If unsure, just load all.min.css which contains everything.
  • To insert an icon, use the <i> tag, and give it a CSS class="fa fa-ICON" – Check out the Font Awesome Gallery for the full list of available icons.

Of course, if you somehow need even more icons, there are also paid plans on Font Awesome.

 

 

4B) MATERIAL ICONS

4b-mat-icons.html
<!-- (A) LOAD MATERIAL ICONS -->
<!-- https://google.github.io/material-design-icons/ -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
 
<!-- (B) USE ICONS -->
<!-- https://fonts.google.com/icons -->
<span class="material-icons">icecream</span>
<span class="material-icons">restaurant</span>
<span class="material-icons">emoji_food_beverage</span>
<span class="material-icons">local_pizza</span>

Material Icons is another set of popular free icons, which you may find very familiar… Because it’s by Google. Check out their page on Google Fonts for the full list of icons.

 

USEFUL BITS & LINKS

That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.

 

WHICH IS THE BEST METHOD?

  • I personally prefer HTML symbols when it comes to small projects, as it is the most lightweight option. But take note that some symbols will not show up properly on the older browsers, you will not want to use this if backward compatibility is an issue.
  • Images are the safest and will always work, but they add to the loading time bloat.
  • Font Awesome and Google Material Icons are also pretty safe options but they bloat the loading time as well.
  • Loading fonts are kind of meh.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Add Icons In HTML CSS (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *