Should I use CSS or Javascript for this hover use case?

51 views Asked by At

I'm building a site where there is an image and icon within a container. I want to be able to hover over the container and the image would move up by 40px, icon would move right by 40px. Do I need to use Javascript in this case or is there a solution that can leverage only CSS?

Thanks in advance.

2

There are 2 answers

0
SebastianK On BEST ANSWER

No reason to get JavaScript involved - simply use the pseudo-selector :hover in CSS. Others have suggested various ways to do so in answers to this question.

If you want the image to be animated upwards, not just instantly move, you can use CSS animations as well.

0
Jack On

Reasons to use CSS over JS

1- CSS is easier than Javascript

2- JavaScript can be disabled

3- CSS is faster than JavaScript