8

I have created a one-page website using tailwindcss and React. In the prototype I use the tailwindcss class "scroll-smooth" and it works. In React the class "scroll-smooth" does not work, but what is the reason?

https://tailwindcss.com/docs/scroll-behavior#basic-usage

When I click "Why" on Navigation i jump to the section "why" but not smoothly:

function App() {
  return (
    <div className="App">
      <div className="relative">
        <div className="flex flex-col scroll-smooth">

          <HomeNav />

          <HomeHero />

          <section id="why" className="flex flex-col items-center px-6 pt-20">
            ...
          </section>
          <HomeFooter />
        </div>
      </div>
    </div>
  );
}

Solution:

I think TailwindCss Class "scroll-smooth" it doesn't work on react. So I use the NPM package "react-scroll" with which it works great and I probably have less compatibility worries.

https://www.npmjs.com/package/react-scroll

2
  • Which browser are you using? The CSS scroll-behavior (used by scroll-smooth) is not supported by all browsers. See: caniuse.com/?search=scroll-behavior Commented Feb 15, 2022 at 18:29
  • I use the latest Firefox and Chrome browser :) And in my clean Tailwind Prototype it works, but not in my React app. Maybe it's a React specific problem. I am new to React ;) Commented Feb 15, 2022 at 19:03

5 Answers 5

14

react-scroll working superb but We can still use scroll-behavior: smooth with react and tailwindcss. Here is my solution:


Folder & File structure:

enter image description here

App.js :

import "./App.css";
import AntyHero from "./components/AntyHero";
import Footer from "./components/Footer";
import Hero from "./components/Hero";
import Navbar from "./components/Navbar";

function App() {
  return (
    <>
      <section id="header">
        <Navbar />
      </section>
      <div className="flex flex-col h-screen items-center justify-center additional gap-3">
        <h1 className="text-5xl">TailwindCSS & React.js</h1>
        <h2 className="text-3xl pb-5">smooth scrolling behavior</h2>
        <div className="flex gap-5 items-center justify-center text-2xl underline bg-white rounded-md p-2">
          <a href="#one" className="text-orange-600">
            Section One
          </a>
          <a href="#two" className="text-red-600">
            Section Two
          </a>
          <a href="#three" className="text-green-700">
            Section Three
          </a>
        </div>
      </div>
      <div className="text-center text-3xl">
        <section id="one" className="h-screen bg-orange-600">
          Section One
        </section>
        <AntyHero />
        <section id="two" className="h-screen bg-red-600">
          Section Two
        </section>
        <Hero />
        <section id="three" className="h-screen bg-green-700">
          Section Three
        </section>
      </div>
      <Footer />
    </>
  );
}

export default App;

index.css :

@tailwind base;
html {
  scroll-behavior: smooth;
}

@tailwind components;
@tailwind utilities;

Output:

enter image description here

Tested with:"tailwindcss": "^3.0.11", "react": "^17.0.2" Firefox and Chrome

Sign up to request clarification or add additional context in comments.

3 Comments

Yes you are right when i add it to base layer than works ;) But for now, I will use my solution via the react-scroll package. Thanks!
@GregorWedlich You're welcome ! React-scroll is marvelous ;-) Best regards and Thank You...
Thank you big time! You just saved me from having to install a bunch of packages ;0
5

Add scroll-behavior: smooth to the code works for me.

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  html {
    scroll-behavior: smooth;
  }
}

Comments

2

Adding "scroll-smooth" to the html tag worked for me. I am using react with next.js.

return (
    <html lang="en" className="h-full scroll-smooth">
      ...body
    </html>
  );

Comments

0

You need add scroll-behavior: smooth in the css file or in the html.

Solution 1 - CSS :

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  html {
    scroll-behavior: smooth;
  }
}

Solution 2 - HTML :

<html lang="fr" className="scroll-smooth">
</html>

Comments

0

Just give this value (scroll-smooth) to the body tag to be applied.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.