Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
How to dynamically set a backGroundColor to a card depending on its category with Nextjs

I have a nextjs app where I want to give different backGroundColor to cards relatively to their category.

I tried this :

card.js

import { React, useEffect } from "react"
import Link from "next/link"
import NextImage from "./image"

const animColor = "#FF006E";
const decorColor = "#FFBE0B";
const illuColor = "#3A86FF";

const Card = ({ article, categories }) => {

  useEffect(() => {
    const overlay = document.getElementById("overlay");

    article.attributes.categories.data.forEach(category => {
        console.log(category.attributes.slug)
        if (category.attributes.slug === "animation") {
          overlay.style.backgroundColor = animColor;
        }
        if (category.attributes.slug === "decor") {
          overlay.style.backgroundColor = decorColor;
        }
        if (category.attributes.slug === "illustrations") {
          overlay.style.backgroundColor = illuColor;

        }

    });

  }, []);

  return (
    <Link href={`/article/${article.attributes.slug}`}>
      <a className="hover:no-underline">
        <div className="card relative grid mb-3 md:mb-0">
          <div className="media">
            <NextImage image={article.attributes.image} />
          </div>
          <div id="overlay" className="overlay-color"></div>
          <div className="card-body flex items-end justify-center pb-5">
            <p className="text-xl px-2 uppercase text-center text-white">
              {article.attributes.title}
            </p>
          </div>
        </div>
      </a>
    </Link>
  )
}

export default Card

articles.js

import React from "react"
import Card from "./card"

const Articles = ({ articles, categories}) => {
 
  return (
    <>
      <div className="md:grid grid-cols-3 gap-5 justify-center place-content-center">
            {articles.map((article, i) => {
              return (
                <Card
                  article={article}
                  categories={categories}
                  key={article.attributes.slug}
                />
              )
            })}
          </div>
    </>
  )
}

export default Articles

I might complicate the task more than it is really, but I'm kind of lost in my head. I'm try to catch the category of each card and if category.slug === [each categories] then It change backGroundColor style.

With this code it changes only the color of the first card. I guess there is an error in the iteration process I chose.

Any idea to solve it and maybe do it better ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!