如何使导航栏固定在页面顶部并且不重叠

时间:2018-09-03 19:32:49

标签: html css html5

所以我还是一个初学者,我的问题对其他人来说可能很愚蠢,但是,我需要解决此问题,所以这是我的html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test</title>
    <link href="test.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div id="first">
        <h1>Test Page</h1>
        <div id="header">
          <ul id="nav">
                <li class="head"><a href="http://">Home</a></li>
                <li class="head"><a href="http://">about</a></li>
                <li class="head"><a href="http://">Contact</a></li>
                <li class="head"><a href="http://">Our products</a></li>
            </ul>
        </div>

    </div>

    <div id = "p">
        <p>Libero cupiditate tempore autem animi iusto, expedita neque voluptatibus dolor sequi quia nihil quam magnam, magni praesentium nam aut ipsum soluta nulla et, eum sed? Explicabo modi similique nobis nesciunt!</p>
        <p>Itaque, corrupti quae corporis excepturi eum vero, rem aliquam dolorem, adipisci maxime deleniti. Dicta beatae voluptatum maiores nesciunt, ducimus eveniet. Reprehenderit doloremque, laboriosam repellat rerum adipisci dolores quia sapiente cupiditate!</p>
        <p>Voluptatibus tenetur sit tempore inventore optio nihil nostrum sapiente neque, odio ipsa atque? Tenetur earum voluptatum omnis dignissimos dolores reprehenderit sint quo ducimus, cumque inventore laboriosam asperiores laborum nemo perferendis.</p>
        <p>Minima ab blanditiis, rerum voluptas dicta ducimus, voluptates itaque reprehenderit numquam mollitia possimus quod amet harum non eius perferendis iste saepe rem maiores est. Similique minus consequatur reiciendis voluptatem quibusdam.</p>
        <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Magni minus aspernatur ullam eligendi illum laboriosam, quas omnis facilis voluptatum quis iste ipsam ut aliquam praesentium dolorem. Fugit assumenda nobis distinctio.</p>
    </div>
<footer> <p>Posted by: Hege Refsnes</p>
    <p>Contact information: <a href="mailto:someone@example.com">
    Personal Email</a>.</p>
</footer>
</body>
</html>

导航栏与段落重叠,正如您注意到的那样,我占据了#header fixed的位置,而我占据了#first top:0; 我无法弄清楚问题,如果我要创建一个侧边栏,如何防止其与其他元素重叠,甚至如何防止元素与其他元素重叠?

 #first{
    top:0;
}
#header{
    position: fixed;

}
#nav{

        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: lightcoral;

        border: lightcoral solid;
        border-top-left-radius: 0.5rem;
        border-top-right-radius: 0.5rem;

}
#nav li{
    float: left;
    padding: 2rem 2rem;
}

#nav li a {

    display: inline-block;
    color: white;
    text-align: center;
    text-decoration: none;
}
#nav li a:hover {

    color: dimgray;

}


#nav li:hover {
    border: (214, 20, 124) solid;
    background-color:rgb(214, 20, 124);
    color: dimgray;
    border-radius: 0.5rem;
}
#nav li:last-child{
    float: right;
}

2 个答案:

答案 0 :(得分:3)

<nav class="navbar navbar-default"></nav> <!-- Dummy nav bar -->
<nav class="navbar navbar-default navbar-fixed-top"> <!-- Real nav bar -->
    <!-- Nav bar details -->
</nav>

这是可以解决您问题的引导程序类。照原样使用

有关更多信息,请阅读

  

Blockquote   https://getbootstrap.com/docs/4.0/components/navbar/

答案 1 :(得分:1)

#first{
    top:0;
    position: fixed;
    background-color:white;
    width:100%;
}
#header{
    position: fixed;
    width:98%;
}
#nav{

        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: lightcoral;
        width:100%;

        border: lightcoral solid;
        border-top-left-radius: 0.5rem;
        border-top-right-radius: 0.5rem;

}
#nav li{
    float: left;
    padding: 2rem 2rem;
}

#nav li a {

    display: inline-block;
    color: white;
    text-align: center;
    text-decoration: none;
}
#nav li a:hover {

    color: dimgray;

}


#nav li:hover {
    border: (214, 20, 124) solid;
    background-color:rgb(214, 20, 124);
    color: dimgray;
    border-radius: 0.5rem;
}
#nav li:last-child{
    float: right;
}
#p{
    margin-top:200px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test</title>
    <link href="test.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div id="first">
        <h1>Test Page</h1>
        <div id="header">
          <ul id="nav">
                <li class="head"><a href="http://">Home</a></li>
                <li class="head"><a href="http://">about</a></li>
                <li class="head"><a href="http://">Contact</a></li>
                <li class="head"><a href="http://">Our products</a></li>
            </ul>
        </div>

    </div>

    <div id = "p">
        <p>Libero cupiditate tempore autem animi iusto, expedita neque voluptatibus dolor sequi quia nihil quam magnam, magni praesentium nam aut ipsum soluta nulla et, eum sed? Explicabo modi similique nobis nesciunt!</p>
        <p>Itaque, corrupti quae corporis excepturi eum vero, rem aliquam dolorem, adipisci maxime deleniti. Dicta beatae voluptatum maiores nesciunt, ducimus eveniet. Reprehenderit doloremque, laboriosam repellat rerum adipisci dolores quia sapiente cupiditate!</p>
        <p>Voluptatibus tenetur sit tempore inventore optio nihil nostrum sapiente neque, odio ipsa atque? Tenetur earum voluptatum omnis dignissimos dolores reprehenderit sint quo ducimus, cumque inventore laboriosam asperiores laborum nemo perferendis.</p>
        <p>Minima ab blanditiis, rerum voluptas dicta ducimus, voluptates itaque reprehenderit numquam mollitia possimus quod amet harum non eius perferendis iste saepe rem maiores est. Similique minus consequatur reiciendis voluptatem quibusdam.</p>
        <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Magni minus aspernatur ullam eligendi illum laboriosam, quas omnis facilis voluptatum quis iste ipsam ut aliquam praesentium dolorem. Fugit assumenda nobis distinctio.</p>
    </div>
<footer> <p>Posted by: Hege Refsnes</p>
    <p>Contact information: <a href="mailto:someone@example.com">
    Personal Email</a>.</p>
</footer>
</body>
</html>

我注意到您没有为任何这些元素定义z-index。尝试修改此元素上的CSS,让我知道这是否是您正在寻找的解决方案:

#first{
    top:0;
    z-index: 9998;
}
#header{
    position: fixed;
    z-index: 9999;
}

这是屏幕截图1