Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the background color of the body?

Tags:

reactjs

I'm using React.js and want to change the background color of the entire page. I can't figure out how to do this. Please help, thank you.

Edit (Sep 2 '18): I have a project on GitHub that I'm linking here. I don't have this project online right now, but in the /client folder is the client server. Check it out. This is how I answered the question.

like image 835
Ethan Davis Avatar asked Feb 26 '17 04:02

Ethan Davis


People also ask

How do I change the background color of my HTML body?

To set the background color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <body> tag, with the CSS property background-color. HTML5 do not support the <body> tag bgcolor attribute, so the CSS style is used to add background color.

How do you change the background from black to white?

Open your device's Settings app . Select Accessibility. Under "Display," select Color inversion. Turn on Use color inversion.

How do you change the background color in CSS?

In CSS, we use the background-color property to specify the background color of an element. In the code snippet below, the entire color of the web page is set using the background-color property.


4 Answers

The simplest solution is a bit hacky, but you can use raw javascript to modify the body style:

document.body.style = 'background: red;';
// Or with CSS
document.body.classList.add('background-red');

A cleaner solution could be to use a head manager like react-helmet or next.js Head component.

import React from 'react';
import {Helmet} from 'react-helmet';

class Application extends React.Component {
  render () {
    return (
        <div className="application">
            <Helmet>
                <style>{'body { background-color: red; }'}</style>
            </Helmet>
            ...
        </div>
    );
  }
};

Some css-in-js also offers tools to manage global level styles; like styled-components injectGlobal.

In the end, there's a lot of tools providing cleaner ways to handle this. But if you don't want to rely on third party, the raw JS option might be good enough if you don't make it too interactive.

like image 60
Simon Boudrias Avatar answered Oct 27 '22 22:10

Simon Boudrias


The simplest solution without doing anything fancy is to:

1) Open public/index.html

2) Add an inline style to your body like this: <body style="background-color: red">

like image 26
Reni Avatar answered Oct 28 '22 00:10

Reni


try this to your code

componentDidMount() {
    document.body.style.backgroundColor = "red"
}
like image 42
superup Avatar answered Oct 27 '22 23:10

superup


React Helmet (https://github.com/nfl/react-helmet)

I really found this library very helpfull. Really clean solution i would say.

Sample Usage:

import Helmet from 'react-helmet';

<Helmet bodyAttributes={{style: 'background-color : #fff'}}/>
like image 37
Rahul Avatar answered Oct 28 '22 00:10

Rahul