Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align using transform: translateY(-50%); inside absolutely positioned div

I'm trying to vertically centre a h2 inside a div that is absolutely positioned with a height of 50% using the following code:

#owl-demo h2 {
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}

Works fine on Chrome, but the h2 disappears in Safari IOS unless I set the h2 to be positioned absolute and then I can't centre the text with text-align.

Any help much appreciated, I've been trying to make this work all day.

Edit: So it looks like the height 50% is the problem on the parent div, if I remove it the text appears in IOS. I'm trying to achieve an overlay that covers 50% of the thumbnail height is there another way to get it to cover 50% of the thumbnail?

#owl-demo  .thumb-overlay {
    text-align: center;
    height: 50%;
width: 100%;
position: absolute;
left: 0;
bottom: 0;
background:rgba(0, 0, 0, .8);
z-index: 9999;
opacity:0;
    filter: alpha(opacity = 0);
    -webkit-transition: all 300ms ease-out;
    -moz-transition: all 300ms ease-out;
    -o-transition: all 300ms ease-out;
    -ms-transition: all 300ms ease-out;
    transition: all 300ms ease-out;
}

#owl-demo  .thumb-overlay:hover {
    opacity:0.75;
    filter: alpha(opacity = 75);
    transition:opacity 0.25s;
    -moz-transition:opacity 0.25s;
    -webkit-transition:opacity 0.25s;
}

.touch #owl-demo  .thumb-overlay,  {
    opacity:1;
    filter: alpha(opacity = 1);
}

#owl-demo h2 {
font-size: .875em;
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    color: $white;
}
like image 844
leanda Avatar asked Apr 09 '14 16:04

leanda


People also ask

How do you vertically center an absolute div position?

To center an element both vertically and horizontally: position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; But if you would like to understand how I came to these solutions, read further for more explanation.

How do I vertically align text in a div?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .

How do you vertically align absolute elements?

Method 1. First Method: We use 'left:0' , 'right:0' , and 'margin:auto' to achieve horizontal centering. Then, in order to achieve vertical centering, we use 'top: 50%' , which makes the element stay almost centered - this will only center the element according to its top boundary.

How will you horizontally and vertically center a div inside a div?

inner-box, center the image tag horizontally using text-align:center property as it's the inline element. Finally, add vertical-align:middle to the inner div which is pushed to vertically center to its parent element.


2 Answers

The following is working for me in safari (5.1.7)

#owl-demo h2 {
 font-size: 1em;
 -webkit-transform: translateY(100%);
 -ms-transform: translateY(100%);
 transform: translateY(100%);
 color: white;
}

JSFiddle

As for other ways, since you can make use of ::before pseudo element for aligning :

#owl-demo .thumb-overlay::before {
content:"";
display:block;
height:30%;

}

check this JSFiddle

like image 73
T J Avatar answered Sep 22 '22 00:09

T J


I found a solution today to solve this issue with mobile safari & mobile chrome.

Use the same code you already have for the CSS h2 in your example, except put vertical-align:top;

This seems to fix the translateY on mobile Safari. I hope this helps someone!

like image 21
Joseph Astrahan Avatar answered Sep 19 '22 00:09

Joseph Astrahan