Can I add color to bootstrap icons only using CSS?
Asked Answered
L

14

161

Twitter's bootstrap uses Icons by Glyphicons. They are "available in dark gray and white" by default:

Picture-58.png

Is it possible to use some CSS trickery to change the colors of the icons? I was hoping for some other css3 brilliance that would prevent having to have an icon image set for each color.

I know you can change the background color of the enclosing (<i>) element, but I'm talking about the icon foreground color. I guess it would be possible to inverse the transparency on the icon image and then set the background color.

So, can I add color to bootstrap icons only using CSS?

Loree answered 11/9, 2012 at 23:8 Comment(3)
I don't think so... But you could probably do that with javascript and a canvas element.Humoral
@Humoral - can you elaborate? do you mean coming up with code / coordinates to draw each of the icons or using the existing png image somehow?Loree
Sorry, I'm not experienced enough with the canvas element to suggest you code, but I know you can manipulate the pixels individually with a canvas.Humoral
H
190

Yes, if you use Font Awesome with Bootstrap! The icons are slightly different, but there are more of them, they look great at any size, and you can change the colors of them.

Basically the icons are fonts and you can change the color of them just with the CSS color property. Integration instructions are at the bottom of the page in the provided link.


Edit: Bootstrap 3.0.0 icons are now fonts!

As some other people have also mentioned with the release of Bootstrap 3.0.0, the default icon glyphs are now fonts like Font Awesome, and the color can be changed simply by changing the color CSS property. Changing the size can be done via font-size property.

Harmaning answered 11/9, 2012 at 23:33 Comment(3)
Ahh, clever! Don't forget to set cursor: default; so users don't see the I-Bar. Also, check out FF Chartwell, a really clever font: tktype.com/chartwell.phpPlication
When you mouse-over text (like in a word processor) your cursor looks like an oversized capital "I" character, it's used for selecting text. In CSS it's referred to as cursor: text;. It's also known as "I-beam".Plication
@Harmaning the Font Awesome link is broken.Hutchings
C
40

With the latest release of Bootstrap RC 3, changing the color of the icons is as simple as adding a class with the color you want and adding it to the span.

Default black color:

<h1>Password Changed <span class="glyphicon glyphicon-ok"></span></h1>

would become

<h1>Password Changed <span class="glyphicon glyphicon-ok icon-success"></span></h1>

CSS

.icon-success {
    color: #5CB85C;
}

Bootstrap 3 Glyphicons List

Corin answered 29/8, 2013 at 20:36 Comment(0)
R
18

Because the glyphicons are now fonts, one can use the contextual classes to apply the appropriate color to the icons.

For example: <span class="glyphicon glyphicon-info-sign text-info"></span> adding text-info to the css will make the icon the info blue color.

Rosalia answered 22/5, 2014 at 20:25 Comment(1)
Thanks was looking for a way to apply the contextual classes. Any of the text-* contextual classes will workStatis
D
14

Another possible way to have bootstrap icons in a different color is to create a new .png in the desired color, (eg. magenta) and save it as /path-to-bootstrap-css/img/glyphicons-halflings-magenta.png

In your variables.less find

// Sprite icons path
// -------------------------
@iconSpritePath:          "../img/glyphicons-halflings.png";
@iconWhiteSpritePath:     "../img/glyphicons-halflings-white.png";

and add this line

@iconMagentaSpritePath:     "../img/glyphicons-halflings-magenta.png";

In your sprites.less add

/* Magenta icons with optional class, or on hover/active states of certain elements */
.icon-magenta,
.nav-pills > .active > a > [class^="icon-"],
.nav-pills > .active > a > [class*=" icon-"],
.nav-list > .active > a > [class^="icon-"],
.nav-list > .active > a > [class*=" icon-"],
.navbar-inverse .nav > .active > a > [class^="icon-"],
.navbar-inverse .nav > .active > a > [class*=" icon-"],
.dropdown-menu > li > a:hover > [class^="icon-"],
.dropdown-menu > li > a:hover > [class*=" icon-"],
.dropdown-menu > .active > a > [class^="icon-"],
.dropdown-menu > .active > a > [class*=" icon-"],
.dropdown-submenu:hover > a > [class^="icon-"],
.dropdown-submenu:hover > a > [class*=" icon-"] {
  background-image: url("@{iconMagentaSpritePath}");
}

And use it like this:

<i class='icon-chevron-down icon-magenta'></i>

Hope it helps someone.

Dr answered 12/11, 2012 at 14:25 Comment(3)
just wanted to reiterate I was hoping for some other css3 brilliance that would prevent having to have an icon image set for each colorLoree
Can't be done purely via CSS that is cross browser compatible too. Usually people need a color scheme with related icons - so I would say upto 5 color sets isn't that much of a challenge.Snowshed
@seppludger How do you create a new color halfling sprite? Have you succeeded in doing this? I cannot change colors if I load them in any graphics editor.Retarded
D
7

Quick and dirty work around which did it for me, not actually coloring the icon, but surrounding it with a label or badge in the color you want;

