我有一个width: 180px;和height: 180px;的div。
如何在容器内水平和垂直地对方舟形图标进行居中?
注意:我也可以在图标下面添加文本。
<div style="width:180px; height:180px; float:left;">
  <i class="fa fa-search fa-4x"></i>
</div>应该是这样的:

发布于 2014-09-07 06:46:03
你可以这样做
#search{
    background: gray;
    width:180px; 
    height:180px; 
    float:left; 
    line-height: 180px;     
    text-align: center;
    vertical-align: bottom;
}
#search > p {
    margin-top: -155px;
}工作示例http://jsfiddle.net/kasperFranz/h91p8w4e/3/ (在示例中使用图标而不是fa,但不应该影响结果)。
发布于 2014-09-07 08:07:57
试试这个:
.centered{
position:relative;
top:50%;
left:50%;
transform:translate(-50%,-50%)
}
.container{
width:100px;
height:100px;
}在手机上。
发布于 2014-09-07 06:47:25
就我的评论而言,这是一个JSFIDDLE
不设置relative或absolute
html
<div class="myDiv">
    <div class="content_wrapper">
        <!--i class="fa fa-search fa-4x">test</i-->
        <i class="icon-search icon-4x"></i><br />
        <span class="myText">Search</span>
    </div>
</div>css
.myDiv{
    width:         180px; 
    height:        180px; 
    float:         left;
    background:    #ccc;
    text-align:    center;
    display:       table;
}
.content_wrapper{
    display: table-cell;
    vertical-align: middle;
}
.myText{
    font-weight: bold;
    font-size:   20px;
}https://stackoverflow.com/questions/25707715
复制相似问题