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('root'));

In my css file, index.css, I am styling the root element:

#root {
    background-color: brown;
    height:100vh;
    width:100vh;
}

However, the background color does not appear as brown when checking the browser's rendered styles. It seems that these attributes are not being applied to the root element.

This is the webpack configuration I am using:

const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader"
          }
        ]
      },
      {
        test: /\.css$/,
        use: ['css-loader'],
      },
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};

It appears that the index.css file is not being loaded at all.

Answer №1

If you want to customize the appearance of your app, avoid styling the #root element directly. Instead, focus on styling the className attribute of the <App /> component. Here's an example:

const App = () => (
  <div className="app>
  // ...
  </div>
)

In your CSS file, define the styles for the "app" class like this:

.app {
  background-color: #ffffff;
  color: brown;
  height: 100vh;
  width: 100vh;
}

Answer №2

When it comes to webpack configuration, it's important to define the entry points for your CSS and JavaScript files like so...

entry: [
        "./src/scripts/main.js",
        "./src/styles/style.css",
    ],
    output: {
        filename: "bundle.js",
        path: path.resolve(__dirname, 'dist/js')
    },
    module: {
        rules: [
/* insert your code here */
...
...

Answer №3

Unique Solution 1

  • try using Javascript
  • vv
    const root = document.getElementById('root') as HTMLDivElement;
    root.style.cssText = "height: 100%;       border: 1px solid green;";
    

Unique Solution 2

  • Add the <style> directly in the html file, instead of placing it inside a separate CSS file.
  • vv
    <style>
      #root {
        height: 100%;
        border: 1px solid green;
      }
    </style>
    

Miscellaneous Notes

  • I am facing the same issue and unsure how others have resolved it. I do not consider my solution to be effective.

  • The styling needs to be applied to the root element directly, rather than wrapping it in another <div> since the objective is to achieve height: 100%.

  • Using :root does not produce the desired effect

  • Applying styles to .App also does not yield the expected outcome

  • Related posts:

    Style root element using css file, but not working (this post)

    CSS 100% height not working in React app (I wonder why some individuals in this post do not encounter any issues...)

    Style background color of root React Javascript (origin of Unique Solution)

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 best way to generate a fresh set of data from a search parameter?

I have received data from an API call that needs to be filtered. I am utilizing Redux instead of local state for this task. Below are the actions I have defined: import ActionTypes from '../constants/ActionTypes'; export const passengersDataAc ...

Error: React JS is unable to access the property 'path' because it is undefined

Currently, I am encountering an issue while setting the src of my image in React to this.props.file[0].path. The problem arises because this state has not been set yet, resulting in a TypeError: Cannot read property 'path' of undefined. To provid ...

The only functioning href link is the last one

Currently, I am in the process of constructing a website using HTML and CSS. My goal is to create a white rectangle that contains 4 images, each serving as a clickable link redirecting users to different sections on the page. The issue I am facing is that ...

The Cascading of Bootstrap Card Designs

Looking for some assistance with my TV Show Searcher project that is based on an API. The functionality is complete, but I'm struggling to get the Bootstrap cards to stack neatly without any empty space between them. I want it to resemble the image ga ...

The border in my HTML table is not displaying when I run my flask application

My flask application was running smoothly in a virtual environment until I encountered an issue with the html borders not showing up. Even after seeking help from a friend who knows some html, the problem still persists. app.py: from flask import Flask, r ...

Initiating "UIVERI 5" with a simple click or triggering a function on a webpage

After developing test cases with the JS framework "UIVERI5" and running them using UIVERI5 in the terminal / console, I am now looking to integrate these test cases into a website. I am currently utilizing the JS framework Angular (although open to switchi ...

Invalid directory file not found in EOENT when parsing Windows symlinks using esprima and esparse

Recently, I inherited an old electron-webpack + react application and encountered a persistent error during the build process: Cannot read file C:\Users\baz\Desktop\foo-bar-app\node_modules\esprima\.bin\esparse: Er ...

Using React's useState in conjunction with useEffect will return the previous value

I am facing an issue where my state is not updating correctly based on the condition. When trying to access the value, it always shows the old state. Can someone please provide guidance on how to resolve this problem? const [isScrollReached, setIsScroll ...

Error occurs while attempting to configure

I am facing some confusion with the usage of aws-sdk in a React project. I have been trying to configure it, but encountered an error that has left me puzzled. Oddly enough, when I checked the object debugger everything seemed fine. However, as soon as I t ...

What could be the reason for the incorrect application of CSS to a button within React Material web components?

As a newcomer to React, I am facing some challenges with CSS styling in rwmc components. My goal is to render two Button components on a webpage by importing them from the '@rmwc/button' package. To guide me through this process, I am referring t ...

When inputting dates manually, the KeyboardDatePicker feature is deducting one day from the selected

When using the datepicker to select a date, everything works fine. However, when manually entering a date and then saving it, the browser ends up posting a different date to the server (only referring to the date part). My time zone is UTC+03. Below is my ...

Pressing the non-responsive button automatically chooses the following item

This example demonstrates how to create a disabled button: import { Grid, IconButton } from "@material-ui/core"; import ArrowBackIosIcon from "@material-ui/icons/ArrowBackIos"; export default function App() { const handleClick = (e) ...

How can I fix the position of the close button when using the paper component of a modal in material ui to ensure responsiveness on the screen

I recently created a cards page where multiple cards are displayed. Each card opens a modal component when clicked, but unfortunately, the close button is not functioning properly. Here's an image showing the issue with the button's position whe ...

What is the procedure for creating an array that contains elements taken sequentially from various arrays?

I am looking to extract specific elements from a given array: const initArray = [ { '18888': [ { unitId: 1, venueId: 18888, }, { unitId: 2, venueId: 18888, }, ], }, { &apos ...

Effortless sliding panel that appears on hover and vanishes when mouse is moved away

I am in the process of creating a menu for my website that utilizes linkbuttons which trigger additional linkbuttons to slide down upon hover. The desired effect is a smooth sliding panel that appears when hovering over the linkbutton, and disappears when ...

I am unable to alter the value of the input using React hooks

I'm relatively new to React and Hooks, and I'm attempting to create a contact form using hooks. I am facing an issue where I cannot update the values in the input fields or type anything, and the onChange() function is not triggering. There are ...

The alignment issue persists when attempting to set 'relative' on the parent and 'absolute' on the inner element in a Bootstrap column for bottom alignment

Here is a snippet of my basic HTML code: <div class = "row"> <div class = "col-xs-8"> <p>long long long long text that generates a taller column</p> </div> <div class = "col-xs-4"> <p> ...

What is the best method for utilizing the CSS :target selector in order to generate a versatile modal box?

I am looking for a way to display my vast collection of proverbs from an endangered language without overcrowding the page. I have implemented a modal box solution using CSS :target selector, which expands a hidden div element when clicked on. However, I n ...

Hide Material UI Drawer when clicking elsewhere

Attempting to implement the Drawer component in Material UI React. Want to ensure that the state within the Drawer component persists even when the Drawer is closed, so passing variant="persistent" in Drawer component. The issue arises with the ...

Import CSS styles from a dynamically imported ES6 module

My website is being developed using Webpack with Laravel Mix. I am looking to dynamically import an ES6 module that has dependencies on other modules and stylesheets. Below is the code for the dynamically imported module (loadTextillate.js): import style ...