Bootstrap Glyphicons
Goals
Icon fonts are becoming increasingly popular due to their benefits. In this tutorial we will discuss how to use and customize Glyphicons with Bootstrap 3. We will explain the CSS rules working behind the scenes, which will give you a better understanding of how icon fonts work. This way, you will become familiar with setting up any icon font besides Glyphicons.
What are Glyphicons
Glyphicons are icon fonts used in web projects. Although using Glyphicons requires a commercial license, you can use these icons for free through Bootstrap in your projects. To show appreciation to the icon authors, please add the Glyphicon website link.
Getting Glyphicons
If you have downloaded Bootstrap 3 (or any pre-3 version) from https://github.com/twbs/bootstrap/releases/download/v3.0.2/bootstrap-3.0.2-dist.zip, you can find the glyphicons in the fonts folder inside the dist folder. There are four files - glyphicons-halflings-regular.eot, glyphicons-halflings-regular.svg, glyphicons-halflings-regular.ttf, and glyphicons-halflings-regular.woff. The relevant CSS rules are written in the bootstrap.css and bootstrap-min.css files in the css folder inside the dist folder.
How to Use Glyphicons
In general, we can derive the common syntax for using Glyphicons with Bootstrap 3 as follows
- can be any keyword representing a specific icon. The following example demonstrates how to use it in a button.
Example
body {
padding-top: 50px;
padding-left: 50px;
}
View the example online
CSS Rule Explanation
The following CSS rules make up the glyphicon class
@font-face {
font-family: 'Glyphicons Halflings';
src: url('../fonts/glyphicons-halflings-regular.eot');
src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}
.glyphicon {
position: relative;
top: 1px;
display: inline-block;
font-family: 'Glyphicons Halflings';
-webkit-font-smoothing: antialiased;
font-style: normal;
font-weight: normal;
line-height: 1;
-moz-osx-font-smoothing: grayscale;
}
So the font-face rule actually declares the font-family and location where the glyphicons are found.
.glyphicon The class declares a relative position offset 1px from the top, renders as inline-block, declares the font, sets font-style and font-weight to normal, and sets line height to 1. In addition, it uses -webkit-font-smoothing: antialiased and -moz-osx-font-smoothing: grayscale; to achieve cross-browser consistency.
Then, the
.glyphicon:empty {
width: 1em;
}
here is an empty glyphicon.
There are 200 classes, each for one icon. The common format of these classes is as follows
.glyphicon-keyword:before {
content: "hexvalue";
}
For example, the user icon used in our example has the following class
.glyphicon-user:before {
content: "\e008";
}
We have seen how to use it; next let's see how to customize Glyphicons.
We will start with the example above and customize the icons by changing the font size, color, and applying text shadows.
Here is the starting code
The effect is shown below
Customizing Font Size
By increasing or decreasing the icon's font size, you can make the icon look larger or smaller.
Customizing Font Color
Applying Text Shadows