2014-09-05 46 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