"Utilizing multiple class names in Next.js to enhance website styling

Looking for a way to apply multiple classNames in Next.js, especially when dealing with variable classnames?
I'm following the component level CSS approach.

Take a look at my code and what I aim to achieve:

import styles from "./ColorGroup.module.css";

const colors = ["red", "sky", "yellow", "green", "golden"];

const ColorGroup = () => {
  return (
    <div className={styles.colorContainer}>
      <text>Colour</text>
      <div className={styles.colorBoxContainer}>
        {colors.map((color, index) => (
          <div
            // The issue lies in applying both variable color classes and colorBox class. How can we do this?
            className={`${styles}.${color} ${styles.colorBox}`}
            key={index}
          ></div>
        ))}
      </div>
    </div>
  );
};

Achievement:

https://i.stack.imgur.com/kprnr.png

CSS snippet:


/* Code responsible for layout and arrangement as shown above */

.colorBox {
  height: 36px;
  width: 36px;
}

.red {
  background-color: lightcoral;
}
.sky {
  background-color: skyblue;
}
.yellow {
  background-color: lightyellow;
}
.green {
  background-color: lightgreen;
}
.golden {
  background-color: greenyellow;
}

However, the current method only applies the colorBox className while ignoring ${styles}.${color}. Any suggestions on how to apply both successfully?

Answer №1

It is recommended to utilize brackets for this scenario

<div className={styles.container} >
    {items.map((item, number) => (
        <div
            className={`${styles[item]} ${styles.box}`}
            key={number}
        ></div>
    ))}
</div>

Answer №2

If you want to add some style to your elements, consider using the style object and incorporating variables for easier customization:

<div key={index} className={`${styles.colorBox}`} style={{ backgroundColor: color }} >
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The error message 'Cannot read property 'navigate' of undefined' is displayed because the function '_this.props.navigation.navigate' is not

I'm facing an issue trying to access 'Home' from Form.js. Despite my efforts in arrowF, routes, and other methods, the same error persists. How can I resolve this? In my Form.js file, when I call onPress=(), I have tried using functions, ...

Creating dual modes (night/day) for your AngularJS application

Currently, I am in the process of developing a project with Angularjs and facing an obstacle with animations. My goal is to implement a feature in my application that allows it to display in two different modes - night mode and day mode. I have come ac ...

"Adjusting the size of a jQuery dialog for optimal viewing on

I am currently working on designing an HTML page that is responsive for both mobile and desktop browsers. One issue I am encountering involves a jQuery dialog that I need to scale properly to fit the page. I believe the problem lies within these lines of ...

When I try to access the divs on my NextJS website by scrolling, they seem to vanish as soon as I scroll past them. What could be causing this

Currently utilizing NextJS to develop a website showcasing user information within cards. These cards contain additional information such as bios and answers to questions. An issue arises when scrolling through the profile card, where other items are not ...

Changing the size of a Material UI card component by clicking and dragging one of its corners

My project involves a collection of Material UI cards organized in a React Beautiful DND list. I have successfully implemented the functionality to move cards from one list to another and reorganize them. Now, I am looking for a way to enable users to resi ...

What is the best way to display an input label or placeholder label consistently?

I am currently utilizing the multi-select feature with checkboxes provided by Material UI v4. By default, the settings display an array of 'SELECTED' values using the renderValue={selected => selected.join(', ') function. However, I ...

Handling scroll events in a functional component using React

I'm having trouble understanding why the onScroll function isn't triggering the console.log statement. <table className="table" onScroll={()=>{console.log("Works")}> The console.log just doesn't seem to be exec ...

When you hover over an image, its opacity will change and text will overlay

I am looking for a way to decrease the opacity and overlay text on a thumbnail image when it is hovered over. I have considered a few methods, but I am concerned that they may not be efficient or elegant. Creating a duplicated image in Photoshop with the ...

Developing with Express and React

Currently, I am running experiments with React and Express. My primary objective is to establish a connection between Express and a MySQL database to retrieve data. I have a good understanding of how both React and Express operate, and after several attem ...

Implementing Pagination in React using Material UI Framework

I am trying to implement pagination based on the length of an array, which is currently 2. Therefore, I need a total of 2 pages to display each array item per page. Can someone help me achieve this? View my solution on CodeSandbox Desired output: Page 1 ...

Adding a background image to a box in MUI is a simple task that can enhance

I've been attempting to include a background image in the Box component of mui, but I can't seem to get it to work. Here is the code I've been using: const Main = () => { return ( <Box sx={{backgroundImage:'images/cove ...

Animate the jQuery display property to show a table without changing any specified spatial dimensions

When utilizing jQuery's $.animate() on an element styled with display:table, any spatial dimensions that are not explicitly specified to change will animate. Check out the fiddle here In the scenario presented, the width is defined for animation, bu ...

Place an image at the top of the canvas at a specific location

Currently, I am in the process of reconstructing this specific website My approach involves working with React (similar to the aforementioned site) and utilizing the same cropper tool that they have implemented. For cropping, I am incorporating react-imag ...

Expand the div to fit one side of the browser

My bootstrap code looks like this: <div class="container"> <div class="row"> <div class="col-md-8">content</div> <div class="col-md-4"> <div class="map">map goes here</div> </div> </div& ...

Is there a way to adjust the label elevation for a Material UI TextField component?

I am trying to enhance the appearance of a textfield label, but I am facing some challenges with my code. textStyle: { backgroundColor: '#1c1a1a', border: 0, borderRadius: 0, width: 400, height: 66, display: 'flex&a ...

Best Practices for Implementing Periodic Background Sync in Next.js PWAs

I am in the process of creating a Next.js progressive web app (PWA) and I'm exploring the implementation of a periodic background sync. To set up this periodic background sync, I need to utilize a registration code similar to the following: // Check i ...

What is causing Next.js link to prefetch on hover every time, and how can I turn this feature off?

It appears that I am currently using the most recent version of Nextjs, which is 13.2.4v, and I am not utilizing the app or src directory. The issue arises when hovering over the Link Next component, as it triggers a server request leading to decreased per ...

Rendering React on the server side while making requests to the backend

I am currently working on a React application with server-side rendering using Express. In my implementation, I have a straightforward App component: import React, { Component } from 'react'; export default class App extends Component { const ...

Customize your Material UI theme colors with SASS variables

I am exploring the idea of customizing the theme of Material UI in a dynamic way by incorporating SASS Variables that would update the Material UI theme automatically. When designing my page with Sass, I typically use variables to define colors: $primary ...

Troubleshooting tips for resolving custom domain image display issues

Images on my website display correctly when accessed via localhost or the default domain, but do not appear on the custom domain. Default domain: Custom domains: & What could be causing this issue? I have followed all the instructions in the docum ...