2

Here I have one div This div image I want to set on whole background but it not set full background(see below screenshot). My project is using reactjs. How to set Image in whole background ?

enter image description here

<div
   style={{
      backgroundImage: `url(${defaultImages.backgroundOfImage})`,
      backgroundRepeat: 'no-repeat',
      backgroundPosition: 'cover',
   }}
  >
</div>
7
  • 2
    backgroundSize, not backgroundPosition. This is mainly about not knowing which CSS rule to use, has nothing to do with React. Commented Mar 19, 2019 at 9:49
  • Possible duplicate: stackoverflow.com/questions/22887548/…. Not closing as this is on CSS and this post is on react Commented Mar 19, 2019 at 9:50
  • 1
    you can have a look over here a similar problem stackoverflow.com/a/39196525/8425297 Commented Mar 19, 2019 at 9:50
  • @SergiuParaschiv I use backgroundSize so My image goes blur how to stop to blur background image Commented Mar 19, 2019 at 9:55
  • 1
    @DDD, is it blur because your image is too small to take the cover size or do you have a blur effect in your image (photoshop or CSS property) ? Commented Mar 19, 2019 at 9:57

2 Answers 2

3

The CSS property you are looking for is background-size, not background-position. The snippet that I normally use for doing this is:

background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-image: url(path/to/image);

Or with React

<div style={{
  backgroundSize: 'cover',
  backgroundRepeat: 'no-repeat',
  backgroundPosition: 'center',
  backgroundImage: `url(${pathToImage})`,
}} />

Edit: This is probably out of the scope of the question, but I suggest you declare the style object somewhere else and import it each time you want to have the same background effect for reusability's sake

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

2 Comments

I refer your answer It works but My image goes blur how to stop to blur image
@DDD That's because your image is to small. Perhaps you need to get a larger one
0

This has nothing to do with ReactJS, but merely CSS issue. You should use background-size:cover instead of background-position since background position property sets the starting position of a background image, but not setting the size of image.

Comments

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.