Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a fontawesome icon inside a div element

I have a div with width: 180px; and height: 180px;.
How can I center a fontawesome icon horizontally and vertically inside the container?
Note: i may also add text below the icon

<div style="width:180px; height:180px; float:left;">
  <i class="fa fa-search fa-4x"></i>
</div>

should look something like this:
enter image description here

like image 720
Aritra Hazra Avatar asked Sep 07 '14 06:09

Aritra Hazra


2 Answers

Try this:

.centered{
position:relative;
top:50%;
left:50%;
transform:translate(-50%,-50%)
}

.container{
width:100px;
height:100px;
}

On mobile.

like image 174
DifferentPseudonym Avatar answered Oct 23 '22 21:10

DifferentPseudonym


You can do it like this

#search{
    background: gray;
    width:180px; 
    height:180px; 
    float:left; 
    line-height: 180px;     
    text-align: center;
    vertical-align: bottom;
}

#search > p {
    margin-top: -155px;
}

Working example http://jsfiddle.net/kasperFranz/h91p8w4e/3/ (using icon instead of fa in the example, but shouldn't affect the result.)

like image 42
Kasper Sanguesa-Franz Avatar answered Oct 23 '22 21:10

Kasper Sanguesa-Franz