Center text in div?
Asked Answered
C

14

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?

Cordero answered 19/5, 2011 at 7:45 Comment(2)
Centered top to bottom, right to left, or both?Marnie
Possible duplicate of CSS center text (horizontally and vertically) inside a div blockProprietor
I
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

Inferential answered 19/5, 2011 at 7:48 Comment(0)
M
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.

Marnie answered 19/5, 2011 at 8:29 Comment(1)
The "vertical-align: middle" in the provided example has no effect. It's the line-height of the span that is centering the textRoughhew
D
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.

Ducks answered 19/5, 2011 at 8:2 Comment(2)
Note: this requires that the <div> is set to position: relative;.Singlecross
@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.Ducks
P
8

For the parent div,use following CSS:

style="display: flex;align-items: center;"
Passacaglia answered 3/12, 2016 at 13:29 Comment(0)
R
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; 
}
Roughhew answered 7/4, 2014 at 15:46 Comment(1)
Unlike one of the other methods, I like that this one doesnt require me to keep track of the inner & outer's heightFisher
F
6

If you have text inside a <div>:

text-align: center;
vertical-align: middle;
display: table-cell;
Freeland answered 21/5, 2014 at 17:21 Comment(0)
H
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.

Helping answered 8/8, 2015 at 5:56 Comment(0)
I
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

Inferential answered 19/5, 2011 at 7:48 Comment(0)
G
2

Adding line height helps too.

text-align: center;
line-height: 18px; /* for example. */
Geophysics answered 24/3, 2015 at 2:8 Comment(0)
C
0

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

text-align:center; 

??

Craunch answered 19/5, 2011 at 7:48 Comment(2)
thats fine on two lines but on one lne theres too big a gap after the first lineCordero
That only centers horizontally. The question seems to imply the OP wants to center vertically.Stringendo
L
0

Will this work for you?

div { text-align: center; }
Lula answered 19/5, 2011 at 7:48 Comment(0)
R
0

I've looked around and the

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

seems to be the most popular solution

Respirator answered 25/3, 2013 at 3:26 Comment(0)
I
0
display: block;
text-align: center;
Interpleader answered 20/4, 2014 at 16:46 Comment(0)
P
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';
    }

}

Preparative answered 20/12, 2022 at 4:54 Comment(0)
W
-1

Add to the selector containing the text

margin:auto;
Woald answered 11/10, 2017 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.