2016-05-29 10 views
0

なぜvertical-align: middle;は、アイコンをヘッダーの中央に揃えないのですか?Fontawasome vertical icon align

.custom { 
 
     color: white; 
 
     vertical-align: middle; 
 
    } 
 
    .header { 
 
     width: 100%; 
 
     height: 40px; 
 
     background: rgb(40,40,40); 
 
    }
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"/> 
 
<div class="header"> 
 
    <i class="fa fa-facebook custom" aria-hidden="true"></i> 
 
    <i class="fa fa-twitter custom" aria-hidden="true"></i> 
 
    <i class="fa fa-instagram custom" aria-hidden="true"></i> 
 
    <i class="fa fa-google-plus custom" aria-hidden="true"></i> 
 
</div>

答えて

1

これらの日、垂直方向に中央に揃えるための最善の方法は、フレキシボックスを使用することです。参照:

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/

.custom { 
 
     color: white; 
 
     vertical-align: middle; 
 
    } 
 

 
.header { 
 
    
 
    display:flex; 
 
    align-items: center; 
 

 
     width: 100%; 
 
     height: 40px; 
 
     background: rgb(40,40,40); 
 
    }
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"/> 
 
<div class="header"> 
 
    <i class="fa fa-facebook custom" aria-hidden="true"></i> 
 
    <i class="fa fa-twitter custom" aria-hidden="true"></i> 
 
    <i class="fa fa-instagram custom" aria-hidden="true"></i> 
 
    <i class="fa fa-google-plus custom" aria-hidden="true"></i> 
 
</div>

1
.custom { 
    color: white; 
    vertical-align: middle; 
    **line-height: 40px;** 
} 
0

.custommargin-top=2.5%またはmargin=2.5%に余裕を与え、それがうまくいきます。

一方、vertical-alignは、このような場合にも機能します。

<a class="header"> 
    <i class="fa fa-facebook custom" aria-hidden="true"></i> 
</a> 
0

あなたが高削除の高さを言及してきたし、それはあなたのヘッダーのため、代わりに親クラスの高さ使用パディングの、真ん中に整列されますので、そのが働いていない理由はあります。

.custom { 
    color: white; 
    vertical-align: middle; 
} 
.header { 
    width: 100%; 
    padding:20px; 
    background: rgb(40,40,40); 
} 
関連する問題