0

Ok this has been bugging me for a while ,after searching alot in the web I found that this should work in index.css

body {
    background-image: url(../src/components/images/photo.jpeg) no-repeat center center 
    fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

However it apparently doenst work .Can someone please tell me how to fix this?

2
  • 3
    How exactly is this question related to React?
    – Jon Koops
    Commented Oct 1, 2018 at 15:04
  • Well I am trying to have a background-Image as full size cover in ReactJs. maybe there is a better way like <BackgroundImage/> in React-Native.Thats why i tagged it Commented Oct 1, 2018 at 15:06

2 Answers 2

3

You're using long-hand syntax, and this doesn't support other values except url. So, just use:

background-image: url(../src/components/images/photo.jpeg)

Further, a better approach to use images from public folder, so you can use like this:

background-image: url(/images/photo.jpeg)

To add other values, use:

background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;

To use short-hand syntax, use:

background: url(/images/photo.jpeg) no-repeat center center fixed; 
5
  • I tryed your approach aswell, no changes in my background still empty screen.I only need to add it to index.css and then it should work right? Commented Oct 1, 2018 at 15:13
  • You can use my approach from anywhere but the style should be binding in the component. Commented Oct 1, 2018 at 15:15
  • I am not sure I follow. I want my whole body to have this background Image, do I need to set anything in my App.js? doesnt setting body with your approach on index.css cover it? Commented Oct 1, 2018 at 15:19
  • be sure to use width and height to 100% Commented Oct 1, 2018 at 18:38
  • I finally figured it out.I had imported this line to my index.js file import 'semantic-ui-css/semantic.min.css'; and this was messing with my css Commented Oct 3, 2018 at 16:43
0

try using style

<body
  style={{
    backgroundImage: `url(${imageUrl})`,
  }}
></body>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.