-2

I am trying to vertically center my div that is of absolute position inside a relative position div. The problem is that my parent div has an ever expanding height. Today it could be 300, tomorrow 100. So defining the height is not possible but the child div has to be vertically centered.

Here is what I have tried:

.parrent{
  position: relative;
}

.child {
  background: red;
  width: 50px;
  height: 50px;
  
  position: absolute;
  top: 0;
  bottom: 0;
}
<div class="parrent">
  <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>
  
  <div class="child">
  
  </div>
</div>

As I need this working as soon as possible, I'm open to any solutions.

Bhuwan
  • 15,392
  • 4
  • 26
  • 50
Bagzli
  • 5,388
  • 14
  • 58
  • 124
  • 1
    it is expected you do a little research before posting a question. This has been asked hundreds of times and there are many answers on this site. Please try to put in a little more effort before you ask a question next time – Pete Jan 26 '18 at 15:11

3 Answers3

4

You can place .child at top: 50%; and move it up with transform: translateY(-50%);, which will centre your element. This will put your element 50% from the top of the parent, and move it 50% up of the child element's height.

.parrent{
  position: relative;
}

.child {
  background: red;
  width: 50px;
  height: 50px;

  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

See working solution on this fiddle

chriskirknielsen
  • 2,560
  • 2
  • 9
  • 19
  • 1
    This did the job. Put this fiddle and code from it as part of your answer so future visitors may see it: https://jsfiddle.net/ocbzdkpd/2/ – Bagzli Jan 26 '18 at 15:12
2

You can use this snippet:

https://jsfiddle.net/ocbzdkpd/1/

Basically you need:

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

Because doesn't matter the sizes of the boxes.

Marcos Pérez Gude
  • 20,206
  • 4
  • 34
  • 65
  • As a caution I can't remember the exact root issue, but I remember there was some sort of compatibility issue with `translateY` that led me to always use `transform: translate(0,-50%)` instead for full compatibility – jmcgriz Jan 26 '18 at 15:12
1

Flexbox can help you here.

.parent{
  display:flex;
  align-items:center;     // vertical alignment
}

.child {
  background: red;
  width: 50px;
  height: 50px;   
  position: absolute;
}
<div class="parent">
  <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>   
  <div class="child"></div>
</div>
Scott Marcus
  • 57,085
  • 6
  • 34
  • 54