Utilizing a CSS file from another page in React

I am currently facing an issue where my CSS imports are appearing on all pages instead of just one. I have them imported in _document.js

import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
  return (
    <Html>
      <Head>
        <link rel="stylesheet" href="/styles/index.css" />
        <link rel="stylesheet" href="index.css"/>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/fontawesome.min.css" integrity="sha512-xX2rYBFJSj86W54Fyv1de80DWBq7zYLn2z0I9bIhQG+rxIF6XVJUpdGnsNHWRa6AvP89vtFupEPDP8eZAtu9qA==" crossOrigin="anonymous" referrerPolicy="no-referrer" />
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  )
}

My goal is to import the CSS files on a per-page basis rather than having all css files on every page. I've attempted importing the css inside each JS file, but it always tries to locate /pagehere/styles/settings.css instead of just /styles/settings.css even when using import '../styles/settings.css'. Any help would be appreciated. Thank you.

Answer №1

Instead of using the <Link> elements, consider replacing them with

import '../../stryles/settings.css'
.

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

Another CSS style is creating additional gaps within the div container

The code I'm working with currently looks like this: <div id="img_preview_text_container" style="width: 376px; height: 75px; top: 156px; left: 50px; color: rgb(255, 0, 0);"> <div id="customOnePreviewText" style="font-family: Oklahoma; ...

Expand <a> to occupy the <li> element within the menu

Encountering a bit of a challenge... Is there a way to expand my <a> elements to completely fill the <li> containers they are placed in? I'm aiming to enhance the accessibility of the links for easier clicking. Check out my jsFiddle: htt ...

Knitting: exporting to HTML file while retaining formatting

Recently discovered the amazing knitr library in R, which looks great when viewed in the viewer. But unfortunately, the style gets lost when saved to an html file. Code library(knitr) library(kableExtra) some.table <- data.frame ( x = rep(1 ...

The search functionality for the MongoDB module is not functioning properly within my Next.js application

Working on my nextjs application, I have encountered an issue with using the mongodb node module to find all documents in one of my collections. Despite successful usage of .findOne and .updateOne for other pages like login and password reset, when I use . ...

Update the React component without needing to reload the entire webpage

Every time I attempt to render a new component, the entire page refreshes instead of just the components re-rendering. Upon loading the page, when I click the drop down menu in the header, it successfully renders a new component without any refreshing. Ho ...

Activate on-demand static regeneration with Next.js

I am thoroughly impressed by the functionality of Incremental Static Regeneration in Next.js. However, I am currently seeking a method to manually trigger static page regeneration as needed. It would be ideal to have a command that can be executed via an ...

Building a Tailored WebSocket Feature within Redux Framework

As I delve deeper into this topic, it feels like falling down a rabbit hole. This Trading application is quite unique as it receives realtime data via web sockets based on a request-response paradigm. There are three separate SPA's where each user act ...

Error: Path parameters are not accessible

Here's the setup I have: <BrowserRouter> <Sidebar> <Switch> <Route path='room/:roomId' component={RoomComponent}/> </Switch> </BrowserRouter> When I console.log(this.props) inside the roo ...

Is it possible for the recoil atom's default value to be a complex object containing multiple nested objects?

Is it possible to define a Recoil atom with a default value that is an object of objects? Look at the code below for an example: export const formData = atom({ key: "formData", default: { user: { firstName: "", l ...

Flexbox: The final element is not positioned on the right side

I have been trying to align the final item on the right side using margin-left: auto. The desired output is achieved in jsfiddle. I am using Visual Studio Code with the same code, but when viewed in Firefox, Chrome, or Edge, the item does not appear on the ...

Implementing Material-UI Autocomplete: How to Include a Starting Value for the startAdornment

I am using autocomplete with multiple selection permission. https://codesandbox.io/s/bold-jackson-dkjmb?file=/src/App.js In the provided example, there are 3 options for cities. How can I manually insert a value in TextField that is automatically selected ...

When implementing Next.js layouts, the content will be displayed below the sidebar

I'm currently working on implementing a sidebar component for my app using mantine.dev, along with next.js layouts to ensure it is displayed on every page. The links within the sidebar are functioning properly, however, I am facing an issue where the ...

Issue with ReactJS: Unable to display icons using '@material-ui/icons/Fastfood'

Issue with using the '@material-ui/icons/Fastfood' in ReactJS. Header.js import React, { Component } from 'react'; import './Header.css'; import Fastfood from '@material-ui/icons/Fastfood'; class Header extends Co ...

Make sure the inputs in separate table data cells are lined up in

I need help aligning two input fields in separate td elements to be on the same line. The issue I am encountering is that when an input is added to a td, it covers up any text within the td. https://i.stack.imgur.com/c7GiQ.png There are two scenarios: I ...

Choosing between JavaScript and Handlebars.js depends on the specific needs

Can anyone explain the advantages of utilizing Handlebars.js or similar libraries over solely relying on JavaScript? Thus far, I haven't come across any functionality in Handlebars that cannot be achieved with just pure JavaScript. ...

Is Bootstrap failing to function properly in my Angular project?

During my work on an angular project, I decided to implement bootstrap CSS and js for styling. However, after completing the project, I noticed that the bootstrap was not functioning correctly. I found that when I used the CDN link in the project, the bo ...

Using REST APIs in React programming

While working on an existing code base for a React application, I found myself in a situation where the original developer had left and I am still quite new to React. In the current code base, the library "request-promise-native" is being passed from one c ...

ReactJs CSS - This file type requires a specific loader for processing. There are currently no loaders configured to handle this file

I've noticed that this issue has been raised multiple times before. Despite going through all the questions, I still can't seem to resolve it. The transition from Typescript to Javascript went smoothly until I reached the implementation of CSS. U ...

What is the best way to minify and concatenate multiple CSS files only after converting SCSS to CSS with Gulp?

When attempting to convert my scss files to css files using gulp, I encountered an issue. After writing the scss code, it is easily converted to css. However, I wanted to minify this css and save it in a different folder called 'min'. Within the ...

The CSS theme toggler for Bootstrap

I am currently working on integrating a style switcher following the instructions provided at . However, when I add a title="" attribute to the CSS link, the CSS file fails to load on the page and the styles revert back to default Bootstrap. I have added ...