What is the Best Way to Utilize CSS ID Selectors with Next JS?

After discovering that npx create-react-app is no longer supported, I decided to delve into Next JS as a new framework. My goal now is to transition one of my websites from pure HTML, SASS (CSS), and JavaScript to utilizing Next JS and SASS.

The current component I am focusing on is the navbar, which has various SASS/CSS class names and IDs applied to it:

import React from 'react'
import navbar from "./navbar.module.scss"

const Navbar = () => {
    return (
        <div>
            <header className={navbar.nav} id={"horizontal"}>
                <div className={navbar.nav_name}><a href="./index.html">Ryan Barillos</a></div>
                <nav className={navbar.nav_links} id="vertical">
                <a href="./pages/about.html" className={navbar.nav_btn} id="active">About Me</a>
                <a href="./pages/projects.html" className={navbar.nav_btn}>Projects</a>
                <a href="./pages/music.html" className={navbar.nav_btn}>Music</a>
                <a href="./pages/contact.html" className={navbar.nav_btn}>Contact</a>
            </nav>
            <div className={navbar.btn_menu} id="open"></div>
        </header>
        
    </div >
    )
}
export default Navbar

If you are interested in the global settings for this project, please check out global.scss on my Github repository.


Although my journey with Next JS is still in its early stages, I am facing challenges when it comes to applying style IDs to my components. Despite the extensive documentation on using classNames, there is a lack of information on how to integrate style IDs effectively within Next JS projects, leaving me puzzled about making my SASS/CSS styling function correctly.


I came across a related question posted years ago, but unfortunately, the answer provided was too advanced for beginners like myself, offering minimal guidance or code demonstration. This leaves me with uncertainties on where to seek assistance and how to implement CSS ID styles on my Next/React components besides relying solely on class names.

Hence, I'm seeking advice on how to incorporate CSS ID styles into my Next/React components effectively, apart from using class names exclusively. Any insights or tips would be greatly appreciated!

Answer №1

Approach 1

To apply styling, utilize CSS classes.

<header className={`${navbar.nav} ${navbar.horizontal}`}>
...
.nav {
...
    /*
    Horizontal Menu - Customizations
    */
    &.horizontal {
      background: red;
      // Position
      z-index: 999;
      left: 0;
    
      // Size adjustments
      height: global.$height_navbar;
    
      // Display configurations
      align-items: flex-end;
      * {
          color: blue;
      }
    }
...
}
...

Approach 2

Utilize ids for customization.

& is limited to the same level elements.

For other modifications, create separate customizations.

<header className={navbar.nav} id={navbar.horizontal}>
...
.nav {
...
    /*
    Horizontal Menu - Customizations
    */
    &#horizontal {
      background: red;
      // Position
      z-index: 999;
      left: 0;
    
      // Size adjustments
      height: global.$height_navbar;
    
      // Display configurations
      align-items: flex-end;
      * {
          color: blue;
      }
    }
...
}
...

Outcome

https://example.com/some-image-link.png

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 react-apollo mutation component is causing the transmission of empty strings as null values

Let's simplify the issue with an example. I have a table component displaying queried data, along with a form for users to input data into the table. The problem I'm facing is that the default values of the input fields are empty strings. Exampl ...

When the screen size decreases, HTML images do not adjust accordingly

Hey there, I'm just diving into the HTML and CSS world with FreeCodeCamp's curriculum. The second project involves creating a survey/form, which I've managed to put together quite nicely--except for one small issue with the images. While the ...

What methods does ReactJS employ to manage memory in applications with an abundance of components?

I am in the process of transforming a classic web application into ReactJS. I have implemented react-router to dynamically load various components, including child components, based on the current route. The app is expected to have a plethora of routes, an ...

Adjusting the OrbitControl target in a React environment

