Icons are an essential part of modern web design, making user interfaces more intuitive and visually appealing. Bootstrap 3 includes a powerful built-in icon library called Glyphicons, which provides over 200 scalable icons that are easy to use in your projects.
In this guide, we’ll cover everything you need to know about using Bootstrap Glyphicons, from basic usage to advanced customization. Let’s dive in!
1. What are Bootstrap Glyphicons?
Glyphicons in Bootstrap are a set of font-based icons that can be styled and scaled without losing quality. They are lightweight, responsive, and integrate seamlessly with Bootstrap components like buttons, navigation bars, and forms.
Advantages of Glyphicons:
- Lightweight and easy to use.
- Scalable for responsive design.
- No need for additional image files.
2. How to Use Glyphicons
To use Glyphicons, simply add the glyphicon
class along with the specific icon’s class to an HTML element, such as <span>
or <i>
.
Basic Example:
<span class="glyphicon glyphicon-search"></span>
Output: A magnifying glass icon appears.
3. Glyphicons: Commonly Used Classes
Here’s a list of some popular Glyphicon classes:
Icon Name | Class |
---|---|
Search Icon | glyphicon-search |
User Icon | glyphicon-user |
Home Icon | glyphicon-home |
Envelope (Mail) Icon | glyphicon-envelope |
Trash Icon | glyphicon-trash |
Plus Icon | glyphicon-plus |
Minus Icon | glyphicon-minus |
Lock Icon | glyphicon-lock |
For the full list of available icons, check the Bootstrap Glyphicons Reference.
4. Using Glyphicons in Buttons
You can easily pair Glyphicons with buttons to create interactive and visually appealing UI elements.
Example:
<button type="button" class="btn btn-primary">
<span class="glyphicon glyphicon-thumbs-up"></span> Like
</button>
Output: A button with a thumbs-up icon and text.
5. Glyphicons in Navigation
Add icons to navigation items to make them more intuitive.
Example:
<ul class="nav nav-pills">
<li class="active"><a href="#"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Profile</a></li>
<li><a href="#"><span class="glyphicon glyphicon-envelope"></span> Messages</a></li>
</ul>
Output: A navigation bar with icons next to each link.
6. Glyphicons in Forms
Icons can enhance form inputs by providing visual cues for users.
Example:
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input type="email" class="form-control" placeholder="Email Address">
</div>
Output: A form input field with an envelope icon on the left.
7. Glyphicons in Alerts
Use Glyphicons to add icons to Bootstrap alerts for better emphasis.
Example:
<div class="alert alert-success">
<span class="glyphicon glyphicon-ok"></span> Success! Your operation completed successfully.
</div>
Output: A success alert with a checkmark icon.
8. Customizing Glyphicons
Since Glyphicons are font-based, you can customize them using CSS.
Example:
.custom-icon {
font-size: 24px;
color: #007bff;
}
HTML:
<span class="glyphicon glyphicon-heart custom-icon"></span>
Output: A larger, blue heart icon.
9. Best Practices for Using Glyphicons
- Accessibility: Add
aria-hidden="true"
to icons for better screen reader compatibility. - Minimal Usage: Avoid overloading your design with too many icons.
- Fallback Options: Use text alternatives for icons in case they don’t load.
10. Alternatives to Glyphicons
While Bootstrap 3 Glyphicons are a great tool, consider alternatives for projects requiring more flexibility or modern design elements:
- Font Awesome: A more extensive and customizable icon library.
- SVG Icons: For high-resolution and responsive needs.
Conclusion
Bootstrap Glyphicons are a simple yet powerful tool for enhancing the usability and aesthetics of your website. Whether you’re designing buttons, forms, or navigation bars, Glyphicons provide a quick and effective way to include icons in your projects.