Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
setting background-image from props not working

How can i set background-image from props. I tried different variations but cant get it to work. css is showing in html but image is not being displayed.

const cover =   "http://www.w3schools.com/css/trolltunga.jpg";

class App extends React.Component {
  render(){
    return(
      <div>
        <h2>Hey</h2>
        <Child image = {cover} />
      </div>
    )
  }
}

class Child extends React.Component{
  render(){
    const style = {
      backgroundImage: "url("+this.props.image+")",
      backgroundSize: "100% 100%"
    }
    return(
      <div style={style}></div>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById("app")
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.min.js"></script>

<div id="app"></div>

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Problem is ur div is empty, thats why background image is not visible. put some content inside that, it will be visible, Try this:

<div style={style}>Add background</div>

check the jsfiddle, ur code is working properly. I just added the above line in that: https://jsfiddle.net/0z38gozj/

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!