1

I want to apply background image for specific component.

I used react-router-dom and my code is below.

[App.js]

import React, { Component } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import Login from './components/Login';
import Home from './components/Home';

class App extends Component {
  render() {
    return (
      <Router>
        <div>
          <Route exact path="/" component={Login} />
          <Route path="/home" component={Home} />
        </div>
      </Router>
    );
  }
}

export default App;

[Login.js]

import React, { Component } from 'react';
import './Login.css';

class Login extends Component {
    render() {
        return (
            <div>
               Login
            </div>
        );
    }
}

export default Login;

[Login.css]

html {
    background-color: red;
}

[Home.js]

import React, { Component } from 'react';
import './Home.css';

class Home extends Component {
    render() {
        return (
            <div>
                Home
            </div>
        );
    }
}

export default Home;

[Home.css]

html {
    background-color: blue;
}

I set the background-color of Login to red and Home to blue.

But not only Login.js but also Home.js's background color is blue.

How can I set the different background color for each components?

2 Answers 2

4

Apply styles to class

Assign a class to the outermost div in Login.js

class Login extends Component {
    render() {
        return (
            <div className="login">
               Login
            </div>
        );
    }
}

export default Login;

Now apply styles to the classes

.home{
    background-color:blue;
    }

.login{
  background-color:red;
  }

If u want to apply background image for full page try this css..

.home {
    background: url("image.jpg");
    background-size: cover;
    background-repeat: no-repeat;
 }

Sign up to request clarification or add additional context in comments.

2 Comments

Works perfectly. Thanks!
Anyway, It's a different matter, I apply background: url("image.jpg"). But It doesn't filled full screen. After height: 100%, is also doesn't work. How can I solve this issue?
0

Change the css to:

body {
    background-color: red !important;
}

The background color property is set to the body tag, not the html tag. The !important will ensure that this style is applied over any other conflicts you may have.

--Edit--

To apply background colors to the individual components, you should add a class to each of the parent div, and style that class directly like so:

Note: Heights have been added to the styles to ensure 100% vertical fill of the browser, as per the OP's request. It is not required otherwise.

Login.js

import React, { Component } from 'react';
import './Login.css';

class Login extends Component {
    render() {
        return (
            <div className="scene_login">
               Login
            </div>
        );
    }
}

export default Login;

Login.css

body, html {
     height: 100%;
}

.scene_login {
    height: 100%;
    background-color: red;
}

Home.js

import React, { Component } from 'react';
import './Home.css';

class Home extends Component {
    render() {
        return (
            <div className="scene__home">
                Home
            </div>
        );
    }
}

export default Home;

Home.css

body, html {
    height: 100%;
}

.scene__home {
    height: 100%;
    background-color: blue;
}

5 Comments

What about giving different background-color to different components which seems to be what the question is about.
I've updated my answer with a solution for this. I took the original approach of changing the body background colour as it looked as if this is what the OP was trying to achieve.
@TPHughes Works fine. Thanks! :)
@TPHughes Anyway, It's a different matter, I apply background: url("image.jpg"). But It doesn't filled full screen. After height: 100%, is also doesn't work. How can I solve this issue?
I've updated the answer with additional CSS that will ensure that the div is stretching to full height

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.