… moves the element down a distance equal to half the height of the parent.
Since the default position puts the top of the inner element at the top of the outer element, this puts the top of the inner element at the middle of the outer element.
transform: translateY(-50%);
This moves the inner element back up a distance of half the height of the inner element.
Combining them puts the middle of the inner element at the middle of the parent element.
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:
<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>
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.
Rather than answering this question directly, I'm going to answer the more general question of:
How does position anchoring work in CSS?
Hopefully, in answering the question generally, you'll understand the parts that apply to your specific case.
What do you mean by "position anchoring"?
Position anchoring is when a DOM node is positioned in a way that it is "anchored" to its parent in a given dimension. If the top left of the node is anchored to the top left of its parent, the nodes will stay aligned at their top left corner, no matter the size of either element.
What does position anchoring look like?
I'm going to use a template for all further examples, so it's important to understand the base example.
This example shows a parent .container which has dark red, dark yellow, dark green, and dark blue quadrants to easily view alignment. Inside, it contains a .box which has red, yellow, green, and blue quadrants to show contrast for the alignment.
All further examples will have this boilerplate minified to make the relevant code stand out more.
Note that by default, the top left of the child is anchored to the top left of the parent.
Parent Anchoring
Parent anchoring can be adjusted by using the top, bottom, left, and right properties on the child element.
Top
Using the top property will anchor the top edge of the child to the top edge of the parent.
Assuming bottom isn't set, top: 0 won't show differently than the default of top: auto
right: 100% leaves teh child hanging off the left side of the parent.
Child Anchoring
Child anchoring can be adjusted independently from parent anchoring by making use of the transform property. Specifically the translate, translateX, and translateY functions will be used to bump the child box to use a different alignment.
The reason this works is because percentages in the translate value are relative to the child, while percentages in the top, bottom, left, and right properties are relative to the parent.
Vertical Alignment
Using transform: translateY(), the alignment of the child can be bumped up or down.
transform: translateY(0) will leave the child where it is, and is generally not very useful.
When the child is anchored to the top of the parent, transform: translateY(-50%) will align the child at its center: