close dropdown menu on click pure css

2019-08-06 09:55发布

i have a menu bar with dropdown menu and submenu using pure css..my problem is how to close/collapse the dropdown when i click on the dropdown or the submenu..this is the sample i made

this is the html

<div id="headermenu">
    <ul >
        <li id=""><a href="#">menu1</a></li>
        <li id=""><a href="#">menu2</a>
            <ul class="">
                <li><a href="#">submenu1</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                        <li><a href="#">submenu.3</a></li>
                        <li><a href="#">submenu.4</a></li>
                    </ul>
                </li>
                <li><a href="#">submenu2</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                        <li><a href="#">submenu.3</a></li>
                    </ul>
                </li>
                <li><a href="#">submenu3</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                    </ul>
                </li>
                <li><a href="#">submenu4</a></li>
                <li><a href="#">submenu5</a></li>
                <li><a href="#">submenu6</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                        <li><a href="#">submenu.3</a></li>
                    </ul>
                </li>
                <li><a href="#">submenu7</a></li>
                <li><a href="#">submenu8</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                    </ul>
                </li>
            </ul>
        </li>           
        <li id=""><a href="#">menu3</a>
            <ul>
                <li><a href="#">submenu1</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                    </ul>
                </li>
                <li><a href="#">submenu2</a></li>
            </ul>
        </li>
        <li id="menutourists"><a href="#">menu4</a>
            <ul>
                <li><a href="#">submenu1</a></li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu3</a></li>
                <li><a href="#">submenu4</a></li>
                <li><a href="#">submenu5</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                    </ul>
                </li>
                <li><a href="#">submenu6</a></li>
            </ul>
        </li>
        <li id="menuresidents"><a href="#">menu5</a>
            <ul>
                <li><a href="#">submenu1</a>
                    <ul>
                        <li><a href="#">submenu.1</a></li>
                        <li><a href="#">submenu.2</a></li>
                        <li><a href="#">submenu.3</a></li>
                    </ul>
                </li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu3</a></li>
                <li><a href="#">submenu4</a></li>
            </ul>
        </li>
    </ul>   
</div>

this is the css

#headermenu{
    width: 90%;
    height: 20%;
    margin-right:5%;
    margin-left:5%;
    margin-bottom: .5%;
}
#headermenu ul ul {
    display: none;
}
#headermenu ul li:hover > ul {
    display: block;
}
#headermenu ul{
    padding:0; 
    margin:0; 
    white-space:nowrap;
    list-style: none;
    position: relative;
    display: inline-table;
}
#headermenu ul li {
    width:100px;
    height:45px;
    float:left;
    left:0;
    text-decoration:none;
    text-align:center;
    font-family:century gothic;
    background-color:#fef3e2;
}
#headermenu ul li:hover {
    background: #feaa38;
}
#headermenu ul li:hover a {
    color: #ffffff;
}
#headermenu ul li a {
    color:#feaa38;
    display: block;
    text-decoration: none;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}   
#headermenu ul ul {
    border-radius: 0px; 
    padding: 0;
    position: absolute; 
    top: 100%;
}
#headermenu ul ul li {
    float: none; 
    background: #b0c7bd;
    position: relative;
}
#headermenu ul ul li:hover {
    background: #aea7a0;
}
#headermenu ul ul li a {
    color: #ffffff;
}   
#headermenu ul ul li a:hover {
    background: #aea7a0;
}   
#headermenu ul ul ul {
    position: absolute; 
    left: 100%; 
    top:0;
}       

2条回答
别忘想泡老子
2楼-- · 2019-08-06 10:29

I think this is not possible and for this purpose you should use javascript. CSS for some actions is not the best solution.

查看更多
不美不萌又怎样
3楼-- · 2019-08-06 10:31

add this (Demo)

 #headermenu:active ul ul *{
        display:none;
    }
查看更多
登录 后发表回答