Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is translateY(-50%) needed to center an element which is at top: 50%?

Tags:

html

css

I can see that this code works to align a div vertically within its parent element:

.element {   position: relative;   top: 50%;   transform: translateY(-50%); } 

The question is why? My first thought was that the parent element encompassed more than the viewport. I made my parent viewport height equal 100vh and width 100%. That did not work. I still needed the translate or a negative margin offset. Why do I need a negative offset when the parent element is set to margin: 0;? Is it because of a computed margin that I'm not taking into account?

like image 692
ltrainpr Avatar asked Nov 10 '16 14:11

ltrainpr


People also ask

What does translate 50% do?

The -50% transform basically means, in simple words, "move this element left and upwards by 50% of the computed dimension along the axis". -50% along the x-axis means "move me leftwards by half my computed width", likewise for that in the y-axis.

What does transform translateY do?

translateY() The translateY() function is a 2D transform function used to translate an element along the y-axis. It takes a translation value ty as an argument. This value specifies the amount by which an element is to be translated.

How do elements get centered?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do you center an element using translation?

Because translateX(-50%) moves something back to the left 50% (because of the - negative value), which means it pairs with left: 50%; to center something. If you want to use right: 50%; then use that with translateX(50%) to center.


2 Answers

top:0 (default)

By default, your element is at the top of the page, and the top of the element is at 0:

--------Top of Page-------- {element}   ------Middle of  Page------    ------Bottom of  Page------ 

top:50%

When you move it down by 50% height (50% of the entire page), the top of the element is at the 50% mark, meaning the element starts at 50% and is not centered.

--------Top of Page--------    ------Middle of  Page------ {element}   ------Bottom of  Page------ 

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

When the top of the element is at the halfway mark, we can move the element back up by half of its own height to center it with the whole page. That's exactly what transform:translateY(-50%); does:

--------Top of Page--------    {element}-Middle of Page---    ------Bottom of  Page------ 

But why can't we just say top: 25% or something like that? I've made a quick snippet to show you the difference with that implementation:

body {    margin: 0;  }  .row {    display: flex;    justify-content: space-between;  }  .container {    display: inline-block;    margin: 5px;    width: 200px;    height: 200px;    background: tomato;  }  .inner {    position: relative;    margin: 0 auto;    height: 50%;    width: 50%;    background: #FFC4BA;  }  .inner.small {    width: 25%;    height: 25%;  }  .inner.big {    width: 75%;    height: 75%;  }  .percent {    top: 25%  }  .transform {    top: 50%;    transform: translateY(-50%);  }
<b>First row </b>looks alright, but that's because the gap works well with the 25%  <div class="row">    <div class="container">      <div class="inner percent"></div>    </div>    <div class="container">      <div class="inner transform"></div>    </div>  </div>  <b>Second row </b>made the center square a bit smaller, and the 25% now is too high as we'd expect the bottom of the element to reach 75%  <div class="row">    <div class="container">      <div class="small inner percent"></div>    </div>    <div class="container">      <div class="small inner transform"></div>    </div>  </div>  <b>Third row </b>now I've made the center box big and it ends lower than 75% making 25% start too late  <div class="row">    <div class="container">      <div class="big inner percent"></div>    </div>    <div class="container">      <div class="big inner transform"></div>    </div>  </div>
like image 88
Andrew Bone Avatar answered Oct 19 '22 18:10

Andrew Bone


While others have provided the answer that the -50 moves the inner element back up half it's own height, I thought this little animation showing the movement to top: 50%; first, followed by transform: translateY(-50%); second, might help.

@keyframes centerMe {    0% { top: 0%; transform: translateY(0%); }    50% { top: 50%; transform: translateY(0%); }    100% { top: 50%; transform: translateY(-50%); }  }    .outer {    position: relative;    border: solid 1px;    height: 200px;    width: 200px;  }    .inner {    position: relative;    background-color: red;    height: 50px; width: 50px;    margin: auto;    animation: centerMe 5s;    animation-fill-mode: forwards;  }    /* rules for example */  .hline,.vline{background:#000;position:absolute}.vline{height:100%;width:1px;left:calc(50% - .5px);top:0}.hline{width:100%;height:1px;top:calc(50% - .5px)}
<div class="outer">    <div class="hline"></div>    <div class="vline"></div>    <div class="inner"></div>    </div>
like image 40
JonSG Avatar answered Oct 19 '22 16:10

JonSG