How to add badge on top of Font Awesome symbol? - css

I would like to add badge with some number (5, 10, 100) on top of the Font Awesome symbol (fa-envelope). For example:
But, I can not understand how to put the badge on top of the symbol. My attempt is available here: jsFiddle.
I would like to have it support Twitter Bootstrap 2.3.2.

This can be done with no additional mark-up, just a new class (which you would use anyway) and a pseudo element.
JSFiddle Demo
HTML
<i class="fa fa-envelope fa-5x fa-border icon-grey badge"></i>
CSS
*.icon-blue {color: #0088cc}
*.icon-grey {color: grey}
i {
width:100px;
text-align:center;
vertical-align:middle;
position: relative;
}
.badge:after{
content:"100";
position: absolute;
background: rgba(0,0,255,1);
height:2rem;
top:1rem;
right:1.5rem;
width:2rem;
text-align: center;
line-height: 2rem;;
font-size: 1rem;
border-radius: 50%;
color:white;
border:1px solid blue;
}

While #Paulie_D's answer is good, it doesn't work so well when you have a variable width container.
This solution works a lot better for that: http://codepen.io/johnstuif/pen/pvLgYp
HTML:
<span class="fa-stack fa-5x has-badge" data-count="8,888,888">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-bell fa-stack-1x fa-inverse"></i>
</span>
CSS:
.fa-stack[data-count]:after{
position:absolute;
right:0%;
top:1%;
content: attr(data-count);
font-size:30%;
padding:.6em;
border-radius:999px;
line-height:.75em;
color: white;
background:rgba(255,0,0,.85);
text-align:center;
min-width:2em;
font-weight:bold;
}

Wrap the fa-envelope and the span containing the number in a div and make the wrapper div position:relative and the span position:absolute.
Check this fiddle
HTML used
<div class="icon-wrapper">
<i class="fa fa-envelope fa-5x fa-border icon-grey"></i>
<span class="badge">100</span>
</div>
CSS
.icon-wrapper{
position:relative;
float:left;
}
*.icon-blue {color: #0088cc}
*.icon-grey {color: grey}
i {
width:100px;
text-align:center;
vertical-align:middle;
}
.badge{
background: rgba(0,0,0,0.5);
width: auto;
height: auto;
margin: 0;
border-radius: 50%;
position:absolute;
top:-13px;
right:-8px;
padding:5px;
}
Hope this might help you

This seems to work, and it has a very minimal code to be added.
.badge{
position: relative;
margin-left: 60%;
margin-top: -60%;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<span class="fa-stack fa-3x">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-bell fa-stack-1x fa-inverse"></i>
<span class="badge">17</span>
</span>
<span class="fa-stack fa-2x">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-bell fa-stack-1x fa-inverse"></i>
<span class="badge">17</span>
</span>
<span class="fa-stack fa-1x">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-bell fa-stack-1x fa-inverse"></i>
<span class="badge">17</span>
</span>

http://jsfiddle.net/zxVhL/16/
By placing the badge inside the icon element I was able to position it relative to the bounds of the icon container. I did all the sizing using ems so that the size of the badge is relative to the size of the icon and this can be changed by simply changing the font-size in .badge

If you're using the SVG + JS version of Font Awesome:
https://fontawesome.com/how-to-use/on-the-web/styling/layering
Layers are the new way to place icons and text visually on top of each other.
<span class="fa-layers fa-fw">
<i class="fas fa-envelope"></i>
<span class="fa-layers-counter" style="background:Tomato">1,419</span>
</span>

From Font Awesome documentation:
<span class="fa-layers fa-fw" style="background:MistyRose">
<i class="fas fa-envelope"></i>
<span class="fa-layers-counter" style="background:Tomato">1,419</span>
</span>

Related

how can I paint the internal color (not font color) of a fontawesome icon to avoid transparency? [duplicate]

How to change the inner (white) color of the icon
<i class="fa fa-exclamation-triangle"></i>?
P.S. Applying <i class="fa fa-exclamation-triangle" style="color:red"></i> is not an answer, as the red color applies to the outer color, not to the inner exclamation white sign itself.
The Font Awesome version is: 4.7.0.
The exclamation mark of this icon is a transparent part, so a trick is to add a background behind it to have the needed coloration. Of couse, the background shouldn't cover the whole area so we need to use a gradient to cover only a part of it.
.fa-exclamation-triangle {
background:linear-gradient(red,red) center bottom/20% 84% no-repeat;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<i class="fas fa-exclamation-triangle fa-7x"></i>
<i class="fas fa-exclamation-triangle fa-4x"></i>
<i class="fas fa-exclamation-triangle fa-2x"></i>
The same thing with the V4:
.fa-exclamation-triangle {
background:linear-gradient(red,red) center /20% 70% no-repeat;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<i class="fa fa-exclamation-triangle fa-5x"></i>
<i class="fa fa-exclamation-triangle fa-4x"></i>
<i class="fa fa-exclamation-triangle fa-2x"></i>
Also the SVG version:
.fa-exclamation-triangle {
background:linear-gradient(red,red) center bottom/20% 84% no-repeat;
}
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js" ></script>
<i class="fas fa-exclamation-triangle fa-7x"></i>
<i class="fas fa-exclamation-triangle fa-4x"></i>
<i class="fas fa-exclamation-triangle fa-2x"></i>
UPDATE
To make the answer more generic we can also consider multiple background and radial-gradient in order to color any kind of shape. The trick is to cover with the background the transparent part without overflowing.
Here is some example of icons:
.fa-exclamation-triangle {
background:linear-gradient(red,red) center bottom/20% 84% no-repeat;
}
.fa-ambulance {
background:
linear-gradient(blue,blue) 25% 30%/32% 45% no-repeat,
radial-gradient(green 60%,transparent 60%) 15% 100%/30% 30% no-repeat,
radial-gradient(green 60%,transparent 60%) 85% 100%/30% 30% no-repeat;
}
.fa-check-circle {
background:radial-gradient(yellow 60%,transparent 60%);
}
.fa-angry {
background:
radial-gradient(red 60%,transparent 60%) 25% 40%/30% 30% no-repeat,
radial-gradient(red 60%,transparent 60%) 75% 40%/30% 30% no-repeat;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<i class="fas fa-exclamation-triangle fa-7x"></i>
<i class="fas fa-ambulance fa-7x"></i>
<i class="fas fa-check-circle fa-7x"></i>
<i class="fas fa-angry fa-7x"></i>
The answer of Temani Afif is a good one! Here's another approach;
.fa-exclamation-triangle{
position: relative;
z-index:0;
}
.fa-exclamation-triangle:after{
content: ' ';
width: 20%;
height: 70%;
position: absolute;
top: 15%;
left: 40%;
background: red;
z-index: -1;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<i class="fa fa-exclamation-triangle fa-5x"></i>
<i class="fa fa-exclamation-triangle fa-4x"></i>
<i class="fa fa-exclamation-triangle fa-2x"></i>

Font Awesome: Stacking icons and getting pixel perfect sizing

I am trying to use the stacking technique built into Font Awesome to display small icons inside of a circle. I'd like however to have total control over the size of the circle - more so then the sizing options they have available.
I've built a proof of concept, which works, but the circle doesn't display at the desired dimensions. I have specified for it to be 60x60, however if you inspect the element the container is the correct size but the actual circle icon is inset a bit.
body {
font-size: 12px;
font-family: Courier;
}
.fa-stack.custom {
font-size: 60px;
width: 60px;
height: 60px;
line-height: 60px;
overflow: hidden;
background-color: pink;
}
.fa-stack.custom .circle {
font-size: 60px;
}
.fa-stack.custom .icon {
font-size: 18px;
color: #ff00ff;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<span class="fa-stack custom">
<i class="circle fa fa-circle fa-stack-2x"></i>
<i class="icon fa fa-times fa-stack-1x fa-inverse"></i>
</span>
https://jsfiddle.net/louiswalch/nxo2s1gt/13/
Any ideas for getting this to work for exact pixel dimensions?
You will have better accuracy if you consder the V5 and the SVG version. The height is defined as 2em so using 30px as font-size will give you the needed result.
.fa-stack.custom {
font-size: 30px;
background-color: pink;
}
.fa-stack.custom .icon {
font-size: 18px;
color: #ff00ff;
}
<script src="https://use.fontawesome.com/releases/v5.12.0/js/all.js"></script>
<span class="fa-stack custom">
<i class="circle fa fa-circle fa-stack-2x"></i>
<i class="icon fa fa-times fa-stack-1x fa-inverse"></i>
</span>
A little bigger if you want the circle to be exactly 60px since the path doesn't cover the whole viewbox area:
.fa-stack.custom {
font-size: 30.97px;
background-color: pink;
}
.fa-stack.custom .icon {
font-size: 18px;
color: #ff00ff;
}
<script src="https://use.fontawesome.com/releases/v5.12.0/js/all.js"></script>
<span class="fa-stack custom">
<i class="circle fa fa-circle fa-stack-2x"></i>
<i class="icon fa fa-times fa-stack-1x fa-inverse"></i>
</span>
<p>Below the SVG of the circle to notice the small gaps</p>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"style="border:1px solid;"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>
You can use CSS variables to make easier to handle. The value 98.66 comes from the fact that if the SVG is 100px height the path inside will be 98.66px height:
.fa-stack.custom {
font-size: calc(var(--s)*(50/96.88));
background-color: pink;
}
.fa-stack.custom .icon {
font-size: 18px;
color: #ff00ff;
}
<script src="https://use.fontawesome.com/releases/v5.12.0/js/all.js"></script>
<span class="fa-stack custom" style="--s:60px">
<i class="circle fa fa-circle fa-stack-2x"></i>
<i class="icon fa fa-times fa-stack-1x fa-inverse"></i>
</span>
<span class="fa-stack custom" style="--s:70px">
<i class="circle fa fa-circle fa-stack-2x"></i>
<i class="icon fa fa-times fa-stack-1x fa-inverse"></i>
</span>
<span class="fa-stack custom" style="--s:100px">
<i class="circle fa fa-circle fa-stack-2x"></i>
<i class="icon fa fa-times fa-stack-1x fa-inverse"></i>
</span>

Font awesome - change icon inside color on hover

I'm trying to change the inside color of an icon when hovering it, I've tried this css code:
.fa-star-o:hover {
color: black;
}
<link href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
<i class="fa fa-star-o fa-2x"></i>
With this code, only the outline of the icon is changed, not the inside, I want to change the inside color
Use stacked icons:
.fa-star{
color: white /* your background color */
}
.fa-stack:hover .fa-star{
color: black;
}
<link href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css" rel="stylesheet"/>
<span class="fa-stack fa-lg">
<i class="fa fa-star fa-stack-2x"></i>
<i class="fa fa-star-o fa-stack-2x"></i>
</span>
JSFiddle
You don't need the icon fa-star-o. Use only fa-star:
.fa-star:hover {
color: black;
cursor:pointer
}
.fa-star {
color:white;
transition:color 0.5s
}
body {
background-color:red !important;
}
<link rel="stylesheet" href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css">
<link rel="stylesheet" href="http://fontawesome.io/assets/css/site.css">
<div style="font-size: 24px; line-height: 1.5em;" id="parent">
<div style="font-size: 24px; line-height: 1.5em;">
<i class="fa fa-star" aria-hidden="true"></i>
</div>
</div>

center div with Font Awesome

I have a site I am making with Bootstrap 3.3.5. I would like to center the div with the icons and I also want the icons centered, like what text-align: center; would do if it was just text and not the Fonat Awesome icons. This is what I have tried so far, the css is in my css folder and in the custom.css page.
#media (min-width: 992px) {
.col-md-1-5 { width: 20%; }
.col-md-2-5 { width: 40%; }
.col-md-3-5 { width: 60%; }
.col-md-4-5 { width: 80%; }
.col-md-5-5 { width: 100%; }
}
#media (min-width: 1200px) {
.col-lg-1-5 { width: 20%; }
.col-lg-2-5 { width: 40%; }
.col-lg-3-5 { width: 60%; }
.col-lg-4-5 { width: 80%; }
.col-lg-5-5 { width: 100%; }
}
.show-grid [class^=col-] span,
.container-fluid .show-grid [class^=col-] {
display: block;
padding-top: 1rem;
padding-bottom: 1rem;
text-align: center;
}
[class^=col-] {
margin-bottom: 2rem;
}
<div class="row show-grid">
<div class="col-sm-6 col-md-4-5 col-lg-1-5 text-center"><span class=""><a href="https://play.google.com/store/apps/details?id=com.nationalkitchencabinets.kitchensolutions" target="_blank">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-google fa-stack-1x fa-inverse"></i>
</span>
</a></span>
</div>
<div class="col-sm-6 col-md-5-5 col-lg-1-5 text-center"><span class=""><a href="https://appsto.re/us/IxCMbb.i" target="_blank">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-android fa-stack-1x fa-inverse"></i>
</span>
</a></span>
</div>
<div class="col-sm-6 col-md-3-5 col-lg-1-5 text-center"><span class=""><a href="mailto:info#nationalkitchencabinets.com">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-envelope fa-stack-1x fa-inverse"></i>
</span>
</a></span>
</div>
<div class="col-sm-6 col-md-3-5 col-lg-1-5 text-center"><span class=""><a href="https://www.facebook.com/NationalKitchenCabinets/" target="_blank">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
</span>
</a></span>
</div>
<div class="col-sm-6 col-md-3-5 col-lg-1-5 text-center"><span class=""><a href="tel:1-413-374-2939">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-phone fa-stack-1x fa-inverse"></i>
</span>
</a></span>
</div>
</div>
This is what it looks like now, the icons are left aligned,
The elements that you are trying to align are block elements, so you simply need to use margin: 0 auto rather than text-align: center.
.show-grid [class^=col-] span,
.container-fluid .show-grid [class^=col-] {
text-align: center;
}
Should be:
.show-grid [class^=col-] span,
.container-fluid .show-grid [class^=col-] {
margin: 0 auto;
}
I've created a Bootply showcasing this here.
UPDATE:
A temporary hack to have the icons display next to each other on mobile would be something like the following:
#media (max-width: 500px) {
.col-sm-6 {
width: 20% !important;
float: left;
}
}
However, this is a workaround, and you should really simply set a smaller column width. Bootstrap columns always add up to 12, and col-sm-6 refers to '6 columns on small devices'. Typically, you'd use something like col-sm-2 col-md-12 col-lg-12 to represent that each element should take up the full width on medium and large devices, and only take up one-sixth of the width on small devices. Of course, 12 isn't divisible by 5, so you'd have to come up with your own workaround, such as centralisation or padding ;)
A secondary fiddle showcasing the hack is available here.
Hope this helps!

Font Awesome hover of stacked icons not working properly

I am facing a Problem regarding the hover effect of my icons.
Here is my HTML:
<div class="container">
<div class="row">
<div class="col-md-6 text-center">
<a href="#">
<span class="fa-stack fa-5x">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-wrench fa-stack-1x fa-inverse"></i>
</span>
</a>
</div>
<div class="col-md-6 text-center">
<a href="#">
<span class="fa-stack fa-5x">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-lightbulb-o fa-stack-1x fa-inverse"></i>
</span>
</a>
</div>
</div>
</div>
Here is my style:
.fa-circle {
color: red;
}
.fa-circle:hover {
color: blue;
}
The hover is working until I hover the inner icon (fa-stack-1x). As soon as I hover the fa-stack-1x the fa-stack-2x loses the hover style.
How can I prevent the fa-stack-2x to lose its hover style with only CSS?
You need to capture the hover event on the container span ".fa-stack" like that:
.fa-circle {
color: red;
}
.fa-stack:hover .fa-circle {
color: blue;
}
here is a working fiddle : https://jsfiddle.net/fou3om77/
You will need to target the parent element for the :hover selector, since there is no way of targeting previous elements:
.fa-stack .fa-circle {
color: red;
}
.fa-stack:hover .fa-circle {
color: blue;
}
That's because your hover is only defined on the .fa-circle element and not either of the .fa-stack-1x elements. If you wish to target all the icons you'll need to place the hover effect on the fa-stack parent:
.fa-stack .fa {
color: red; /* All icons within the fa-stack are red by default. */
}
.fa-stack:hover .fa {
color: blue; /* All icons within the fa-stack are blue when hovered over. */
}

Resources