CSS center text (horizontally and vertically) inside a div block

I have a div set to display:block . ( 90px height and width ) and I have some text inside.

I need the text to be aligned in the center both vertically and horizontally.

I have tried text-align:center , but it doesn't do the horizontal part so I tried vertical-align:middle but it didn't work.

Any ideas?


If it is one line of text and/or image, then it is easy to do. Just use

text-align: center;
vertical-align: middle;
line-height: 90px;       /* the same as your div height */

that's it. If it can be multiple lines, then it is somewhat more complicated. But there are solutions on http://pmob.co.uk/ Look for "vertical align".

Since they tend to be hacks or adding complicated divs... I usually use a table with a single cell to do it... to make it as simple as possible.


Update for 2016 / 2017:

It can be more commonly done with transform , and it works well even in older browsers such as IE10 and IE11. It can support multiple lines of text:

position: relative;
top: 50%;
transform: translateY(-50%); 

example: https://jsfiddle.net/wb8u02kL/1/

To shrink wrap the width:

The solution above used a fixed width for the content area. To use a shrink wrap width, use

position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

example: https://jsfiddle.net/wb8u02kL/2/

I tried flexbox, but it wasn't as widely supported, as it would break in some older version of IE such as IE10.


Common techniques as of 2014:


  • Approach 1 - transform translateX / translateY :

    Example Here / Full Screen Example

    In supported browsers (most of them), you can use top: 50% / left: 50% in combination with translateX(-50%) translateY(-50%) to dynamically vertically/horizontally center the element.

    .container {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateX(-50%) translateY(-50%);
    }
    

  • Approach 2 - Flexbox method:

    Example Here / Full Screen Example

    In supported browsers, set the display of the targeted element to flex and use align-items: center for vertical centering and justify-content: center for horizontal centering. Just don't forget to add vendor prefixes for additional browser support (see example).

    html, body, .container {
        height: 100%;
    }
    .container {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    

  • Approach 3 - table-cell / vertical-align: middle :

    Example Here / Full Screen Example

    In some cases, you will need to ensure that the html / body element's height is set to 100% .

    For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle .

    For horizontal centering, you could either add text-align: center to center the text and any other inline children elements. Alternatively, you could use margin: 0 auto assuming the element is block level.

    html, body {
        height: 100%;
    }
    .parent {
        width: 100%;
        height: 100%;
        display: table;
        text-align: center;
    }
    .parent > .child {
        display: table-cell;
        vertical-align: middle;
    }
    

  • Approach 4 - Absolutely positioned 50% from the top with displacement:

    Example Here / Full Screen Example

    This approach assumes that the text has a known height - in this instance, 18px . Just absolutely position the element 50% from the top, relative to the parent element. Use a negative margin-top value that is half of the element's known height, in this case - -9px .

    html, body, .container {
        height: 100%;
    }
    .container {
        position: relative;
        text-align: center;
    }
    .container > p {
        position: absolute;
        top: 50%;
        left: 0;
        right: 0;
        margin-top: -9px;
    }
    

  • Approach 5 - The line-height method (Least flexible - not suggested):

    Example Here

    In some cases, the parent element will have a fixed height. For vertical centering, all you have to do is set a line-height value on the child element equal to the fixed height of the parent element.

    Though this solution will work in some cases, it's worth noting that it won't work when there are multiple lines of text - like this.

    .parent {
        height: 200px;
        width: 400px;
        text-align: center;
    }
    .parent > .child {
        line-height: 200px;
    }
    

  • Methods 4 and 5 aren't the most reliable. Go with one of the first 3.


    Using flexbox/CSS:

    <div class="box">
        <p>&#x0D05;</p>
    </div>
    

    The CSS:

    .box{
        display: flex;
        justify-content: center;
        align-items: center;
    }
    

    Taken from Quick Tip: The Simplest Way To Center Elements Vertically And Horizontally

    链接地址: http://www.djcxy.com/p/13226.html

    上一篇: 如何将绝对定位的元素居中在div中?

    下一篇: div中的CSS中心文本(水平和垂直)