Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Bootstrap Glyphicons work?

I know some basics of CSS and HTML and sometimes work with them, but am not a professional, and I'm curious about how Bootstrap Glyphicons work. I mean there are no images in the Bootstrap zip file, so where do the images come from?

like image 210
adamsmith Avatar asked Nov 29 '13 06:11

adamsmith


People also ask

How do I use bootstrap Glyphicons?

Glyphicons. Bootstrap includes 260 glyphs from the Glyphicon Halflings set. Glyphicons Halflings are normally not available for free, but their creator has made them available for Bootstrap free of cost. As a thank you, you should include a link back to Glyphicons whenever possible.

What is the main purpose of Glyphicons?

The Glyphicons are a set of symbols and icons to understand more effectively and easily in web projects. The Glyphicons are used for some texts, forms, buttons, navigation, and etc. The bootstrap has many Glphyicons but there has a standard format to display icons.

How do I use Glyphicons in bootstrap 5?

In lines 15 and 17 the <span> and <a> element has the bootstrap glyphicon glyphicon-pencil classes, so to use any glyphicon in bootstrap: Use the <span> or <a> element. Set the class attribute of the <span> or <a> element as glyphicon then a space and then glyphicon-iconname.


1 Answers

In Bootstrap 2.x, Glyphicons used the image method - using images as you mentioned in your question.

The drawbacks with the image method was that:

  • You couldn't change the color of the icons
  • You couldn't change the background-color of the icons
  • You couldn't increase the size of the icons

So instead of glyphicons in Bootstrap 2.x, a lot of people used Font-awesome, as this used SVG icons which did not have such limitations.

In Bootstrap 3.x, Glyphicons use the font method.

Using a font to represent an icon has advantages over using images:

  • Scalable - works nicely regardless of client device's resolution
  • Can change the colour with CSS
  • Can do everything traditional icons can (e.g. change opacity, rotation, etc.)
  • Can add strokes, gradients, shadows, and etc.
  • Convert to text (with ligatures)
  • Ligatures are read by screen readers
  • Changing icons to fonts is as simple as changing the font-family in CSS

You can see what you can do with the font method for icons here.

So in the Bootstrap distribution, you can see the glyphicon font files:

fonts\glyphicons-halflings-regular.eot fonts\glyphicons-halflings-regular.svg fonts\glyphicons-halflings-regular.ttf fonts\glyphicons-halflings-regular.woff 

The Bootstrap CSS refers to the glyphicon font like so:

@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'); } 

And the CSS links to this font using the .glyphicon base class (note the font-family):

.glyphicon {   position: relative;   top: 1px;   display: inline-block;   font-family: 'Glyphicons Halflings';   font-style: normal;   font-weight: normal;   ... } 

And then each glyphicon uses an individual icon class which refers to a Unicode reference within the Glyphicon Halflings font, e.g.:

.glyphicon-envelope:before {   content: "\2709"; } 

This is why you must use the base .glyphicon class as well as the individual icon class against the span element in Bootstrap 3:

<span class="glyphicon glyphicon-envelope"></span> 
like image 137
mccannf Avatar answered Nov 12 '22 20:11

mccannf