2014-09-05 47 views
2

我想在boxes div中間垂直對齊+符號,但是我無法使它工作。我究竟做錯了什麼?我也想避免使用表格。謝謝(我還附上codepen鏈接)如何在兩個div中間垂直對齊文本

<div class="boxes"> 
    <div class="boxes_box"> 
    </div> 
    <div class="boxes_plus">+</div> 
    <div class="boxes_box"> 
    </div> 
</div> 

CSS

.boxes { 
    height: 250px; 
} 
.boxes_box { 
    width: 250px; 
    height: 250px; 
    display:inline-block; 
    background:#000; 
} 
.boxes_plus { 
    display:inline-block; 
    height:250px; 
    line-height:250px; 
    vertical-align:middle; 
} 

http://codepen.io/anon/pen/aoiGm

+0

對不起,我忘了提,我想避免第一 – simPod 2014-09-05 13:30:59

回答

2

對於來自你有什麼變化最小,改變.box_plusvertical-aligntop

http://codepen.io/jwhitfieldseed/pen/FeJco

說明:line-height放入的.boxes_plus垂直中心的 「+」 文本。

文本已經在其容器中垂直居中,所以您現在需要使.boxes_plus的頂部與頂部.boxes_box對齊。

+0

謝謝,它幫助表我瞭解它究竟做了什麼 – simPod 2014-09-05 13:29:58

3

使用本:

.boxes { 
    height: 250px; 
    display:table;/*Add display table*/ 
} 
.boxes_box { 
    width: 250px; 
    height: 250px; 
    display:inline-block; 
    background:#000; 
    display:table-cell;/*display table cell here is not necessary*/ 
} 
.boxes_plus { 
    display:inline-block; 
    height:250px; 
    line-height:250px; 
    vertical-align:middle; 
    display:table-cell;/*Add display table cell*/ 
} 

fiddle

替代,你可以簡單的刪除line-height

.boxes_plus { 
    display:inline-block; 
    height:250px; 
    /*line-height:250px;*/ 
    vertical-align:middle; 
} 

fiddle

+0

無論如何做到這一點沒有表? (忘記提及,對不起) – simPod 2014-09-05 13:25:46

+0

是的,只是刪除'行高'http://jsfiddle.net/cea6qhgg/4/ – 2014-09-05 13:27:38

+0

謝謝我的作品,謝謝! – simPod 2014-09-05 13:30:25

2

請更新你的CSS如下

.boxes { 
    height: 250px; 
    display: table 
} 
.boxes_box { 
    width: 250px; 
    height: 250px; 
    display:table-cell; 
    background:#000; 
} 
.boxes_plus { 
    display:table-cell; 
    height:250px; 
    line-height:250px; 
    vertical-align:middle; 
} 

http://codepen.io/anon/pen/crBea

2

試試這個:

DEMO

.boxes { 
    height: 250px; 
    display:table; 
} 
.boxes_box { 
    width: 250px; 
    height: 250px; 
    display:inline-block; 
    background:#000; 
} 
.boxes_plus { 
    display:table-cell; 
    height:250px; 
    line-height:250px; 
    vertical-align:middle; 
} 
3
<style> 
.boxes { 
    height: 250px; 
    display:table; 
} 
.boxes_box { 
    width: 250px; 
    height: 250px; 
    display:table-cell; 
    background:#000; 
} 
.boxes_plus { 
    display:table-cell; 
    height:250px; 
    line-height:250px; 
    vertical-align:middle; 
} 
</style> 
+0

我正要提示我的朋友完全一樣的東西! >:3 – Allan 2014-09-05 15:23:48