47

I want my <p> element to be at the center of a container <div>, as in perfectly centered -- the top, bottom, left and right margins split the spaces equally.

How can I achieve that?

div {
  width: 300px;
  height: 100px;
}
p {
  position: absolute;
  top: auto;
}
<div>
  <p>I want this paragraph to be at the center, but it's not.</p>
</div>
showdev
  • 25,529
  • 35
  • 47
  • 67
Johnsy
  • 1,093
  • 3
  • 12
  • 13
  • 2
    That I know of, you can only vertically center with table cells, JavaScript, or if you know the height of the element to be vertically centered ahead of time – Explosion Pills Feb 27 '13 at 20:08
  • @ExplosionPills is right. You'd need to know the height of the

    tag. If you did, you could do something like this in your stylesheet: p { width: 200px; height: 10px; position: relative; top: 50%; left: 50%; margin: -100px 0 0 -5px; }

    – Spencer Cameron-Morin Feb 27 '13 at 20:14
  • 2
    Here are two simple methods to center an element within a div, vertically, horizontally or both (pure CSS): http://stackoverflow.com/a/31977476/3597276 – Michael Benjamin Aug 19 '15 at 21:12

7 Answers7

71

You dont need absolute positioning Use

p {
 text-align: center;
line-height: 100px;

}

And adjust at will...

If text exceeds width and goes more than one line

In that case the adjust you can do is to include the display property in your rules as follows;

(I added a background for a better view of the example)

div
{
  width:300px;
  height:100px;  
  display: table; 
  background:#ccddcc;  
}


p {
  text-align:center; 
  vertical-align: middle;
  display: table-cell;   
}

Play with it in this JBin

enter image description here

Fico
  • 2,037
  • 16
  • 18
  • 4
    I didn't vote you down but this will fail if the text is longer than a single line. – insertusernamehere Feb 27 '13 at 20:13
  • Alright, I followed your advice and with a little tweaking it worked. div { width:300px; background-color:yellow; padding-top:50px; padding-bottom:50px } p { text-align:center; }

    Lorem ipsum dolor sit amet

    Like that, it centers perfectly, but Is this the right way to do?
    – Johnsy Feb 27 '13 at 20:13
  • @insertusernamehere Thats why he must then adjust at will. As for example wraping the paragraph etc. I was adressing the center issue. – Fico Feb 27 '13 at 20:16
  • You are welcome @Johnsy Omniscient. Unfortunately , in spite of me trying to help, I recived a down vote. Who knows why!!. Have a good day – Fico Feb 27 '13 at 20:21
  • Johnsy & @insertusernamehere, Last but not least. I extended my answer to cover the case of a longer text that the width available. I included a jsBin to play with. See you! :) – Fico Feb 27 '13 at 21:37
  • Ok ok, thanks again, now I've read this thread again and I know what to do next time I encounter this problem. ^_^ – Johnsy Feb 28 '13 at 16:44
11

To get left/right centering, then applying text-align: center to the div and margin: auto to the p.

For vertical positioning you should make sure you understand the different ways of doing so, this is a commonly asked problem: Vertical alignment of elements in a div

Community
  • 1
  • 1
GordonsBeard
  • 528
  • 3
  • 14
2

♣you should do these steps :

  1. the mother Element should be positioned(for EXP you can give it position:relative;)
  2. the child Element should have positioned "Absolute" and values should set like this: top:0;buttom:0;right:0;left:0; (to be middle vertically)
  3. for the child Element you should set "margin : auto" (to be middle vertically)
  4. the child and mother Element should have "height"and"width" value
  5. for mother Element => text-align:center (to be middle horizontally)

♣♣simply here is the summery of those 5 steps:

.mother_Element {
    position : relative;
    height : 20%;
    width : 5%;
    text-align : center
    }
.child_Element {
    height : 1.2 em;
    width : 5%;
    margin : auto;
    position : absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    }
kia nasirzadeh
  • 1,411
  • 10
  • 20
0

Centered and middled content ?

Do it this way :

<table style="width:100%">
    <tr>
        <td valign="middle" align="center">Table once ruled centering</td>
    </tr>
</table>

I fiddled it here

Ha, let me guess .. you want DIVs ..

just make your first outter DIV behave like a table-cell then style it with vertical align:middle;

<div>
    <p>I want this paragraph to be at the center, but I can't.</p>
</div>

div {
    width:500px;
    height:100px;
    background-color:aqua;
    text-align:center;
    /*  there it is */
    display:table-cell;
    vertical-align:middle;
}

jsfiddle.net/9Mk64/

Milche Patern
  • 17,400
  • 6
  • 31
  • 52
0

on the p element, add 3 styling rules.

.myCenteredPElement{
    margin-left:  auto;
    margin-right: auto;
    text-align: center;
}
freddy mercury
  • 580
  • 5
  • 17
0

This solution works fine for all major browsers, except IE. So keep that in mind.

In this example, basicaly I use positioning, horizontal and vertical transform for the UI element to center it.

        .container {
            /* set the the position to relative */
            position: relative;
            width: 30rem;
            height: 20rem;
            background-color: #2196F3;
        }


        .paragh {
            /* set the the position to absolute */
            position: absolute;
            /* set the the position of the helper container into the middle of its space */
            top: 50%;
            left: 50%;
            font-size: 30px;
            /* make sure padding and margin do not disturb the calculation of the center point */
            padding: 0;
            margin: 0;
            /* using centers for the transform */
            transform-origin: center center;
            /* calling calc() function for the calculation to move left and up the element from the center point */
            transform: translateX(calc((100% / 2) * (-1))) translateY(calc((100% / 2) * (-1)));
        }
<div class="container">
    <p class="paragh">Text</p>
</div>

I hope this help.

Zoltán
  • 11
  • 1
  • 5
-1

You only need to add text-align: center to your <div>

In your case also remove both styles that you added to your <p>.

Check out the demo here: http://jsfiddle.net/76uGE/3/

Good Luck

JGallardo
  • 9,783
  • 7
  • 72
  • 84
John
  • 144
  • 3
  • 12