I want to have a CSS selector for a header with custom font, color and a bullet to the left. So I want my header to use my custom font, and it's :before
pseudo-element to use font-awesome. So I would like my :before
to have the .fa
class, while the whole element doesn't have this class.
I have this html: <h1 class="bulleted-header">Hello</h1>
And I would like to write something like this in LESS:
.bulleted-header {
color: #61cbe6;
font: 16px 'ds_goose', sans-serif;
&:before {
content: @fa-var-bullseye; // font-awesome's bullet icon
.fa; // calling font-awesome's class. DOESN'T COMPILE
}
}
The problem is that .fa
class is declared like this in font-awesome LESS sources: .@{fa-css-prefix} { ... }
, so the code above doesn't compile. I tried to reuse the .fa
code like this:
&:before {
content: @fa-var-bullseye; // font-awesome's bullet icon
.@{fa-css-prefix}; // DOESN'T COMPILE
}
and like this:
&:before:extend(.@{fa-css-prefix}) { // compiles but no effect
content: @fa-var-bullseye; // font-awesome's bullet icon
}
I know I can just change my html like this <h1 class="bulleted-header"><span class = "fa fa-bullseye"></span>Hello</h1>
and not to use :before
at all, but it's more logical to keep all this bullets thing in CSS.
I ended up just copy-pasting the content of .fa
into my :before
, but there is something wrong in this approach because now I have to maintain this code myself in case FA guys change something. Are there any more elegant solutions?
To use font awesome icons as CSS content code follow the below steps. Add a unique CSS class name to the icon element you want to use. Set the font-weight css property as 900 (For Solid), 400 (Regular or Brands), 300 (Light for pro icons). Set the content css property to the unicode value font awesome icon.
fas - solid icons are usually filled with transparent outlines. far regular and fal light are similar. These icons are different than fas solid because they are mostly outlines and differ only in outline width. fal light icons have thinner outline compared to far regular.
Font Awesome Free is free, open source, and GPL friendly. You can use it for commercial projects, open source projects, or really almost whatever you want.
It has a performance advantage All the icons are included in one font file, so it only takes a single HTTP request to load Font Awesome. This is great for performance so it's worth considering Font Awesome as a solution.
You can not really use interpolated selectors for mixins in Less ... so the rules with interpolated selectors can not be included in other rulesets. See answer here:
LESS mixin a variable class name
But in your case you could just import the compiled css as less instead of the less files, if you are not doing any other special stuff with these less files. Simply by using:
@import (less) 'font-awesome.css';
Then you can use .fa
as a mixin, exactly like you wanted.
However, this way you don't import the character variables, which you could do separately, but instead you could also just use content: "\f140";
directly (the way it's used in .fa-bullseye:before
)
Or alternatively just extend the selectors imported from font-awesome.css
with your selector, like so:
.bulleted-header {
color: #61cbe6;
font: 16px 'ds_goose', sans-serif;
&:extend(.fa, .fa-bullseye all);
}
Hope this helps.
As of LESS >= 1.6 rules with interpolated selectors can be accessed as mixins. So calling .fa
as a mixin, like you do in your above example, should now work perfectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With