Rotating an SVG element using CSS :hover leads to element being translated

12 Views Asked by At

Full code: https://codepen.io/fcc-danielw/pen/oNBLjrj

Relevant parts:

<svg width="235" height="381" viewBox="0 0 235 381" fill="none" xmlns="http://www.w3.org/2000/svg">
...
<g id="images">
          <g id="img1">
            <rect id="Rectangle 10" x="37" y="158" width="71" height="71" rx="3" fill="#C4C4C4" fill-opacity="0.25" />
            <mask id="mask0" mask-type="alpha" maskUnits="userSpaceOnUse" x="38" y="159" width="70" height="70">
              <rect id="Rectangle 9" x="38" y="159" width="70" height="70" rx="3" fill="#C4C4C4" />
            </mask>
            <g mask="url(#mask0)">
              <g id="mountains">
                <path id="Polygon 1" d="M58.5 187L89.2439 229.75H27.7561L58.5 187Z" fill="#C4C4C4" />
                <path id="Polygon 2" d="M94.5 187L125.244 229.75H63.7561L94.5 187Z" fill="#C4C4C4" />
              </g>
            </g>
          </g>
...
</g>
...
</svg>
#img1 { 
  transform: rotate(0deg);
  transition: 0.3s;
}
#img1:hover {
  transform: rotate(-17deg);
  transform-origin: center center;
}

What it looks like:

What I expected: it would rotate in place without moving around.

What actually happens: it not only rotates but also moves to a different spot on the svg plane. I assume it has to do with the origin, though I am not sure what else to set it to.

0

There are 0 best solutions below