2014-01-13 71 views
0

我想中心对齐动态高度固定div内的浮动div。 有没有办法做到这一点,没有定义高度,但固定的父div?如何垂直对齐动态高度

这里是HTML代码:

<div class="main"> 
    <div class="child"> 
     test 
    </div> 
</div> 

这里是CSS:

.main{ 
    position:fixed; 
    height:100px; 
    top:0px; 
    width:100%; 
    background-color:yellow; 
} 

.child{ 
    display:table-cell; 
    height:100%; 
    vertical-align:middle; 
    width:100px; 
    background-color:lightblue; 
    float:left; 
} 

这里是与它小提琴:the link to the jsFiddle

谢谢:)

回答

1

试试这个代码:

http://jsfiddle.net/KS523/4/

.child{ 
    height:100%; 
    vertical-align: center; 
    width:100px; 
    background-color:lightblue; 
    display:inline-block; 


/* Internet Explorer 10 */ 
display:-ms-flexbox; 
-ms-flex-pack:center; 
-ms-flex-align:center; 

/* Firefox */ 
display:-moz-box; 
-moz-box-pack:center; 
-moz-box-align:center; 

/* Safari, Opera, and Chrome */ 
display:-webkit-box; 
-webkit-box-pack:center; 
-webkit-box-align:center; 

/* W3C */ 
display:box; 
box-pack:center; 
box-align:center; 

} 

新的零件,使水平方向和垂直方向上居中对于其父元素。

+0

嗨@filip,谢谢,但这不是我要找的。 我需要孩子的内容与中间的垂直对齐+孩子应该漂浮在左边,而不是在父母的中心。 (“测试”应该是垂直中间的位置) –

+0

这就是我要找的http://jsfiddle.net/KS523/3/但没有高度:100px;在子元素,而是我希望它是由父母动态高度(身高:100%) –

+0

@ErabBO改变了代码,希望能帮助你多一点! –