The Silent Disregard for CSS Files in React Rendered Pages

I'm currently working on a React application that I created using create-react-app. In my index.html file, I have linked it to my CSS file like this:

<link rel="stylesheet" href="../src/site.css"></link>

The content of the site.css file is as follows:

body {
  background-color: green;
  height:100%;
  width:100%; 
}

However, when I run my app, the styles are not being applied to the body even though the CSS file is successfully downloaded.

Answer №1

If you're working with create-react-app, keep in mind that the src directory is not directly served. To include CSS files, either relocate them to the public directory or import them within your JavaScript files.

All assets are bundled using Webpack and appropriate loaders for stylesheets. The most straightforward approach would be to eliminate the link tag from your public/index.html and instead import the CSS file in your src/index.js:

import "./site.css";

In case you must link to the stylesheet from the HTML file, move it to public/site.css and update the link tag accordingly:

<link rel="stylesheet" href="/site.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

Animate sliding bar to move from the left using jQuery

I am currently experiencing an issue with a sliding animation on mouseover in the navigation. The animation works fine, but the problem arises when I hover over any menu item - the sliding bar starts from the very left instead of starting where the navigat ...

Click a button to show or hide text

I am attempting to create a button that will toggle text visibility from hidden using 'none' to visible using 'block'. I have tried the following code but unfortunately, it did not yield the desired outcome. <p id='demo' s ...

Leveraging state for logic within the same rendering cycle

I am struggling to create a loader in React using functional components that displays multiple posts. The issue I am facing is how to use the current state, which changes during the render cycle, to impact other component logic. For instance, in the code ...

Changing the border color of material ui Rating stars: A guide

I am struggling to change the border color of stars in Material UI Rating because my background color is black, making it difficult to see when the star is empty! Here's the code snippet: import Rating from '@material-ui/lab/Rating'; import ...

Node express not serving Gzip file properly

I'm having trouble serving a gzipped file using node/express for a react app. Despite my efforts, I keep receiving the large 1.2MB file instead of the expected gzipped 300kb file. Below is the code snippet for the express route handler that I am curre ...

Tips for creating a hovering bar underneath your links

I'm currently working on designing a stylish navigation bar using CSS, and I have a specific feature in mind. When a user hovers over a link in the navigation bar, I want a colored bar to appear under a different link. Essentially, I need the backgrou ...

What is the best way to apply CSS max-left or min-left positioning to an absolute element?

I currently have an element with the CSS properties: position:absolute; left: 70%; Is there a way to restrict this element from moving more than 900px from the left side? Similar to max-width but for positioning? ...

Is CSS General sibling selector not functioning properly when used with :focus?

How can I display text that turns into an input form when hovered over, and stays visible even if it's unhovered but the user is interacting with the input form? The issue is that when the input form is focused while unhovered, both the input form an ...

Guide on configuring remix using aws cdk

Currently, I am delving into the world of remix and attempting to configure a remix project that utilizes AWS CDK for the server. I stumbled upon this GitHub example: https://github.com/ajhaining/remix-cloudfront-cdk-example However, it lacks clarity on ...

Sending data to a child component within Storybook

Looking for a more efficient method to pass values to a nested component in Storybook. Instead of explicitly listing each value, is there a way to set them all at once? function Dropdown({ label, type, size, title, disabled }) { return ( <div cl ...

What is the best way to vertically align a pseudo element image using CSS?

After looking for similar questions, none of the answers seem to be working for my specific issue. This is what I'm struggling with: I am attempting to insert and center a decorative bracket image before and after certain content in the following man ...

Adjustable centralized menu with buttons that resize accordingly

I am currently working on making a webpage responsive. I have successfully created a logo div that resizes accordingly. Now, I am facing a challenge with centering the buttons in the menu list. Despite my efforts, the buttons are not aligning in the center ...

Is there a way to navigate through a component on mobile devices when in responsive mode?

click here for image description I need assistance with implementing scroll functionality on this component in mobile view to prevent overflow and maintain responsiveness. Specifically, I want the component to scroll horizontally (scrollX) without causing ...

The CSS3 Animation remains stationary

I've been attempting to animate my block element moving to the left using CSS animation shorthand property, but unfortunately, it's not working as expected. Here is my HTML: <div id="game"> <div id="block">&l ...

Mongo deletion causing backend crashes without any error message

Seeking help with my first ever question, unable to find a solution so here it is. My tech stack includes React, Mongo, Express, and Node. Dealing with PatientDetails.js file. This code snippet demonstrates how I retrieve a specific patient's detai ...

What advantages does the use of $(e).attr(name,value) offer compared to using e.setAttribute(name,value)?

Scenario: The variable "e" represents an element of type "HtmlElement" and not a "css selector" I am referring to any attribute, not just the standard allowed ones like "atom-type" or "data-atom-type". Regardless of the attribute name, will it function wi ...

Having trouble with Redux's mapStateToProps function?

I'm working on a React component that triggers an AJAX call in the componentWillMount lifecycle method and then stores the fetched data in a Redux store. Here's the code snippet: componentWillMount() { var self = this; var xmlhttp = new XMLH ...

What is the best way to ensure a CSS element maintains its position margins even after adjusting them with JavaScript?

Currently, I am in the process of developing a minesweeper game using a combination of HTML, CSS, and JavaScript. The code snippet I am utilizing to generate the grid is as follows: <div id="game-space"></div> <script type="t ...

cleaner urls using nextjs routing

Currently working on developing a fresh marketing platform for my company utilizing next.js, and encountering some hurdles with URLs. Essentially, I've created a custom API route to retrieve data from our internal database using Prisma: getAllDealers ...

Error encountered when using LocalizationProvider for localization services

Currently, I am using the following packages in my project: "material-react-table": "^1.6.5" | "@material-ui/core": "^4.12.4" | "@mui/material": "^5.11.7" | "@mui/styles": "^5.11.7 ...