固定标题与内容重叠,无法用填充或边距对其进行固定

时间:2019-07-19 15:49:46

标签: html css

我正在尝试构建一个带有position:fixed的简单导航标题,然后在其下面跟随内容(div),有人知道如何解决此问题吗?

我知道position:fixed将标头从DOM流中移出,所以我尝试将padding-top应用于主体,并将margin top应用于随后的任何div,但它与标头一起移动仍然会导致重叠

HTML `     

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="navbar.css">
</head>
<body>
    <div class="nav-container clearfix">
    <ul>
        <li><a class="active" href=#>HTML</a></li>
        <li><a href=#>CSS</a></li>
        <li><a href=#>JAVASCRIPT</a></li>
        <li><a href=#>PHP</a></li>
    </ul>
    </div>
    <div class="second">
            JAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPT
    </div>
</body>

`

CSS

*{
    box-sizing: border-box;
    font-family: "Segoe UI",Arial,sans-serif;

}
body{
    height:3000px;
    margin:0;
    padding:0;
    padding-top:51.1px;
} 
.nav-container{
    width:100%;
    padding:0;
    background-color:#5f5f5f;
    position:fixed;
    box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 2px 10px 0 rgba(0,0,0,0.12);
    z-index:1;

}
.clearfix {
    content: "";
    display: table;
    clear: both;
}
ul {
    list-style-type: none;
    text-align: center;
    margin: 0;

}
li {
    float:left;
}
li a{
    display:block;
    color:#f1f1f1;
    padding: 14px 16px;
    text-decoration: none;
    text-align:center;
    font-size: 17px;
}
.active{
    background-color: mediumseagreen;
}

li a:hover:not(.active) {
    background-color: black;
}
.second{
    background-color: aqua;
    display:block;
    width:100%;
    height:2000px;
    z-index:10000; /*doesnt work bec. element not positioned*/
}

仅预期在应用填充或边距时标题之后的内容。

2 个答案:

答案 0 :(得分:1)

您期望这样吗

<style>
*{
    box-sizing: border-box;
    font-family: "Segoe UI",Arial,sans-serif;
}
body{
    height:3000px;
    margin:0;
    padding:0;
    padding-top:51.1px;
} 
.nav-container{
    width:100%;
    padding:0;
    background-color:#5f5f5f;
    position:fixed;
    box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 2px 10px 0 rgba(0,0,0,0.12);
    z-index:1;
}
.clearfix {
    content: "";
    display: table;
    clear: both;top:0;
}
ul {
    list-style-type: none;
    text-align: center;
    margin: 0;
}
li {
    float:left;
}
li a{
    display:block;
    color:#f1f1f1;
    padding: 14px 16px;
    text-decoration: none;
    text-align:center;
    font-size: 17px;
}
.active{
    background-color: mediumseagreen;
}

li a:hover:not(.active) {
    background-color: black;
}
.second{
    background-color: aqua;
    display:block;
    width:90%;
    margin:5%;
    height:2000px;
    word-break: break-word;
    }
<div class="nav-container clearfix">
    <ul>
        <li><a class="active" href=#>HTML</a></li>
        <li><a href=#>CSS</a></li>
        <li><a href=#>JAVASCRIPT</a></li>
        <li><a href=#>PHP</a></li>
    </ul>
</div>

<div class="second">                          
     JAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPTJAVASCRIPT
</div>

答案 1 :(得分:0)

如果您只想将菜单保留在页面顶部,请删除position:fixed;。 https://jsfiddle.net/mkLan96o/

.nav-container{
width:100%;
padding:0;
background-color:#5f5f5f;

box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 2px 10px 0 rgba(0,0,0,0.12);
z-index:1;

如果要滚动菜单,请使用引导程序。