A guide on linking a CSS file to a component in Next.js

I recently started working on a project in Next.js and have been creating components. I wanted to add some CSS styling to my components by linking an external CSS file using

<link rel="stylesheet" href="app.css" />
.

However, when I tried to implement this code, the page could not be found. Can anyone help me troubleshoot this issue?

Here is a snippet of my code:

const Header = () => {
    return (
      <Fragment>
        <Head>
          <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
          <meta charset="UTF-8" />
          <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
          />
          <title>Site | Title</title>
          <link href="/statics/app.css" rel="stylesheet" />
        </Head>
      </Fragment>
    );
}

If anyone has any suggestions or solutions, please let me know. Thank you and best regards.

Answer №1

When working with Next.js, which is a React framework, you can handle styling similar to how you would in React. If you are using an external CSS file, simply import it at the beginning of your component file. For example, if you have a class named Foo defined in app.css:

.Foo {
    color: blue;
}
import '/statics/app.css';

const Header = () => {
    return (
      <Fragment>
        <Head>
          <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
          <meta charset="UTF-8" />
          <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
          />
          <title>Site | Title</title>
        </Head>
       <>
         <p className="Foo">Bar</p>
       </>
      </Fragment>
    );
}

If you need to apply styles globally or define themes for your entire application, you can do so in your pages/_app.js file. For more information on this topic, visit this resource.

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

What is the process for activating my redux function within a component?

I'm working on a form that should create a new user when submitted. In my handleCreate function, I want to use Redux to trigger the addUser action and update the state to add the new user. However, it seems like I'm having trouble calling the act ...

What is the best way to position a div next to a form field?

My current HTML code is shown below. <div class="field"> <label>E-mail address: </label> <input type="text" id="email" name='email' style="width:200px;"></input> < ...

Creating a user-friendly login feature within the navigation bar of an HTML webpage

As someone who is new to CSS, I am currently working on a welcome page that includes a login section in the navbar. While I have successfully created div sections for the entire page, I am seeking assistance with coding the navbar using CSS. The current C ...

I am having trouble with my React Router v6 code - when I use the navigate function, the component does not load

As a beginner, I appreciate your patience as I navigate through this Codesandbox to learn about the newest version of React Router v6 Preview. Currently, I am encountering difficulties configuring the routes. My goal is simple - I have a button that, when ...

Combine React 17 with webpack 5 and babel-plugin-react-css-modules, enabling the use of CSS modules with stylename functionality

I am in the process of setting up a new React application using the latest technologies available, including React 17, Webpack 5, and other essential modules. My goal is to implement CSS modules utilizing the styleName concept with the help of babel-plugi ...

Having trouble displaying HTML UL content conditionally in Reactjs?

My goal is to display a list of items, limited to a maximum of 5 items, with a "more" button that appears conditionally based on the number of items in the list. However, I am encountering an issue where passing in 5 li items causes them to be rendered as ...

My CSS letters are running off the screen. What's the best way to show the complete

Seeking assistance with a header text formatting issue on my website. I am using Google Fonts (Pinyon Script) and have noticed that one of the letters is being cut off. Despite trying adjustments with overflow properties, the problem persists. The specific ...

JavaScript in Reactjs is throwing an Uncaught TypeError saying that projects.map is not a function. This issue started happening after I made some modifications in

Previously, my code was functioning well with both tables in MySQL having just "id" as the primary key. To add more specificity and introduce a foreign key, I decided to rename the primary keys in the two tables as follows: Bugtracker_table: id --> pro ...

The result of Netinfo.isConnected.fetch() is a positive response

I currently implement NetInfo in my application to verify internet connectivity. NetInfo.isConnected.fetch().then( (isConnected) => { console.log('isConnected: ', isConnected); }); This method will return true if I am connected to a rout ...

Incorporating additional options onto the menu

I recently designed a menu on https://codepen.io/ettrics/pen/ZYqKGb with 5 unique menu titles. However, I now have the need to add a sixth title to the menu. After attempting to do so by adding "strip6" in my CSS, the menu ended up malfunctioning and looki ...

Attempting to display four videos in my application by making an API request to YouTube

import React, { useState, useEffect } from 'react'; Grabbing my custom hooks function useFetch(url, defaultResponse) { const [data, setData] = useState(defaultResponse); async function getDataFromAPI(url) { try { ...

How to easily make a table in the center of the page using

I have a collection of 20 icons that I would like to arrange in a centered horizontal line of images. However, I am looking for a responsive solution and cannot use an HTML table. It needs to be mobile-friendly and wrap properly for small screens. If I wer ...

Having trouble applying CSS styles to the root element despite using a CSS file

My reactjs entry component setup looks like this: import React from "react" import ReactDOM from 'react-dom'; import App from "./js/components/App.js" import './index.css'; ReactDOM.render(<App />, document.getElementById(' ...

Testing a React component's function within the confines of a Redux Provider component

My current challenge involves unit-testing a React component called TodoList. This component essentially maps through items and displays them in the render method. These items, of type TodoItem, are retrieved from the Redux store using MapStateToProps. Be ...

Steps for styling the header of an Antd Collapse Panel

I'm attempting to modify the text color in the header of an ant panel. Below is the entire ant collapse component code: <Collapse accordion defaultActiveKey={defaultOpenPanel}> <StyledCollapsePanel key="tasksAssignedToMe" header={ ...

Experimenting with the testing of two distinct functions

Hello, I am new to the world of testing and I would appreciate some guidance. I have two functions that I need help with. The first function is a bits-per-second converter that converts data into a more readable format. const convertBitrate = bitrate =&g ...

Is it possible to hide a class by toggling it with jQuery mobile's click event?

I've come across an issue in my CSS where I have a class called hide that I applied to a textarea. When clicking a button, I want the class to be removed, but for some reason, it's not working as expected. I even added an alert in the function to ...

Could there be an error within the React documentation regarding the concept of How to approach problem-solving in React?

Starting my journey with React, I decided to dive into the documentation first. In the latest section on "Thinking in React" (https://reactjs.org/docs/thinking-in-react.html), they suggest asking specific questions to determine whether a particular piece o ...

Utilizing React Typescript to Efficiently Manage Multiple Checkboxes within a List

I'm working on a scenario where I have to manage multiple checkboxes in a list Only one checkbox can be selected at a time For example, if I toggle on Checkbox 1 and then click on Checkbox 2 - I want to automatically toggle off Checkbox 1 as I toggl ...

Styling for a <h:panelGrid using CSS

JSF / CSS / Trinidad Note - these three are part of a delivery from IBM ILog and cannot be changed JSF 1.2-1.2_07-b03-FCS JSTL 1_1-mr2 (special build) Trinidad 1.2.8 Java 1.6.0_22-b04 Eclipse 3.6.0 (Helios) Tomcat 6.0.28 (needs to run also on Weblog ...