<span class="label-important label"><i class="icon-remove icon-white"></i></span>
<span class="label-success label"><i class="icon-ok icon-white"></i></span>
Delitescent answered 17/4, 2013 at 12:19 Comment(2)
Your answer was useful,+1.I wanted to use edit icon and want to put yellow color. Can you please tell me how to do that?Illuminism
Well you could just change the icon-ok to something else; e.g. <span class="label-warning label"><i class="icon-<iconname> icon-white"></i></span>. For example: <span class="label-warning label"><i class="icon-edit icon-white"></i></span>. It is better to go for the new bootstrap imo..Delitescent
T
6

The Bootstrap Glyphicons are fonts. This means it can be changed like any other text through CSS styling.

CSS:

<style>
   .glyphicon-plus {
       color: #F00; 
   }
</style>

HTML:

<span class="glyphicon glyphicon-plus"></span>

Example:

<!doctype html>
<html>
<head>
<title>Glyphicon Colors</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>
   .glyphicon-plus {
        color: #F00;    
   }
</style>
</head>

<body>
    <span class="glyphicon glyphicon-plus"></span>
</body>
</html>


Watch the course Up and Running with Bootstrap 3 by Jen Kramer, or watch the individual lesson on Overriding core CSS with custom styles.

Thiouracil answered 4/2, 2014 at 13:39 Comment(0)
P
4

It is actually very easy:

just use:

.icon-name{
color: #0C0;}

For example:

.icon-compass{
color: #C30;}

That's it.

Prochronism answered 28/8, 2013 at 12:33 Comment(0)
U
4

This is all a bit roundabout..

I've used the glyphs like this

  </div>
        <div class="span2">
            <span class="glyphicons thumbs_up"><i class="green"></i></span>
        </div>
        <div class="span2">
            <span class="glyphicons thumbs_down"><i class="red"></i></span>
        </div>

and to affect the color, i included a bit of css at the head like this

<style>
        i.green:before {
            color: green;
        }
        i.red:before {
            color: red;
        }
    </style>

Voila, green and red thumbs.

Uniformity answered 28/8, 2013 at 20:17 Comment(0)
L
4

Also works with the style tag:

<span class="glyphicon glyphicon-ok" style="color:#00FF00;"></span>

<span class="glyphicon glyphicon-remove" style="color:#FF0000;"></span>

enter image description here

Landeros answered 19/1, 2015 at 11:28 Comment(0)
P
3

Use the mask-image property, but it's a bit of a hack. It's demonstrated in the Safari blog here.

It's also described in-depth here.

Basically you'd create a CSS box, with say a background-image: gradient(foo); and then apply an image mask to it based on those PNG images.

It would save you development time making individual images in Photoshop, but I don't think it would save much bandwidth unless you'll be displaying the images in a wide variety of colours. Personally I wouldn't use it because you need to adjust your markup to use the technique effectively, but I'm a member of the "purity is imperitive" school-of-thought.

Plication answered 11/9, 2012 at 23:14 Comment(1)
Keep in mind support for the mask-image property isn't that great.Chondro
L
1

Just use the GLYPHICONS and you will be able to change color just setting the CSS:

#myglyphcustom {
    color:#F00;
}
Lory answered 12/8, 2013 at 13:28 Comment(0)
K
1

The accepted answer (using font awesome) is the right one. But since I just wanted the red variant to show on validation errors, I ended using an addon package, kindly offered on this site.

Just edited the url paths in css files since they are absolute (start with /) and I prefer to be relative. Like this:

.icon-red {background-image: url("../img/glyphicons-halflings-red.png") !important;}
.icon-purple {background-image: url("../img/glyphicons-halflings-purple.png") !important;}
.icon-blue {background-image: url("../img/glyphicons-halflings-blue.png") !important;}
.icon-lightblue {background-image: url("../img/glyphicons-halflings-lightblue.png") !important;}
.icon-green {background-image: url("../img/glyphicons-halflings-green.png") !important;}
.icon-yellow {background-image: url("../img/glyphicons-halflings-yellow.png") !important;}
.icon-orange {background-image: url("../img/glyphicons-halflings-orange.png") !important;}
Keyte answered 2/5, 2014 at 16:17 Comment(1)
Finally, the one compatible with the old versions of BootstrapExchangeable
D
1

You can change color globally as well

i.e.

 .glyphicon {
       color: #008cba; 
   }
Dibru answered 23/7, 2014 at 17:46 Comment(0)
O
0

I thought that I might add this snippet to this old post. This is what I had done in the past, before the icons were fonts:

<i class="social-icon linkedin small" style="border-radius:7.5px;height:15px;width:15px;background-color:white;></i>
<i class="social-icon facebook small" style="border-radius:7.5px;height:15px;width:15px;background-color:white;></i>

This is very similar to @frbl 's sneaky answer, yet it does not use another image. Instead, this sets the background-color of the <i> element to white and uses the CSS property border-radius to make the entire <i> element "rounded." If you noticed, the value of the border-radius (7.5px) is exactly half that of the width and height property (both 15px, making the icon square), making the <i> element circular.

Oram answered 10/11, 2014 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.