开发者

How can I make my text appear in the center of this DIV both vertically and horizontally? [duplicate]

This question already has answers here: How can I center text (horizontally and vertically) inside a div block? (27 answers) 开发者_如何学Python Closed 4 months ago.

I need to center text vertically and horizontally. Is there a simple way to do this for the following code?

<div style="display: block; height: 25px;">
    <span id="ctl_txt" style="background-color: rgb(255, 0, 0);">
        Basic
    </span>
</div>


You can use the text-align property to center the text horizontally, and line-height equal to the div height to center inline elements vertically.

<div style="display: block; height: 25px; text-align:center; line-height:25px;">
    <span id="ctl_txt" style="background-color: rgb(255, 0, 0);">Basic</span>
</div>

Example here.


Another way to do this, different from @Jose, is to use

display:table; and display:table-cell

like this

div{
    display:table; 
    height:125px;  //JUST FOR SHOW
    width:125px;   //JUST FOR SHOW
}

span{
    background-color: rgb(255, 0, 0);
    display:table-cell;
    vertical-align:middle;
    text-align:center;
}

Then vertical-align:middle; and text-align:center; do the work.

Example: http://jsfiddle.net/jasongennaro/unfwL/1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