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

Encountering an unidentified entity within a ReactJS application

Within a container, I've included a search bar inside a form element with two inputs - from and to, along with a submit button. Upon submitting the form, I create an OBJECT named query which looks like this: const query = { from : this.s ...

What could be causing the bootstrap columns to automatically shift onto separate lines?

Check out my code snippet: html, body { width: 100%; height: 100%; margin: 0; padding: 0; overflow-x: hidden; } .row { width: 100%; height: 80%; margin: 0px; padding: 0px; } body { background: #EFEFEF; } .container-fluid { wid ...

Dots are used to indicate overflow of title in React Material UI CardHeader

Is there a way to add ellipsis dots to the title in my Cardheader when it exceeds the parent's width (Card width)? Here is what I have attempted so far: card: { width: 275, display: "flex" }, overflowWithDots: { textOverflow: &apo ...

How to apply custom styling to a specific element within an Angular Material 2 component using CSS based on its Id or

My EntryComponent features a material button menu where I attempted to customize the default style using ::ng-deep. However, the changes affected all button components in the parent Component as well. <style> ::ng-deep .mat-button{ max-width: 50 ...

Does text disappear when hovered over in CSS?

Having some trouble getting a text to display on hover over the parent container while adjusting the opacity. Despite trying multiple methods, it just won't show up no matter what I do! Here is my current code: .project{ position: relative; ...

You can only use the 'iframe' tag as a child of the 'noscript' tag. Were you trying to use 'amp-iframe' instead? - NextJs

We are currently experiencing an issue with AMP errors. The error message states: "The tag 'iframe' may only appear as a descendant of tag 'noscript'. Did you mean 'amp-iframe'?" It's important to note that custom JavaSc ...

Update websocket-extensions version from 0.1.3 to 0.1.4

After transferring my project to GitHub, I received multiple security alerts from dependabot. All the errors were related to some external dependencies, which I fixed by updating versions in package.json. However, one dependency, websocket-extensions, had ...

Tips for defining conditions within the style attribute in JSP

Below is the code that I attempted: <div style="width:85%;"> <input class="formbutt" value="AddNew" title="AddNew" type="button" style="{(${projectEnvironmentBean.divStyle}=='dipslay:none') ? 'display:block' :'display: ...

Interactive image grid with adjustable description field per image upon selection

My goal is to create a grid of images with a single text field below the grid. This text field should display the description of the image that was last clicked. The grid is implemented using floating divs within a main div, as shown in the code snippet be ...

What is the Method for Multiplying Two ng-module Values in AngularJS?

In my application, I am utilizing the MEAN stack with AngularJS for the front-end. I have a table that contains two values - 'Payment value' with commas and 'commission' without commas. How can I multiply these two values? For example: ...

The data updating issue in Next.js 13.5+ production mode needs to be resolved

Currently, I am dealing with next js 13.5+ and facing an issue after deploying to Versel. The new data is not being fetched as expected. I have come across similar issues on Stack Overflow where others suggest using the fetch method instead of the axios me ...

What is the best way to send data from a child component to its parent in React?

Is there a way to pass a modified prop from a Child Component to a Parent Component? Here is the scenario: I am currently working on an existing codebase that has a Parent Component nested in an 'unstated.Container' and a separate Child Componen ...

Exclude mock files from webpack configuration in AngularClass/angular2-webpack-starter for production builds

I've encountered some obstacles while working with the AngularClass/angular2-webpack-starter framework, specifically in configuring its webpack settings. My goal is straightforward, yet I've been struggling to achieve it. In my project directory ...

Is there a way to make the text field in a redux-form FieldArray read-only with initial values, but allow it to be editable when a new field.push

I am facing a challenge in creating a redux-form that loads data into a modal with initial values. Specifically, I want one of the fields (quoteField) to render as readOnly when there is initial data present. The initial data will always contain at least o ...

Tips for positioning dynamic high charts in a bootstrap row

I have implemented HighCharts for displaying charts on my website. Specifically, I am utilizing the chart type solidgauge. The charts are dynamic and I am looking to arrange them horizontally across the page. My goal is to align the charts in a layout sim ...

Tips to avoid the page from scrolling to the top when the menu is opened

Whenever a user taps the menu button on the page, a full-page menu opens. However, there is an issue - the main content page automatically scrolls to the top. Can you provide suggestions on how to prevent this from happening? I have already looked into a s ...

Ensure that a DIV element stretches beyond the limits of its container

I am having an issue with a div that should overlap everything else as it functions as a menu. Despite trying various positioning tricks, the desired effect is not achieved. The div is only shown when hovering over a certain element. Here is the structure ...

Transferring a PDF document to Next.js

I am facing an issue while trying to upload a PDF file (CV) on next js. Everything seems fine according to my expectations, but when I console.log(req.data) it displays [object:object] Please note: When I console.log the data in frontend, it works fi ...

Cross-origin resource sharing (CORS) will still be ineffective, even when the Access-Control-Allow-Origin

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus quis est eu arcu tincidunt pulvinar. Aenean vel sapien vitae quam varius vulputate. Vestibulum et lacinia sem. Vivamus tristique mi ac metus tincidunt eget. // Donec fermentum al ...

I am facing an issue where the inline CSS is not being applied in my

I recently integrated tcpdf in my CodeIgniter project and encountered an issue with applying inline CSS to td. This is a snippet of my code: <table style="float:left;width:100%; border:1px solid #c2c2c2; margin-top:10px;"> <tr style="float:l ...