Reference file in public folder from CSS in create-react-app

后端 未结 3 1506
攒了一身酷
攒了一身酷 2021-02-07 08:07

I am using creat-react-app (CRA) and simply want to include a png file placed in the public folder via CSS (I keep all image files there). Now I am trying to reference this imag

相关标签:
3条回答
  • 2021-02-07 08:42

    Remove the extra .. in the relative path. :)

    0 讨论(0)
  • 2021-02-07 08:48

    In my case, to access the images from css/scss, had to move the images directory as well as fonts, to the src directory. After which i was able to refer them in the css/scss files directly,

     background-image: url("/images/background.jpg");
    

    references:

    https://github.com/facebook/create-react-app/issues/829

    https://create-react-app.dev/docs/using-the-public-folder/

    0 讨论(0)
  • 2021-02-07 08:57

    Just use a / before the name, this will make it relative to the build output root, which includes anything in the public folder.

    so for the question asked above:

    .App-header {
      background-color: #222;
      height: 150px;
      padding: 20px;
      color: white;
      background-image: url("/example.png");
    }
    

    the critical part being

    /example.png 
    

    refers to a file, example.png, that is in the public folder

    0 讨论(0)
提交回复
热议问题