DEV Community

Discussion on: CSS percentage unit, the evil parts

Collapse
 
ismail9k profile image
Abdelrahman Ismail

My pleasure 😄😄, I think you meant using .slider__next { top: calc(50% - 15px/2); }.

It will work here because you know the height of the element already; so you can simply subtract half of its hight from the top position.

But in other scenarios, the height of the element will be unknown for example a text paragraph, in most cases, you can't know exactly its height.(suppose it positioned absolute) the transform technique will be better here.

example

text paragraph example

There are cases, where the height of the element will change in mobile (using a media query), so you have to change it again by subtracting the new height.

There are many ways to center elements (flex-box, grid,... ), so use the way that suits your situation best.