This content originally appeared on DEV Community and was authored by ri ki
Today’s Work: Fixing Scroll Animation Hover and Hydration Mismatch Error
I spent today fixing the scroll animation so that it properly pauses on hover. During this process, I encountered a common Next.js issue called the "hydration mismatch" error.
What Causes Hydration Mismatch?
This error happens when the HTML rendered on the server does not match what React renders on the client during hydration. In my case, the root cause was:
- Embedding dynamic values like
imageWidth
andimages.length
(which can differ between server and client) directly into CSS styles. - Using
<style jsx global>
with calculated pixel values in@keyframes
animations.
Key Points and Solutions
Use percentage values (%) instead of dynamic pixel (
px
) values inside@keyframes
.
For example, instead oftranslateX(-${imageWidth * images.length}px)
, usetranslateX(-100%)
.Control animation duration and other dynamic properties via inline styles rather than global CSS to avoid discrepancies between server and client.
The
transform: translateX(-50%)
means the images scroll by half their total width because the images are duplicated to create an infinite scrolling effect.Pause on hover by toggling a CSS class that changes
animation-play-state
. However, if the hover state differs between server and client renders, it will cause hydration issues.Since I wasn’t using
useState
oruseEffect
, some client-side value changes weren’t syncing properly.
Proposed Approach
- Keep the dynamic parts of the animation in inline styles that run only on the client.
- Use fixed CSS animations with percentages in global styles.
- Set the container widths dynamically to match the parent element.
- Avoid embedding dynamic pixel values inside
@keyframes
.
Deployment Challenges
After applying these fixes, I deployed the site to Netlify to verify behavior in production. However, the root folder was still set to an old LP project I had worked on before, so Netlify wasn’t serving this project correctly.
To fix this, I deleted the old project folder locally and removed the repository on GitHub to reset it properly.
Next Steps
Tomorrow, I plan to redeploy to Netlify with the correct folder and check the page speed and animation behavior in the live environment.
Fixing hydration mismatches in Next.js animations is tricky, but taking care to separate dynamic client-only values from static CSS is the key to a smooth user experience.
tags: nextjs, react, animation, performance, hydration, portfolio
This content originally appeared on DEV Community and was authored by ri ki

ri ki | Sciencx (2025-07-13T01:00:22+00:00) Umemura Farm Website – Devlog #34: Fixing Scroll Animation and Hydration Mismatch in Next.js. Retrieved from https://www.scien.cx/2025/07/13/umemura-farm-website-devlog-34-fixing-scroll-animation-and-hydration-mismatch-in-next-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.