Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda