Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add font-awesome icon to a series of buttons

I know I can specifically add a font awesome to a button like this:

<a href="#" class="btn btn-default">Some text <i class="fa fa-chevron-right"></i></a>

But is there a way I can create a CSS class so that the icon is automatically inserted for all buttons?

like image 348
shenku Avatar asked Aug 08 '14 04:08

shenku


People also ask

How do I use Font Awesome icons on buttons?

Basic use. Note that the span element is also acceptable for use with icons. Notice that there are two parts to using an icon, the style prefix and the icon name. In the example above, the style prefix and icon name are fas and fa-thumbs-up , respectively.

How do I stack Font Awesome icons?

To stack multiple icons, use the fa-stack class on the parent HTML element of the 2 icons you want to stack. Then add the fa-stack-1x class for the regularly sized icon and add the fa-stack-2x class for the larger icon. fa-inverse can be added to the icon with the fa-stack-1x to help with a knock-out looking effect.

How do you put an icon inside a button?

It is as simple as applying Font Awesome icon on any button. The <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the web pages, it needs the font-awesome link inside the head section. The font-awesome icon can be placed by using the fa prefix before the icon's name.


2 Answers

This is with Glyphicon and FontAwesome. Add your unicode (found in the CSS file) in the content and create a class name that is appropriate for your situation or just add it to the .btn or .btn-default, but change the css to reflect that:

FontAwesome: http://jsbin.com/seguf/2/edit *

Glyphicon: http://jsbin.com/seguf/1/edit

HTML

  <a href="#" class="btn btn-default btn-plus">Some text</a>  

http://jsbin.com/seguf/2/edit

CSS

.btn-arrow:after {
    font-family: 'FontAwesome';
    content: '\f054';
    padding-left: 5px;
    position: relative;
    font-size: 90%;
}


.btn-plus:after {
    font-family: 'Glyphicons Halflings';
    content: '\2b';
    padding-left: 5px;
    position: relative;
    top: 1px;
    font-size: 90%;
}
like image 94
Christina Avatar answered Oct 19 '22 21:10

Christina


You can achieve this in CSS with the :after pseudo-element:

End Result

Firstly, remove the i element from your a element, then give your a element a new class. I'm going to use "chevron-right":

<a href="#" class="btn btn-default chevron-right">Some text</a>

Now navigate to FontAwesome's Cheatsheet to find out the Unicode ID for the icon you're wishing to use. In here we'll find the following:

fa-chevron-right [&#xf054;]

This means that the Unicode ID of our icon (in hexadecimal) is f054.

Now we can go to our CSS file and add the following:

.chevron-right:after {
    content: '\f054';
    font-family: 'FontAwesome';
    padding-left: 5px;
}

Note that I've replaced the &#x of &#xf054 with \ and dropped the semicolon completely.

Here's a JSFiddle demo showing both the HTML icon and the CSS icon in use together.

like image 5
James Donnelly Avatar answered Oct 19 '22 21:10

James Donnelly