简体   繁体   English

div的高度不会改变CSS

[英]Height of a div doesn't change CSS

I'm working on a animated navigation bar. 我正在制作动画导航栏。 Now I want to make the height of the div 0px, so that if I click on a button, the menu will pop-out. 现在,我想将div的高度设置为0px,这样,如果单击按钮,将弹出菜单。 But the height stays the same if I change it to 0px: 但是如果我将高度更改为0px,则高度保持不变:

I hope someone could help me. 我希望有人可以帮助我。

HTML: HTML:

<div id="mobileMenu">
    <a href="#"> <h1> Home </h1> </a>
    <a href="#"> <h1> Blog </h1> </a>
    <a href="#"> <h1> Lorem ipsum </h1> </a>
    <a href="#"> <h1> Lorem ipsum </h1> </a>
    <a href="#"> <h1> Contact </h1> </a>
</div>

CSS: CSS:

#mobileMenu {
    position: fixed;
    height: 0px;
    z-index: 10;
    top: 0;
    margin-top: 50px;
    width: 100%;
}

#mobileMenu a {
    text-decoration: none;
    color: white;
}

#mobileMenu h1 {
    padding: 20px 0px;
    font-size: 15px;
    background-color: white;
    color: black;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    font-family: OpenSans-Regular;
    padding-left: 15%;
}

You need to set overflow: hidden; 您需要设置overflow: hidden; . The height is 0 but the content is overflowing, and overflow is visible by default. 高度为0但内容溢出,并且默认情况下visible overflow

 div { height: 0; overflow: hidden; } 
 <div id="mobileMenu"> <a href="#"> <h1> Home </h1> </a> <a href="#"> <h1> Blog </h1> </a> <a href="#"> <h1> Lorem ipsum </h1> </a> <a href="#"> <h1> Lorem ipsum </h1> </a> <a href="#"> <h1> Contact </h1> </a> </div> 

You can also use a CSS rule: set in #mobileMenu display:none and via JS change with display:block dynamically 您还可以使用CSS规则:在#mobileMenu display:none设置,并通过JS使用display:block动态更改

 #mobileMenu { position: fixed; display: none; height: 0; z-index: 10; top: 0; margin-top: 50px; width: 100%; } #mobileMenu a { text-decoration: none; color: white; } #mobileMenu h1 { padding: 20px 0px; font-size: 15px; background-color: white; color: black; border-bottom: 1px solid rgba(0, 0, 0, 0.05); font-family: OpenSans-Regular; padding-left: 15%; } 
 <div id="mobileMenu"> <a href="#"> <h1> Home </h1> </a> <a href="#"> <h1> Blog </h1> </a> <a href="#"> <h1> Lorem ipsum </h1> </a> <a href="#"> <h1> Lorem ipsum </h1> </a> <a href="#"> <h1> Contact </h1> </a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM