Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create react app not loading css background images in dev or build

I'm having difficulty with some background images not loading. I've made some significant modifications to the initial create react app, my folder structure is now as follows:

Note: I have omitted some files & folders, if you need more please let me know.

App/
 node_modules/
 src/
  client/
   build/
   node_modules/
   public/
   src/
    css/
     App.css
    images/
     tree.png
     yeti.png
    App.jsx
  server/
 package.json
 Procfile

Here are the steps I take to create this issue:

$ cd src/server && npm run dev

This will boot up the dev server and open the browser to my app, everything is working fine except for some elements on the page not displaying a image.

Note: I load in an image yeti.png and this renders fine.

App.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import './css/App.css';
import yeti from './images/yeti.png';

const Footer = function(props) {
  return (
    <div>
      <Yeti />
      <Trees />
    </div>
    );
};

const Yeti = function(props) {
  return (        
      <img
        src={yeti}
        className="yeti yeti--xs"
        alt="Yeti"
      />
    );
};

const Trees = function(props) {
  return (        
      <div className="trees">
        <div className="trees__tree trees__tree--1"></div>
        <div className="trees__tree trees__tree--2"></div>
      </div>
    );
};

ReactDOM.render(
  <Footer />,
  document.getElementById('root')
);

App.css

.trees {
    bottom: 0;
    height: 110px;
    left: 0;
    position: fixed;
    width: 100%;
    z-index: 1;
}

.trees__tree {
    background-size: 30px;
    background: url('../images/tree.png') no-repeat;
    float: left;
    height: 50px;
    width: 30px;
}

.trees__tree--1 {
    margin: 0 0 0 6%;
}

.trees__tree--2 {
    margin: 2% 0 0 4%;
}

When I inspect the elements in Chrome, the path to the images appears to be correct. When I hover over the path to the image in the styles tab of my inspector the image appears.

styles source

Note that the path for the image I import is similar to the path for the background image:

enter image description here

If I were to import the tree.png as import tree from './images/tree.png'; and change my two <div> elements to <img src={tree} role="presentation" className="trees__tree trees__tree--1" /> and <img src={tree} role="presentation" className="trees__tree trees__tree--2" /> the images will of course load.

How can I get the background images to display? I have other background images in the app that are not loading so the previously mentioned bandaid will not help me. I'm afraid to eject my app and mess with the configuration.

I'm also having the same problem when I build the app.

If you need to see more of the source you can view it at https://github.com/studio174/ispellits but bare in mind I have simplied this example to isolate the problem. The issue I am having is actually in the Footer.jsx and Footer.css

like image 624
j_quelly Avatar asked Dec 01 '16 19:12

j_quelly


People also ask

How do I add a background image in Create react app?

Method 2: Using external CSS: In this method, we add an external CSS file to set a background image for the react component. In App. js, we will add a simple div element with the className attribute. Also, we import an external CSS file to set a background image for the div element.

How do you call a background image in react JS?

import image from "./img/UpmostlyLogo. png"; function Component() { return ( <div style={{ backgroundImage:`url(${image})`,backgroundRepeat:"no-repeat",backgroundSize:"contain" }}> Hello World </div> ); } export { Component }; All we did was add “backgroundSize: 'contain' ” to the div's styles.

Does create react app support CSS modules?

CSS Modules are supported with create-react-app out of the box. There is however a catch that you need to know in order to get your CSS Modules working in your create-react-app project.

Why not to use create react app in production?

However, it is not created for production. Unfortunately, devs have been using it for all their projects. CRA limits your ability to change any configurations, it has tons of unneeded dependencies, you can't customize configurations, you can't build microfrontends …


2 Answers

The issue was purely an issue with CSS in the App.css file:

App.css

.trees__tree {    
    background: url('../images/tree.png') no-repeat;
    background-size: 30px; /** moved this property down */
    float: left;
    height: 50px;
    width: 30px;
}
like image 96
j_quelly Avatar answered Sep 19 '22 17:09

j_quelly


My issue was specifically that I did not have background-size: 30px; defined. Thanks for this.

like image 36
IonicBurger Avatar answered Sep 22 '22 17:09

IonicBurger