Next.js Discord

Discord Forum

Dynamic Backgrounds in Next.js + Tailwind CSS Hero Section: Issue with Rendering from Constants

Answered
Mugger Crocodile posted this in #help-forum
Open in Discord
Mugger CrocodileOP
Hey everyone! 👋

I hope you're all doing well. I've been working on a project using Next.js and Tailwind CSS that involves having a HERO section on every page, each with a different background image. However, I'm encountering a problem with dynamic background rendering. Here's a breakdown:

### Tailwind Configuration:
// tailwind.config.js
module.exports = {
  // ... other configurations
  theme: {
    extend: {
      backgroundImage: {
        home: "url('/assets/images/bg/hero-bg.jpg')",
        store: "url('/assets/images/bg/store-bg.jpg')",
        about: "url('/assets/images/bg/about-bg.jpg')",
        contact: "url('/assets/images/bg/contact-bg.jpg')",
        cta: "url('/assets/images/bg/CTA-bg.jpg')",
      },
    },
  },
}


### Constants for About Page:
// constants.js
export const aboutHero = [
  { background: "bg-about", heading: "About Us" }
];


### Component Implementation:
// SomeComponent.js
import { aboutHero } from 'path/to/constants';
import Hero from 'path/to/Hero';

// ...

{aboutHero.map((about, index) => (
  <Hero key={index} image={about.background} heading={about.heading} />
))}


### Dynamic Hero Component:
// Hero.js
const Hero = ({ image, topText, heading, button }) => {
  return (
    <section
      className={`hero-radius min-h-[87vh] ${image} bg-cover bg-center`}
    >
      {/* Content goes here */}
    </section>
  );
};

export default Hero;


### Problem Statement:
The problem arises when dynamically passing the background to the Hero component; it doesn't seem to work as expected. However, hardcoding the background, such as "bg-home" or "bg-about," works perfectly fine.

Any insights on what might be causing this issue would be greatly appreciated! 🙏
Answered by Ray
try
content: [
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "constants.js"
  ]
View full answer

10 Replies

@Mugger Crocodile **Hey everyone! 👋** I hope you're all doing well. I've been working on a project using **Next.js** and **Tailwind CSS** that involves having a HERO section on every page, each with a different background image. However, I'm encountering a problem with dynamic background rendering. Here's a breakdown: ### **Tailwind Configuration:** javascript // tailwind.config.js module.exports = { // ... other configurations theme: { extend: { backgroundImage: { home: "url('/assets/images/bg/hero-bg.jpg')", store: "url('/assets/images/bg/store-bg.jpg')", about: "url('/assets/images/bg/about-bg.jpg')", contact: "url('/assets/images/bg/contact-bg.jpg')", cta: "url('/assets/images/bg/CTA-bg.jpg')", }, }, }, } ### **Constants for About Page:** javascript // constants.js export const aboutHero = [ { background: "bg-about", heading: "About Us" } ]; ### **Component Implementation:** javascript // SomeComponent.js import { aboutHero } from 'path/to/constants'; import Hero from 'path/to/Hero'; // ... {aboutHero.map((about, index) => ( <Hero key={index} image={about.background} heading={about.heading} /> ))} ### **Dynamic Hero Component:** javascript // Hero.js const Hero = ({ image, topText, heading, button }) => { return ( <section className={`hero-radius min-h-[87vh] ${image} bg-cover bg-center`} > {/* Content goes here */} </section> ); }; export default Hero; ### **Problem Statement:** The problem arises when dynamically passing the background to the Hero component; it doesn't seem to work as expected. However, hardcoding the background, such as "bg-home" or "bg-about," works perfectly fine. **Any insights on what might be causing this issue would be greatly appreciated!** 🙏
does constants.js include in the content array in tailwind.config.js?
@Ray does `constants.js` include in the `content` array in `tailwind.config.js`?
Mugger CrocodileOP
No, I haven't defined it there, and I think it is the problem.
I'm new to Next.js.
//tailwind.config.js
content: [
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
  ]

could you please tell me more about content?
@Ray what is the path of `constants.js`? you need to add it inside the content array
Mugger CrocodileOP
its in root directory
@Mugger Crocodile its in root directory
try
content: [
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "constants.js"
  ]
Answer
Mugger CrocodileOP
i've a question
what does the ./pages path do? i know about others as i can visually see those folders in root dir.
.pages/ are used for pages router
Content array in tailwind config are for all those files and folders where you want to apply your tailwind classes. If an file is made outside the scope of that array, any tailwind class you declare won't apply
Mugger CrocodileOP
Thank you, guys! That worked! What an active community.
really loved and means alot!