简体   繁体   English

CSS下拉响应子菜单

[英]CSS dropdown responsive sub menu

I am newbie and learning about CSS code. 我是新手,正在学习CSS代码。 I want to add the dropdown submenu in the dashboard menu but I don't have exprience about css dropdown responsive menu and don't know how to apply this, bellow are the code that I found from other resources and I add the dashboard menu and the submenu inside it. 我想在仪表板菜单中添加下拉子菜单,但是我对css下拉响应菜单不了解,也不知道如何应用,下面是我从其他资源中找到的代码,并添加了仪表板菜单和其中的子菜单。 This code can be run in mobile. 该代码可以在移动设备中运行。

When I add the dropdown submenu inside the dashboard, The css do not disappear when I navigate it. 当我在仪表板内添加下拉子菜单时,导航时CSS不会消失。 Here are the codes: 以下是代码:

 /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */ function myFunction() { document.getElementsByClassName("topnav")[0].classList.toggle("responsive"); } 
 /* Remove margins and padding from the list, and add a black background color */ ul.topnav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } /* Float the list items side by side */ ul.topnav li {float: left;} /* Style the links inside the list items */ ul.topnav li a { display: inline-block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; transition: 0.3s; font-size: 17px; } /* Change background color of links on hover */ ul.topnav li a:hover {background-color: #111;} /* Hide the list item that contains the link that should open and close the topnav on small screens */ ul.topnav li.icon {display: none;} @media screen and (max-width:680px) { ul.topnav li:not(:first-child) {display: none;} ul.topnav li.icon { float: right; display: inline-block; } } @media screen and (max-width:680px) { ul.topnav.responsive {position: relative;} ul.topnav.responsive li.icon { position: absolute; right: 0; top: 0; } ul.topnav.responsive li { float: none; display: inline; } ul.topnav.responsive li a { display: block; text-align: left; } } 
 <ul class="topnav"> <li><a 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> <li> <a href="#dashboard">Dashboard</a> <ul class="hidden"> <li><a href="#">Profile</a></li> <li><a href="#">Change Password</a></li> <li><a href="#">Other</a></li> </ul> </li> <li class="icon"> <a href="javascript:void(0);" onclick="myFunction()">☰</a> </li> </ul> 

I show you here how I meant with a parent structure. 我在这里向您展示parent结构的含义。

:hover is just for demo case. :hover仅用于演示案例。

You can change to Javascript click . 您可以更改为Java click

 /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */ function myFunction() { document.getElementsByClassName("topnav")[0].classList.toggle("responsive"); } 
 /* Remove margins and padding from the list, and add a black background color */ ul.topnav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } /* Float the list items side by side */ ul.topnav li {float: left;} /* Style the links inside the list items */ ul.topnav li a { display: inline-block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; transition: 0.3s; font-size: 17px; } /* Change background color of links on hover */ ul.topnav li a:hover {background-color: #111;} /* Hide the list item that contains the link that should open and close the topnav on small screens */ ul.topnav li.icon {display: none;} .hidden{display:none} #dashboard:hover + .hidden{display:block !important} .hidden{display:none} .upper:hover .hidden{display:block} @media screen and (max-width:680px) { ul.topnav li:not(:first-child) {display: none;} ul.topnav li.icon { float: right; display: inline-block; } } @media screen and (max-width:680px) { ul.topnav.responsive {position: relative;} ul.topnav.responsive li.icon { position: absolute; right: 0; top: 0; } ul.topnav.responsive li { float: none; display: inline; } ul.topnav.responsive li a { display: block; text-align: left; } } 
 <ul class="topnav"> <li><a 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> <li class="upper"><a href="#dashboard">Dashboard</a> <ul class="hidden"> <li><a href="#">Profile</a></li> <li><a href="#">Change Password</a></li> <li><a href="#">Other</a></li> </ul> </li> <li class="icon"> <a href="javascript:void(0);" onclick="myFunction()">☰</a> </li> </ul> 

 /*Style for dropdown links*/ .topnav>li:hover ul a { color: #fff; height: 40px; line-height: 40px; } /*Hover state for dropdown links*/ .topnav>li:hover ul a:hover { color: #fff; } /*Hide dropdown links until they are needed*/ .topnav>li ul { display: none; } /*Make dropdown links vertical*/ .topnav>li ul li { display: block; float: none; } /*Prevent text wrapping*/ .topnav>li ul li a { width: auto; min-width: 100px; padding: 0 20px; } /*Display the dropdown on hover*/ .topnav>ul li a:hover+.hidden, .hidden:hover { display: block; } 

The sub menu became vertical but the background black color is too long: Below are the image of my problem: dropdownmenucss 子菜单变为垂直,但背景黑色颜色太长:以下是我的问题的图像: dropdownmenucss

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

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