【问题标题】:Any ideas why my sticky menu stopped sticking?知道为什么我的粘性菜单不再粘了吗?
【发布时间】:2022-12-14 11:03:26
【问题描述】:

我真的很抱歉问这个问题,但我一直在寻找这段代码中的错误大约 1.5 小时,我受够了。

这可能是非常愚蠢和明显的事情。

练习 IT 资格考试。

成功搭建了一个置顶菜单,用了一段时间。

在将下一个元素添加到我的网站后,尽管在 css 中具有所有需要的属性,但它实际上不再具有粘性。有任何想法吗?

<!DOCTYPE html>
    <html lang="pl">
        <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">
            <link rel="stylesheet" href="style.css">
            <title>omaga</title>
        </head>
        <body>
            <div class="main">
                <div class="header">
                    <h1 class="header1">header</h1>
                </div>
                <div class="menu">
                        <button class="button_menu" onclick="">
                            <img class="menu_icon" src="menuicons/menu.png" alt="menu icon">
                        </button>
                        <button class="button_menu" onclick="">
                            <img class="menu_icon" src="menuicons/menu.png" alt="menu icon">
                        </button>
                </div>
                <div class="content">
                    <div class="left">
                        312312312
                    </div>
                    <div class="centerright">
                        <div class="center">
                            2312312
                        </div>
                        <div class="right">
                            123123
                        </div>
                        <div class="bottom">
                            najs
                        </div>
                    </div>
                </div>
            </div>
        </body>
    </html>
* {
    
    background-color: rgb(30, 33, 38);
    color: rgb(230, 229, 229);
    font-size: 40px;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    font-weight: Italic;
    font-variant: common-ligatures tabular-nums;
    text-align: center;
    text-shadow: rgb(26, 26, 26) 2px 2px 2px;
    margin: 0%;
}
.main{
    width:100%;
    height:auto;
}
.header {

    width: 98.8%;
    margin-top: 10px;
    margin-left: auto;
    margin-right: auto;
    background-color: rgb(27, 31, 36);
    height: auto;
    margin-bottom: 10px;
    padding-top: 20px;
    padding-bottom: 20px;
}
.header1{

    background-color: rgba(0, 0, 0, 0);
}
.menu{

    z-index: 9999;
    position: sticky;
    top: -1px;  
    width: 98.8%;
    height: 70px;
    background-color: rgb(35, 38, 44);
    margin-bottom: 10px;
    margin-left: auto;
    margin-right: auto;
    box-shadow: 2px 2px 3px rgba(36, 36, 36, 0.451);

}
.button_menu{
    position: relative;
    float: left;
    background-color: rgb(41, 44, 49);
    border: none;
    width: 70px;
    height: 100%;
    padding-left: auto;
    padding-right: auto;
}
.menu_icon {

    float:left;
    width: 100%;
    height: 70%;
    background-color: rgba(0, 0, 0, 0);

}
.content{
    clear: both;
    width:98.8%;
    height: auto;
    box-shadow: 2px 2px 3px rgba(36, 36, 36, 0.451);
    margin-left: auto;
    margin-right: auto;

}
.left {

    position: relative;
    float: left;
    width: 40%;
    height: 4000px;
    background-color: rgb(47, 50, 56);
    box-shadow: -2px 2px 3px rgba(36, 36, 36, 0.451);
}
.centerright {

    float: left;
    width: 60%;
    height: 4000px;
    background-color: rgba(0, 0, 0, 0);

}
.center {

    position: relative;
    float: left;
    margin-right: 0px;
    width: 55%;
    height: 1500px;
    background-color: rgb(47, 50, 56);
    box-shadow: 2px -1px 3px rgba(36, 36, 36, 0.451);

}   
.right {

    position: relative;
    float: right;
    width: 45%;
    margin-left: 0px;
    margin-right: 0px;
    height: 1500px;
    background-color: rgb(47, 50, 56);
    box-shadow: 2px -1px 3px rgba(36, 36, 36, 0.451);

}
.bottom{

    clear: both;
    width: auto;
    margin-left: 0%;
    margin-right: 0%;
    height: 500px;
    background-color: rgb(47, 50, 56);
    box-shadow: 2px 2px 3px rgba(36, 36, 36, 0.451);


}

【问题讨论】:

    标签: html css menu sticky


    【解决方案1】:

    你的.main elements height 不够大。您应该将 min-hight 添加到您的 .main 元素。 尝试这个:

    .main{
      min-height:100vh;
    }
    

    并且您需要关闭 .main 元素的 close-tag 您希望 sticky menu 停止的位置。 换句话说,您的.main 元素应该包含页面中的所有元素。

    【讨论】:

    • 天哪,谢谢你,我正要跳出窗外。你是最好的我爱你
    猜你喜欢
    • 2010-12-31
    • 2018-05-19
    • 2021-08-03
    • 2014-10-05
    • 2018-05-24
    • 2019-01-12
    • 1970-01-01
    • 2023-02-07
    相关资源
    最近更新 更多