Set background in React.js using style

I want to add a background image in React.js using a style definition that works:

let imgUrl = 'images/berlin.jpg' let styles = { root: { backgroundImage: 'url(' + imgUrl + ')', overflow: 'hidden', }, ... 

enter image description here

As you can see, the image repeats in the x direction. So I wanted to expand it:

 let imgUrl = 'images/berlin.jpg' let styles = { root: { backgroundImage: 'url(' + imgUrl + ')', backgroundImage: { flex: 1, resizeMode: 'cover', // or 'stretch' }, overflow: 'hidden', }, ... 

But the image no longer loads:

enter image description here

So how to set background image and adjust it in React.js?

+5
source share
3 answers

It works:

  let imgUrl = 'images/berlin.jpg' let styles = { root: { backgroundImage: 'url(' + imgUrl + ')', backgroundSize: 'cover', overflow: 'hidden', }, ... 

enter image description here

+8
source

Have you tried something like this:

 let imgUrl = 'images/berlin.jpg' let styles = { root: { background: 'url('+ imgUrl + ') noRepeat center center fixed', backgroundSize: 'cover', } 

Inspired from this post: Stretch and scale CSS image in background - only with CSS

+4
source

Clarification of another answer

 let imgUrl = 'images/berlin.jpg' let styles = { root: { backgroundImage: `url(${ imgUrl })` backgroundRepeat : 'no-repeat', backgroundPosition: 'center', } } 
+3
source

Source: https://habr.com/ru/post/1246434/


All Articles