Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering FontAwesome icons vertically and horizontally

I'm currently using FontAwesome, and am having a really hard time centering the icons both vertically and horizontally in their container. I have tried doing it via positioning and ran into issues bc the icon sizes were different. I basically have the horizontal, and am trying to get the vertical.

<div class='container'>     <div class='row'>         <div class='offset2 span6 loginContainer'>             <div class='row'>                 <div class='login-icon'>                         <i class='icon-user'></i>                 </div>                 <input type="text"  placeholder="Email" />              </div>             <div class='row'>                 <div class='login-icon'><i class=" icon-lock "></i></div>                 <input type="password" class="" placeholder="Password" />             </div>         </div>     </div> </div>  .login-icon{     font-size: 40px;     line-height: 40px;     background-color:black;     color:white;     width: 50px;     height: 50px;  } .login-icon [class*='icon-']{   height: 50px;   width: 50px;   display: inline-block;   text-align: center;   vertical-align: baseline; } 

http://jsfiddle.net/ncapito/e2UPC/

like image 567
Nix Avatar asked Jul 14 '13 19:07

Nix


People also ask

How do I center Font Awesome icons horizontally?

The most preferred way to center Font Awesome Icons is to assign a center class to each <i> tag. Setting width to 100%, let's each icon cover 100% area horizontally. Also text-align then centers the icon accordingly to the width being used.

How do I center Font Awesome icons vertically?

If you want to make a text appear vertically aligned next to a Font Awesome icon, you can use the CSS vertical-align property set to “middle” and also, specify the line-height property. Change the size of the icon with the font-size property.

How do I align Font Awesome icons to the right?

To pull icon fa-align-right to the right of the container use fa-pull-right class.


2 Answers

This is all you need, no wrapper needed:

.login-icon{     display:inline-block;     font-size: 40px;     line-height: 50px;     background-color:black;     color:white;     width: 50px;     height: 50px;     text-align: center;     vertical-align: bottom; } 

http://jsfiddle.net/e2UPC/6/

like image 62
omma2289 Avatar answered Sep 21 '22 08:09

omma2289


I have used transform to correct the offset. It works great with round icons like the life ring.

<span class="fa fa-life-ring"></span>  .fa {     transform: translateY(-4%); } 
like image 36
Phuc Le Avatar answered Sep 20 '22 08:09

Phuc Le