49

I have a div 30px high and 500px wide. This div can contain two lines of text one under the other, and is styled (padded) accordingly. But sometimes it only contains one line, and I want it to be centered. Is this possible?

2

14 Answers 14

58

To center horizontally, use text-align:center.

To center vertically, one can only use vertical-align:middle if there is another element in the same row that it is being aligned to.
See it working here.

We use an empty span with a height of 100%, and then put the content in the next element with a vertical-align:middle.

There are other techniques such as using table-cell or putting the content in an absolutely positioned element with top, bottom, left, and right all set to zero, but they all suffer from cross browser compatibility issues.

1
  • 15
    The "vertical-align: middle" in the provided example has no effect. It's the line-height of the span that is centering the text
    – Lars
    Apr 7, 2014 at 15:40
25

I believe you want the text to be vertically centered inside your div, not (only) horizontally. The only reliable way I know of doing this is using:

display: table-cell;
vertical-align: middle;

on your div, and it works with any number of lines. You can see a test here: http://jsfiddle.net/qMtZV/1/

Be sure to check browser support of this property, as it is not supported — for example — in IE7 or earlier.

UPDATE 02/10/2016

Five years later this technique is still valid, but I believe there are better and more solid solutions to this problem. Since Flexbox support is good nowadays, you might want to do something along these lines: http://codepen.io/michelegera/pen/gPZpqE.

2
  • Note: this requires that the <div> is set to position: relative;. Apr 26, 2017 at 10:30
  • @JamieBirch it should be set to relative or to the (default) value of static. It won’t center the lines when position is absolute or fixed. Apr 26, 2017 at 14:08
8

For the parent div,use following CSS:

style="display: flex;align-items: center;"
7

HTML

<div class="outside">
  <div class="inside">
      <p>Centered Text</p>
  </div>
</div>

CSS

.outside { 
  position: absolute; 
  display: table; 
}

.inside {
  display: table-cell; 
  vertical-align: middle; 
  text-align: center; 
}
1
  • 1
    Unlike one of the other methods, I like that this one doesnt require me to keep track of the inner & outer's height
    – Nyaarium
    Apr 7, 2015 at 12:53
5

If you have text inside a <div>:

text-align: center;
vertical-align: middle;
display: table-cell;
5
div {
    height: 256px;
    width: 256px;
    display: table-cell;
    text-align: center;
    line-height: 256px;
}

The trick is to set the line-height equal to the height of the div.

2

You may try to use in your CSS the property vertical-align in order to center it verticaly

div {  
    vertical-align:middle;  
}

if it's a size problem, please notice that 2 text lines and a padding style have great chance to have a height superior to 30px.

For example, if your font size is 12 px and your div padding is 5 px, a one text line div height will be 5px (padding-top) + 12px + 5 px (padding-bottom) = 22px < 30px so no problem,

With a 2 text lines div, it will be 5px +12px *2 (2 lines) + 5px = 34px > 30px and your div height will be automatically changed.

Try either to increase your div height (maybe 40px) or to reduce your padding.

Hope it will help

2

Adding line height helps too.

text-align: center;
line-height: 18px; /* for example. */
0

I may be missing something here, but have you tried:

text-align:center; 

??

2
  • thats fine on two lines but on one lne theres too big a gap after the first line
    – Rifki
    May 19, 2011 at 7:50
  • 2
    That only centers horizontally. The question seems to imply the OP wants to center vertically.
    – waldyrious
    May 4, 2013 at 12:18
0

Will this work for you?

div { text-align: center; }
0

I've looked around and the

display: table-cell;
vertical-align: middle;

seems to be the most popular solution

0
display: block;
text-align: center;
0
<div class="header">
<div class="phone">&#9742; 052 824 134</div>
<div class="adres">&#10031; бул. Княз Борис I, No 115 - Дворец на Културата и Спорта</div>

using flexbox To center div

.header{
        background: #ff6400;
        height:100px;
        box-sizing: border-box;
        display: flex;
        justify-content:'center';
        flex-direction: column;
    }
    .phone{
        color: white; 
        font-size: 20px; 
        padding: 9px 0 0 125px;
     
    }
    .adres{
        display: inline-block;
        color: white; 
        font-size: 20px;
        width: 50%;
        margin: 0 auto;
        text-align:'center';
    }

}

-1

Add to the selector containing the text

margin:auto;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.