vertical text center in <div>

放肆的年华 提交于 2019-12-02 21:47:55

问题


So i am trying get 2 div-containers which both should contain centered text (Both x- and y-axis).

Thanks to Google and stackoverflow, i stumbled over a few workarounds which play with vertical-align etc. But nothing seems to work.

#right-menu {
    position: absolute ;
    right: 0% ;
    top: 0% ;
    height: 100% ;
    width: 5% ;
    text-align:  center ;
    background: #ededed ;
        display: table-cell;

vertical-align: middle ;

Here is a fiddle:

http://jsfiddle.net/7Rgvs/


回答1:


Vertical-align:middle property is not used with position:absolute; if you are using vertical-align:middle then try this layout modified layout http://jsfiddle.net/7Rgvs/5/




回答2:


Basically first you have to define the parent element as table from CSS and then you can display its child element as a table cell.

For example,

 div.parent {
    display: table;
 }
 div.parent div.child {
    display: table-cell; 
     vertical-align: middle;  
}

Check out this link, it would clarify your queries.. http://css-tricks.com/vertically-center-multi-lined-text/




回答3:


use display: table for the (holder #left-menu element) and display: table-cell for the inner holder element and use this styles:

#left-menu .inner {
    display: table-cell;
    margin: 0 auto;
    text-align: center;
    vertical-align: middle;
    width: 100%;
}

i have edit the left menu inside your fiddle: http://jsfiddle.net/7Rgvs/



来源:https://stackoverflow.com/questions/11048541/vertical-text-center-in-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!