Is there any way to embed HTML in the css content:
element when using a :before
pseudo-element?
I want to use a Font Awesome (or Glyphicon) in a use case like this:
h1:before {
content: "X";
padding-right: 10px;
padding-left: 10px;
color: @orangeLight;
}
Where X
is something like <i class="icon-cut"></i>
.
I can, of course do this manually in HTML, but I really want to use :before
in this case.
Similarly, is there any way to use <i>
as a list bullet? This works, but doesn't behave correctly for multi-line bullet items:
<ul class="icons">
<li><i class="icon-ok"></i> Lists</li>
<li><i class="icon-ok"></i> Buttons</li>
<li><i class="icon-ok"></i> Button groups</li>
<li><i class="icon-ok"></i> Navigation</li>
<li><i class="icon-ok"></i> Prepended form inputs</li>
</ul>
This question is related to
css
twitter-bootstrap
font-awesome
The accepted answer (as of 2019 JULY 29) is only still valid if you have not started using the more recent SVG-with-JS approach of FontAwesome. In which case you need to follow the instructions on their CSS Pseudo-Elements HowTo. Basically there are three things to watch out for:
This approach should be avoided. The default value for vertical-align
is baseline
. Changing the font-family of only the pseudo element will result in elements with differing fonts. Different fonts can have different font metrics and different baselines. In order for different baselines to align, the overall height of the element would have to increase. See this effect in action.
It is always better to have one element per font icon.
@keithwyland answer is great. Here's a SCSS mixin:
@mixin font-awesome($content){
font-family: FontAwesome;
font-weight: normal;
font-style: normal;
display: inline-block;
text-decoration: inherit;
content: $content;
}
Usage:
@include font-awesome("\f054");
In the case of your list items there is a little CSS you can use to achieve the desired effect.
ul.icons li {
position: relative;
padding-left: -20px; // for example
}
ul.icons li i {
position: absolute;
left: 0;
}
I have tested this in Safari on OS X.
<ul class="icons-ul">
<li><i class="icon-play-sign"></i> <a>option</a></li>
<li><i class="icon-play-sign"></i> <a>option</a></li>
<li><i class="icon-play-sign"></i> <a>option</a></li>
<li><i class="icon-play-sign"></i> <a>option</a></li>
<li><i class="icon-play-sign"></i> <a>option</a></li>
</ul>
All the font awesome icons comes default with Bootstrap.
This is the easiest way to do what you are trying to do:
<style>
ul {
list-style-type: none;
}
</style>
<ul class="icons">
<li><i class="fa fa-bomb"></i> Lists</li>
<li><i class="fa fa-bomb"></i> Buttons</li>
<li><i class="fa fa-bomb"></i> Button groups</li>
<li><i class="fa fa-bomb"></i> Navigation</li>
<li><i class="fa fa-bomb"></i> Prepended form inputs</li>
</ul>
Re: using icon in :before
–
recent Font Awesome builds include the .fa-icon()
mixin for SASS and LESS. This will automatically include the font-family
as well as some rendering tweaks (e.g. -webkit-font-smoothing
). Thus you can do, e.g.:
// Add "?" icon to header.
h1:before {
.fa-icon();
content: "\f059";
}
Source: Stackoverflow.com