I'm not sure how to use the global .container CSS style in Next.js for multiple pages


I'm currently diving into the world of React and Next.js, tackling a project with Next.js. I've set up a global CSS file with a .container style definition which I imported in my _app.js file. However, I'm unsure how to apply this global style to a specific page. In one of my pages, I have a module.css file that I understand how to work with, but there's a div element with the global container class that I can't seem to style properly within that page. Can I define the .container property in the global CSS file or does it only affect global tags like a, img, h1, etc? I'm feeling a bit overwhelmed by the CSS aspect of Next.js.

After figuring out how to pass global CSS to a page that uses module.css, I now find myself stuck with the module CSS.

I've created a component called HeroSection like this :

import React from "react";
import style from "./heroSection.module.css";

function HeroSection({ title, paragraphs, image }) {
  return (
    <div className={style.heroSection}>
      <div className={style.text}>
        <h1>{title}</h1>
        <div className={style.paragraphes}>{paragraphs}</div>
      </div>
      <img src={image} alt="" />
    </div>
  );
}

export default HeroSection;

And on a page that contains the component and adds props:

            <HeroSection
              title="Contact Us"
              paragraphs={
                <>
                  <div className="para1">
                    <p> Let's improve this societal project together!</p>
                    <p>
                      We value your suggestions, opinions, and comments.
                    </p>
                  </div>
                  <p>
                    Right here
                  </p>
                </>
              }
              image="/images/contact.jpg"
            />

In my heroSection.module.css file, I have the following code:

.para1 {
  margin-bottom: 44px;
}

As you can see, in the page containing HeroSection, a div has the className "para1" but I'm having trouble applying the CSS styles to it. I tried using className={style.para1} without success. How can I style the div passed as props?

Answer №1

Make sure to include the CSS file on the page where you are using the <HeroSection> component.

import style from "<relative-path-to-heroSection.module.css>";

After that, assign the correct class to the <div> element below.

<HeroSection
    title="Contact Us"
    paragraphs={
        <>
            <div className={style.para1}>
                <p> Let's improve this project together!</p>
                <p>
                    We value your suggestions, opinions, and feedback.
                </p>
            </div>
            <p>It's right here</p>
        </>
    }
    image="/images/contact.jpg"
/>

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

Having issues with customizing the design of MaterialUI Accordion header

Link to my code sandbox project I am encountering issues with the styling in my Accordion Heading. My Accordion Contents are set up like this: <Accordion heading1= { <DishItem name="Döner Teller Spezial" price="13,60€"/> ...

Creating a step wizard form with ReactJs can be accomplished by breaking down the form into

I have developed a resume generation application with two main components: BasicDetails and EmploymentDetails. To see a working example, click here: https://codesandbox.io/s/next-dynamic-testing-issue-forked-h1nt8 index.js file: <form onSubmit={ha ...

`Unable to activate label function in HTML`

As I dabble around with HTML and CSS, I've been experimenting with creating a custom file selection button. I came across a method on the internet that involves hiding the original button and superimposing a new one over it using the following code: ...

Attempting to minimize the repetition of code in Redux by implementing some utility functions

Is there a potential issue with the method I'm attempting in this URL: The concept involves altering only the actions file when introducing a new action. One improvement I am considering is recursively merging the status passed from the actions with ...

Implementing dynamic value changes in MUI Select using Formik's onChange event

Integrating the Formik with the Select component of MUI presented me with a few obstacles. Initially, it failed to update the value on the onChange event and did not display any error messages when nothing was selected. After some troubleshooting, I was ...

nextjs dynamic routing is not compatible with the next-i18next framework

After integrating next-i18next into my Next.js project using the official guide, everything appeared to be functioning correctly. However, I encountered a 404 error when switching from the default language (Italian) to English and navigating to the detail ...

An error occurred while trying to load the configuration "next/core-web-vitals" for extension

If you're embarking on a new project using NextJs and TypeScript, chances are you may encounter the following error: Failed to load config "next/core-web-vitals" to extend from. Wondering how to resolve this issue? ...

Creating Flexible Designs - Implementing a responsive sidebar with dynamic scrolling

I am in the process of developing a simple web application that features a sidebar whose vertical size is dependent on the number of elements it contains. My goal is to make the web app responsive, ensuring it works well on devices of any size. In cases ...

Tips for dynamically updating the id value while iterating through a class

Here's the HTML snippet I am working with: <div class="info"> <div class="form-group"> <label class="control-label col-sm-2">Title</label> <div class="col-xs-10 col-sm-8"> <inpu ...

Tips for using jQuery to dynamically apply CSS styles to new content added to a webpage

I have encountered this question multiple times on stackoverflow and through google search, but unfortunately none of the suggested solutions have worked for me. My issue involves a page that loads an HTML page with empty sections, which are then dynamica ...

Error message "npm: not found" was encountered while attempting to generate a React app using tools like NVM and NPM within

I'm currently working on setting up a new React project in pycharm. Utilizing version 0.34.0 of NVM, I have successfully installed node. Before initiating the project, here is my pycharm window: https://i.stack.imgur.com/Opt7z.png The command npm - ...

Dealing with errors from APIs in a React TypeScript application

Currently, I am in the process of learning React and Typescript by creating a demo application. This app sends a request to the API located at in order to retrieve postcode information and display details about a specific location based on the entered pos ...

Is there a way to retrieve match.params from within a Redux Thunk action?

Is there a way to achieve the functionality shown below, without manually passing data into the function from a component? function doSomething() return (dispatch, getState) => { console.log(getState().router.match.params) } } This is i ...

Tips for creating a sticky bootstrap column that remains in place as you scroll

I am looking to design a FAQ page similar to Twitter's FAQ. The goal is to have the left column remain fixed in its position while allowing users to scroll through the content. Here is what I have attempted so far, but it is not functioning as expect ...

Guide on implementing hover effects in React components

Within my component SecondTest, I have defined an object called useStyle which sets the background color to red. Is it feasible to add a hover effect to this object? const useStyle = { backgroundColor: "red", }; function SecondTest() { return < ...

Leverage the functionality of both Flask/Python API routing and NodeJS API routing by incorporating a pages router

I have integrated a Python Flask API backend with my Next app as per the instructions in this guide. Currently, I am utilizing Next 12.3.4 (pages router). The structure of my API directory is as follows: pages --|api ----|__pycache__ ----|index.py In my ...

Setting today's date as the default option for the Material UI datepicker

I'm having trouble setting today's date as the default in my React app using Material UI datepicker. Here is the snippet of my code: <TextField id="dateTimeFrom" type=" ...

Struggling with lag in MaterialUI TextFields? Discover tips for boosting performance with forms containing numerous inputs

Having some trouble with Textfield in my MateriaUI project. The form I created has multiple inputs and it's a bit slow when typing or deleting values within the fields. Interestingly, there is no lag in components with fewer inputs. UPDATE: It appear ...

Having trouble loading image on web application

Currently, I am facing an issue while attempting to add a background image to an element within my Angular web application. Strangely enough, the relative path leading to the image seems to be causing my entire app to break. https://i.stack.imgur.com/b9qJ ...

Linking to an element within a component on the same page using Next.js

Currently, I am in the process of developing my very first Next.js project which consists of a single-page website. I have successfully created the navigation component, and now my focus is on managing links. I would like to know how I can incorporate the ...