Center content in a absolute positioned div
Asked Answered
L

6

25

I have an absolutly positioned element with content inside. It can be a <h1> or a few <p> tag. So I don't know the height of the content.

How can I vertically center the content inside the absolutly positioned div?

HTML :

<div id="absolute">
    <div class="centerd">
    <h1>helo</h1>
    <span>hi again</span>
    </div>
</div>   

CSS :

#absolute {
    position:absolute;
    top:10px;
    left:10px;
    width:50%;
    height:50%;
    background:yellow;
}

.centerd {
    display:table-cell;
    vertical-align:middle;
}

Fiddle

Leroi answered 26/4, 2013 at 14:34 Comment(0)
I
14

if you add display:table to your #absolute div you should get what you want:

http://jsfiddle.net/3KTUM/2/

Idiophone answered 26/4, 2013 at 14:37 Comment(2)
This doesn't center the content .. it just makes the content start from the centerGoldia
@ExplosionPills Seems to center the content for me - try adding more content in and you will see it stays vertically aligned, not start from the center. You may think it starts in the center as the h1 will have natural top padding which pushes it downIdiophone
B
22

Horizontal and vertical position absolute middle.

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50% , -50%);
  -webkit-transform: translate(-50%, -50%);
}
<div class="obj">
  <div class="center">Test</div>
</div>
Bathsheba answered 7/3, 2018 at 11:33 Comment(1)
the question was how can I vertically center the content inside the absolutely positioned div? the absolutely positioned div is .obj. your answer doesn't have position: absolute; on .obj, it doesn't work when it is applied, see this jsFIddle.Longspur
I
14

if you add display:table to your #absolute div you should get what you want:

http://jsfiddle.net/3KTUM/2/

Idiophone answered 26/4, 2013 at 14:37 Comment(2)
This doesn't center the content .. it just makes the content start from the centerGoldia
@ExplosionPills Seems to center the content for me - try adding more content in and you will see it stays vertically aligned, not start from the center. You may think it starts in the center as the h1 will have natural top padding which pushes it downIdiophone
N
12

This can be done with flexbox too:

#absolute {
  align-items: center;
  display: flex;
  justify-content: center;
}
Neoprene answered 9/6, 2017 at 13:18 Comment(0)
S
7

Change your #absolute div to also use:

display:table;
vertical-align:middle;
text-align:center;

http://jsfiddle.net/3KTUM/4/

Sculpturesque answered 26/4, 2013 at 14:39 Comment(0)
S
2

Just make the div relative to its absolute parent and use text-align: center, like this:

.centerd {
    position: relative;
    width: 100%;
    text-align: center;
    /*display:table-cell;
    vertical-align:middle;*/
}

See example fiddle

Signor answered 26/4, 2013 at 14:42 Comment(1)
Your fix correctly shows how to align text elements horizontally, but the OP asked how to align things vertically.Courson
A
1

use text-align:center or left:50%

Amaranthaceous answered 22/8, 2016 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.