3

I have the entry component in reactjs:

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

I am trying to style in the root element inside of my css file, index.css:

  #root {

    background-color: brown;
    height:100vh;
    width:100vh;
  }

The background though is not brown. When looking at the styling the browser renders I cannot see any of these attributes being applied to the root element.

This is my webpack config:

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 is as if it is not loading the index.css file at all.

user2924127
  • 6,034
  • 16
  • 78
  • 136

3 Answers3

0

You cannot style the #root of your app. Just style the className of <App /> component, for example:

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

css file:

.app {
  background-color: #ffffff;
  background-color: brown;
  height:100vh;
  width:100vh;
}
mfakhrusy
  • 1,128
  • 1
  • 11
  • 20
0

When using webpack, i think you have to specify your entry points of your css/js files like this...

entry: [
        "./src/js/index.js",
        "./src/sass/style.scss",
    ],
    output: {
        filename: "bundle.js",
        path: path.resolve(__dirname, 'assets/js')
    },
    module: {
        rules: [
/* you code here */
...
...
DigitalJedi
  • 1,577
  • 1
  • 10
  • 29
0

soln-workaround 1

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

soln-workaround 2

  • put the <style> directly in the html, instead of inside the css.
  • vv
    <style>
      #root {
        height: 100%;
        border: 1px solid green;
      }
    </style>
    

misc

Nor.Z
  • 555
  • 1
  • 5
  • 13