I attempted to create a model using react-three-fiber and react-drei, but the OrbitControl target setting is causing it to appear too high. import React, { useRef, useState, Suspense, useEffect } from 'react' import { Canvas, useFrame, useLoader, ...

Auth.js and Next.js, route error with callback handling

When deploying the app on a server using auth.js with credentials provider, an error is encountered. Interestingly, there are no errors when running the app on localhost. The login page structure can be seen below: 'use client' import { Label } ...

What sets React component instances apart from those in other frameworks?

According to the documentation, React emphasizes that it doesn't focus on instances because Components take props as input and generate elements as output. The comparison with other frameworks illustrates how React simplifies the process by eliminatin ...

Next.js is like Gatsby but with the power of GraphQL

I'm curious if it's possible to set up GraphQL in Next.js similar to how it's done in Gatsby, allowing me to query pages and retrieve data from them. Are there any plugins available for Next.js that work like Gatsby-file-source and gatsby-ma ...

Setting the flex-direction for <td> elements: A helpful guide

This is a snippet from the render function of one of my components, showcasing a table: return ( ... <td> <div style={{ width: '100%' }}>50</div> {' '} <span clas ...

A beginner's guide to implementing Auth0 routing with the Next.js App Router

Is it possible to utilize Auth0 routes on the new App Router instead of the traditional Pages Router setup kit provided by Auth0? Learn more about App Router (Next.js) here: https://nextjs.org/docs/app Check out how to add the dynamic API route with Auth0 ...

yarn.lock file contains a vulnerability related to serialize-javascript

After creating a React project with the npx create-react-app my-app command and testing it using npm start, I encountered a security advisory on the Github page when committing the project. A vulnerability in serialize-javascript was found in yarn.lock * ...

What is the method to extract information from the provided URL?

Hello, I am looking to retrieve system requirements data from . How can I achieve this using JS or react? ...

Stop the Bootstrap row from automatically adjusting to the height of the tallest child column element, and instead utilize the extra space below the smaller element

My current framework is Bootstrap 5.2. I am aiming to create two columns, each with a col-md-6 class inside a row. You can visualize the layout through this image: . The challenge I'm facing is the inability to add another row with col-md-6 element ...

The initial release of the NPM package results in a blank object being returned

After attempting to publish a simple react component, I encountered an issue where it returns an empty object when imported? The package in question is called react-selekt and can be found at https://www.npmjs.com/package/react-selekt You can also check ...

What is the best way to initiate an AJAX call once the setState() function has updated the state

I am in the process of developing a search page component that includes multiple filter inputs to refine search results. Every input modifies the component's state and should initiate an AJAX call to update the search results: handleSearchStringChan ...

Injecting a variety of wallpapers dynamically using css and html

As a beginner in CSS, HTML, and coding in general, I am currently working on creating my own start page with a local path that loads my bookmarks using an HTML file. The background-image control is handled by the style.css file. I have researched ways to c ...

When the browser is resized, the menus will automatically drop down

I am experiencing an issue with my horizontal menu bar code. The menu bar displays correctly for the resolution 1366 x 768, but when I press the ctrl - shortcut key and reduce the browser size using the same shortcut key, my menus move down. I have tried ...

What are the distinguishing factors between using redux saga and redux thunk?

How do you know when to use redux-saga versus redux-thunk? Which one would be more suitable for an ecommerce platform? Is there a noticeable difference in performance between the two? Are thunks faster than sagas? ...

"Developing a stand-alone ASP.NET Core WebAPI with back-end authentication, and creating a React Redux application with front-end authentication that operate autonom

Looking to develop an ASP.NET Core Web API with authentication using ApplicationDbContext as the default database context. I prefer working with Visual Studio for the backend development. 2. Interested in building a React-Redux app with authentication for ...

Styling the resizable textarea background color in Firefox

I am seeking to change the background color of multiple textareas on my website from the default white. textarea { background-color:Red; width:120px; height:80px; } <textarea>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ...

Activate the 'li' element within the 'ul' whenever a 'ui-sref' link on the

I have been working on building a dashboard that consists of an abstract state called dashboard, with subdashboard as the immediate child state. https://i.sstatic.net/YTHky.png Previously, clicking on user details would take the user to the dashboard.tab ...