CSS位置粘滞

时间:2019-11-09 21:16:14

标签: html css

如何使这些菜单显示在铭文菜单下的一行中,而不显示在另一行下面?并且菜单应该始终在顶部可见。

function menux() {
    var x = document.getElementById("menu");
    if (x.style.display === "none") {
        x.style.display = "block";
    }
    else {
        x.style.display = "none";
    }
}
nav {
    top: 0;
    font: 15px verdana;
    background-color: #368a9a;
    width: 15%;
    height: 100%;
    position:sticky;         
}
ul {
    list-style-type: none;
    overflow: hidden;
    padding: 0;
    margin: 0;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>
<body>
    <nav>
        <h3 onclick="menux()">Menu</h3>
        <ul id="menu">
            <li><a class="active" href="#home">Home</a></li>
            <li><a href="#news">News</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#about">About</a></li>
        </ul>
    </nav>

    <section>
        <div> test</div>
        <div> test</div>
        <div> test</div>        
        <div> test</div>
        <div> test</div>
        <div> test</div>        
    </section>
</body>
</html>

-编辑后 我添加了一段代码,我指望有人来帮助我

1 个答案:

答案 0 :(得分:0)

使li元素内联:

function menux() {
    var x = document.getElementById("menu");
    if (x.style.display === "none") {
        x.style.display = "block";
    }
    else {
        x.style.display = "none";
    }
}
nav {
    top: 0;
    font: 15px verdana;
    background-color: #368a9a;
    width: 15%;
    height: 100%;
    position:sticky;         
}
ul {
    list-style-type: none;
    overflow: hidden;
    padding: 0;
    margin: 0;
}
li {
    display: inline;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>
<body>
    <nav>
        <h3 onclick="menux()">Menu</h3>
        <ul id="menu">
            <li><a class="active" href="#home">Home</a></li>
            <li><a href="#news">News</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#about">About</a></li>
        </ul>
    </nav>

    <section>
        <div>test</div><div>test</div><div>test</div><div>test</div><div>test</div>
        <div>test</div><div>test</div><div>test</div><div>test</div><div>test</div>
        <div>test</div><div>test</div><div>test</div><div>test</div><div>test</div>
        <div>test</div><div>test</div><div>test</div><div>test</div><div>test</div>
        <div>test</div><div>test</div><div>test</div><div>test</div><div>test</div>
        <div>test</div><div>test</div><div>test</div><div>test</div><div>test</div>
        <div>test</div><div>test</div><div>test</div><div>test</div><div>test</div>
    </section>
</body>
</html>