You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

网站背景图无法显示在导航栏下方,设置背景图出现重复问题

背景图无法正常显示在导航栏下方,且出现重复显示的问题

我有一个基础网站,需要一张背景图显示在所有元素下方,但实际显示时,它永远不会出现在导航栏下方。即使将其设置为背景图,也不会正常显示,反而会在导航栏背景中显示该图片的副本。

效果说明

  • 直接插入图片时:导航栏区域无法显示背景图,仅主体区域有图
  • 同时设置为背景图和普通图片时:导航栏和主体区域各显示一次背景图,出现重复
  • 仅设置为背景图并移除普通图片标签时:背景图在导航栏和主体区域重复显示

我的代码

HTML代码

<!DOCTYPE html>
<html class="gallery-html">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>The Create Mod - Gallery</title>
        <link rel="stylesheet" href="style.css" />
        <script src="https://kit.fontawesome.com/8d3778546a.js" crossorigin="anonymous"></script>
        <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;600;700&display=swap" rel="stylesheet" />
        <script defer src="app.js"></script>
    </head>
    <body>
        <div class="container home">
            <section class="home">
                <nav class="nav-2">
                    <a href="boxmodel.html"><img class="nottobescaled" src="images/logo.png" alt="The Create 'Rise and Shine' Logo" class="image-1" /></a>
                    <div class="nav-links-2" id="navLinks-2">
                        <i class="fa-solid fa-xmark" style="color: #fffcdd" onclick="hideMenu()"></i>
                        <ul>
                            <li><a href="boxmodel.html">HOME</a></li>
                            <li><a href="about.html">ABOUT</a></li>
                            <li><a href="https://github.com/Creators-of-Create/Create">THE GITHUB</a></li>
                            <li><a href="https://github.com/Creators-of-Create/Create/wiki">OFFICIAL WIKI</a></li>
                            <li><a href="https://create.fandom.com/wiki/Create_Mod_Wiki">UNOFFICIAL WIKI</a></li>
                        </ul>
                    </div>
                    <i class="fa-solid fa-bars" style="color: #fffcdd" onclick="showMenu()"></i>
                </nav>
            </section>
            <script>
                var navLinks = document.getElementById("navLinks");

                function showMenu() {
                    navLinks.style.right = "0";
                }

                function hideMenu() {
                    navLinks.style.right = "-200px";
                }
            </script>
            <section class="home onlytexthome image-slide-parent">
                <img class="image-slide" src="images/gallery-1.png" />
                <!-- <div class="image-blank-div"></div> -->
                <div class="content">
                    <h1>
                        Beautiful.
                        <br />
                        <span class="styled-text">Aesthetics</span>
                    </h1>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi esse ullam enim vero blanditiis nostrum unde, eius inventore provident voluptates voluptatibus molestiae dolorum, nobis maxime doloribus quae nesciunt delectus error!</p>
                </div>
            </section>
        </div>
    </body>
</html>

CSS代码

.nav-2 img {
    transition: transform 0.5s ease-in-out;
    width: 150px;
}
.nav-2 img:hover {
    transform: scale(1.2);
}
.nav-2 i {
    display: none;
}
.nav-2 {
    display: flex;
    padding: 2% 6%;
    justify-content: space-between;
    align-items: center;
}
.nav-links-2 {
    flex: 1;
    text-align: right;
}
.nav-links-2 ul li {
    list-style: none;
    display: inline-block;
    padding: 8px 12px;
    position: relative;
}
.nav-links-2 ul li a {
    color: white;
    text-decoration: none;
    font-size: 13px;
}
.nav-links-2 ul li::after {
    content: "";
    width: 0%;
    height: 2px;
    background: var(--orange-wheel-hex);
    display: block;
    margin: auto;
    transition: 0.5s;
}
.nav-links-2 ul li:hover::after {
    width: 100%;
    border-radius: 50px;
}
.onlytexthome {
    padding: 100px 200px;
}
.gallery-html {
    position: relative;
    display: block;
    width: 100%;
}
.gallery-html ul li a {
    color: black;
}
.home {
    position: relative;
    display: flex;
    justify-content: center;
    flex-direction: column;
    background: var(--xanthous-hex);
    z-index: 10;
    background-image: url(images/);
}
.home .image-slide-parent img {
    z-index: 0;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background-size: cover;
}
.onlytexthome p {
    padding-left: 0;
    color: black;
    font-weight: 600;
}
.onlytexthome h1 {
    font-size: 4em;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 5px;
    line-height: 75px;
    margin-bottom: 40px;
}
.content {
    z-index: 1;
}
.container .home {
    z-index: 1;
}

我尝试将其改为背景图,但图片出现重复,分别在导航栏和主体区域各显示一次。我期望图片能覆盖整个屏幕,也尝试过调整z-index,但似乎没有效果。


解决方案

问题根源

  1. 多个元素(.container、两个section)都加了.home类,而.home设置了背景图,导致每个带.home的元素都显示背景图,出现重复。
  2. 导航栏所在的section.home有背景色,遮挡了底层背景图;同时绝对定位的图片被.home的z-index:10盖住。
  3. JS中获取的ID与HTML实际ID不匹配,菜单切换功能失效。

修复步骤

  1. 移除重复的.home类:只给最外层的.container保留.home类,去掉两个section上的.home类,避免多元素重复加载背景图。
  2. 设置全屏背景图:给.home(最外层容器)设置正确的背景图路径,添加background-size: cover、background-position: center、min-height: 100vh确保覆盖全屏且不重复。
  3. 让导航栏背景透明:移除导航栏所在section的背景色,确保底层背景图能透出来;给导航栏设置更高z-index,确保在背景图上方。
  4. 清理冗余元素:移除<img class="image-slide">标签,避免和背景图冲突。
  5. 修复JS ID错误:将getElementById("navLinks")改为getElementById("navLinks-2"),确保菜单切换功能正常。

修改后的代码示例

修改后的HTML(关键部分)

<div class="container home">
    <!-- 去掉section的home类 -->
    <section>
        <nav class="nav-2">
            <!-- 导航栏内容不变 -->
        </nav>
    </section>
    <!-- 去掉section的home类,移除img标签 -->
    <section class="onlytexthome image-slide-parent">
        <div class="content">
            <!-- 内容不变 -->
        </div>
    </section>
    <script>
        // 修正ID匹配问题
        var navLinks = document.getElementById("navLinks-2");

        function showMenu() {
            navLinks.style.right = "0";
        }

        function hideMenu() {
            navLinks.style.right = "-200px";
        }
    </script>
</div>

修改后的CSS(关键部分)

/* 修改.home样式,设置全屏背景图 */
.home {
    position: relative;
    display: flex;
    justify-content: center;
    flex-direction: column;
    background: transparent;
    min-height: 100vh;
    /* 补充正确的背景图路径 */
    background-image: url(images/gallery-1.png);
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    z-index: 1;
}

/* 确保导航栏背景透明且层级正确 */
.nav-2 {
    background: transparent;
    position: relative;
    z-index: 10;
}

/* 确保内容在背景图上方 */
.content {
    position: relative;
    z-index: 10;
}

内容的提问来源于stack exchange,提问作者Coiny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 04:57:42