iT邦幫忙

0

請問react用css的background:url()的問題

小弟在App.css裡幫App.js的某一個標籤設置了background:url()
圖片資料夾在public裡面,可是寫完後會顯示:

which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.

如果把圖片放到src裡面就沒有問題
想請問圖片檔案放在public裡面會無法使用background:url()嗎?

用img就沒有問題

相對與絕對路徑的問題。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Mao
iT邦新手 1 級 ‧ 2021-10-03 20:19:21
最佳解答

CSS 檔案無法讀取到 public 資料夾的檔案,
那是因為 React 對 src 資料夾做外部的導入限制
如果想使用 public 資料夾的圖,
可以在 App.js 內使用使用內聯樣式
style={{ backgroundImage: "url('/image.png')" }}

我要發表回答

立即登入回答