在nextjs中将背景图像设置为div不起作用

Jac*_*k B 3 html css jsx next.js

我是 nextjs 的新手,我不明白为什么没有为此 div 设置背景图像。在根目录中,我有一个名为 public 的文件夹,其中包含art-thing.jpg文件。styles.css在根目录中。index.js位于根目录的 pages 目录中。我可以将背景设置为颜色,但无法将其设置为图像。有人有类似问题的帖子,但我希望我的 css 不是内联的。

样式文件

html {
    scroll-behavior: smooth;
    height: 100%;
}

body {
    margin: 0px;
    padding: 0px;
}
.page-container {
    height: 100vh;
    position: relative;
}

.page-container-welcome {
    height: 100vh;
    position: relative;
    background-size: cover;
    background-image: url("/public/img/Branding/art-thing.jpg");
}
.content-wrap{
    padding-bottom: 2.5rem;
}
Run Code Online (Sandbox Code Playgroud)

索引.js


import 'bootstrap/dist/css/bootstrap.min.css';
import Head from 'next/head';
import HekateNav from "../client/components/nav";
import Footer from "../client/components/footer";

function Home() {
    return (
        <div>
            <Head>
                <title>Hekate</title>
            </Head>

            <div className="page-container-welcome">
                <div className="content-wrap">
                    <HekateNav/>
                </div>
            </div>
            <Footer/>
        </div>
    );
};
export default Home
Run Code Online (Sandbox Code Playgroud)

我得到的控制台错误

DevTools failed to load SourceMap: Could not load content for http://localhost:3000/_next/static/css/bootstrap.min.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

Run Code Online (Sandbox Code Playgroud)

Jac*_*k B 5

解决方案是通过删除公共路径来更改图像的路径。这是回答here将路径更改为 /img 而不是 /public/image。在这种情况下,/public 是站点的实际根目录。

.page-container-welcome {
    height: 100vh;
    position: relative;
    background-size: cover;
    background-image: url("/img/art-thing.jpg");
}
Run Code Online (Sandbox Code Playgroud)