图像在固定标题上可见

sam*_*rma 2 html css

我创建了一个标题,position: fixed在下面我用图像替换添加了一个标识.但是,当我向下滚动时,图像会标题上移动而不是标题移动.

如何让固定标题始终位于顶部(即强制图像在其下移动)?

这是我的代码的一部分:

HTML

<header>
    <nav>
        <ul>
            <li>Home</li>
            <li>About</li>
            <li>Work</li>
        </ul>
    </nav>
</header>

<div class="banner">
    <h1><img src="imgs\logo.png" />Jackle</h1>
    <h3>We design and create.</h3>
</div>

<img src="" class="representationBannerPic">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis recusandae ullam dolores sint eligendi voluptatibus incidunt esse vero totam aspernatur. Iste quae molestias sed enim corrupti rerum nam culpa asperiores.</p>
Run Code Online (Sandbox Code Playgroud)

CSS

header
{
    background-color: black;
    width: 100%;
    height: 47px;
    color: white;
    position: fixed;
}
.banner
{
    background-color: #3b97b1;
    background-color: #5fa1b4;
    height: 247px;
    width: 100%;
}
.banner h1 
{
    text-align: center;
    position: relative;
    top: 100px;
    text-indent: -9999px;
}
.banner h1 img
{
   position: absolute;
   top: 50%;
   left: 50%;
   width: 200px;
   height: 60px;
   margin-top: -30px; /* Half the height */
   margin-left: -100px; /* Half the width */
   padding-top: 20px;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要查看更多内容,可以在此处找到整个代码.

Tom*_*Tom 14

将以下规则添加到标题样式:

header
{
    /* ... */
    z-index: 99;
}
Run Code Online (Sandbox Code Playgroud)


z-index属性指定定位元素的叠加顺序.z-index因此,具有更大元素的元素总是在具有较低元素的元素的前面z-index.将z-index值设置为大于(或甚至等于)0会将该元素设置在未定位的非定位元素之上z-index.您可以始终将其设置为低于99的值,但是,通常的做法是避免与其他定位元素的潜在冲突.