Design comparison
Solution retrospective
Question: is there a better way of positioning an icon over an image than calculating top/bottom and left/right percentages? Like, I'm sure there is, but it seems that I didn't learn about it yet.
Community feedback
- @SinisaVukmirovicPosted almost 2 years ago
Hello!
Yes! Yes there is a better and a correct way to position icon to always be in 50%.
First, you get the icon at 50% with
.icon { left: 50% }
...and since the coordinate of any element start at 0, 0 which is at top - left corner, you need to shift the icon itself by 50% of itself dimensions, with
.icon { left: 50%; transform: translateX(-50%); }
Ofc, values for the Y axis need to be added, in the same way as for X axis.
Hope I made this understandable.
If not, check out this article on translate property
Good luck in fixing your code!
Marked as helpful0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord