如何仅使用 css 为垂直导航栏创建子菜单
Posted
技术标签:
【中文标题】如何仅使用 css 为垂直导航栏创建子菜单【英文标题】:How to create sub menus for a vertical navigation bar using css only 【发布时间】:2013-10-09 01:13:23 【问题描述】:我已经浏览了很长时间才知道如何为我的网站的垂直导航页面创建子菜单。
我想要使用简单的 CSS。
我的 nav.jsp 页面包含所有导航链接。相同的代码如下
<table cellspacing="0" cellpadding="0" >
<tr>
<td bgcolor="#D8D8D8" valign="top">
<ul id="nav">
<li><a href="home.jsp"> Home</a>
</li>
<li><a href="addCompanyDetails.jsp"> Add Company Details</a>
</li>
<li><a href="modifyCompanyDetails.action"> Modify Company Details</a>
<ul id = "subMenu">
<li>Sub Menu 1
</li>
<li>Sub Menu 2
</li>
<li>Sub Menu 3
</li>
<li>Sub Menu 4
</li>
</ul>
</li>
<li><a href="deleteCompanyDetails.action"> Delete Company Details</a>
</li>
<li><a href="search.jsp"> Search</a>
</li>
<li><a href="viewAllDetails.action"> View All Details</a>
</li>
</ul>
</td>
</tr>
</table>
我已经为父链接编写了我的 CSS 代码。相同的CSS代码如下
#nav
padding: 0;
margin: 0;
width: 220px;
padding: 0px;
border: 0px;
margin: 0px;
#nav li
width: 100%;
display: block;
float: left;
height: 35px;
border-bottom: 1px solid #ffffff;
#nav li a
width: 100%;
display: block;
float: left;
height: 35px;
line-height: 35px;
text-decoration: none;
color: #000000;
#nav li a:hover
width: 100%;
display: block;
float: left;
height: 35px;
line-height: 35px;
background-color: #404040;
color: #ffffff;
没有显示子菜单的 CSS 代码。我只有 CSS 代码来显示父菜单。 将鼠标悬停在父菜单上时,应出现子菜单。
请帮帮我。如果有什么请回信。
【问题讨论】:
phrogz.net/css/WhyTablesAreBadForLayout.html 【参考方案1】:#submenu
display:none;
#nav li:hover > ul
display:block;
我建议子菜单使用 class 而不是 id,因为您可能想要多个。
【讨论】:
【参考方案2】:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sample </title>
<!--<link rel="stylesheet" href="Style.css" type="text/css" >-->
<style type="text/css" media="screen">
#horizontalmenu ul
padding:1;
margin:2;
list-style:none;
#horizontalmenu li
float:left;
position:relative;
padding-right:60;
display:block;
border:2px solid #FF0000;
border-style:outset;
#horizontalmenu li ul
display:none;
position:absolute;
#horizontalmenu li:hover ul
display:block;
background:#F9F2FF;
height:auto; width:14em;
#horizontalmenu li ul li
clear:both;
border-style:none;
</style>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top">
<form id="cse-search-box" action="home.html">
<table border="0" bgcolor="#FDFEFF">
<tr>
<td > </td>
<td ><!--<img src="Images/Laptop2.jpg" />--></td>
<td align="center"> </td>
<td ><table border="0">
<tr>
<td><font color="#663333"> Call our LapTop Expert</font></td>
</tr>
<tr>
<td ><font color="#666633"> +919855566689</font></td>
</tr>
<tr>
<td> </td>
</tr>
</table></td>
<td > </td>
</tr>
<tr>
<td colspan="4" >
<div id="horizontalmenu">
<ul> <li><a href="sample.html">Home</a></li>
<li><a href="#">Battery</a>
<ul><li><a href="Batterysample.html">HP Laptop Battery</a></li>
<li><a href="SonyBattery.html">Sony Laptop Battery</a></li>
<li><a href="DellBattery.html">Dell Laptop Battery</a></li>
<li><a href="LenovoBattery.html">Lenovo Laptop Battery</a></li></ul></li>
<li><a href="#">Adapter</a>
<ul> <li><a href="HP Battery.html">HP Laptop Adapter</a></li>
<li><a href="#">Sony Laptop Adapter</a></li>
<li><a href="#">Dell Laptop Adapter</a></li>
<li><a href="#">Lenovo Laptop Adapter</a></li>
<li><a href="#">Toshiba Laptop Adapter</a></li></ul></li>
<li><a href="#">Laptop Care</a>
<ul> <li><a href="Battery care.html">Battery Care Practices</a></li></ul></li>
<li><a href="#">Service Status</a></li>
</ul>
</div> </td>
<td align="right">
<input type="text" placeholder="Search" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td align="right">
<input type="submit" name="sa" value="Search" /> </td>
</tr>
<tr>
<td colspan="5" bgcolor="#F0F0F0" valign="bottom"><font face="Agency FB" size="+4" color="#FFA8A8">Dell Laptop Battery </font></td>
</tr>
<tr>
<td colspan="5"bgcolor="#FFA6A6"></td>
</tr>
<!--<tr>
<td colspan="5" ><font face="Agency FB" size="+2" color="#AAAAFF">Display </font>
<select><option value="5">5</option>
<option value="10" selected="selected">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="0">All</option>
</select>
</td>
</tr>-->
<tr>
<td colspan="5">
<p><font face="Agency FB" size="+2" color="#FF0000">Dell 14r Battery </font></p>
<p><font face="Agency FB" size="+2" color="#FF0000">Dell Inspiron 1464 Battery </font></p>
<p><font face="Agency FB" size="+2" color="#FF0000">Dell Inspiron 1525 Battery </font></p>
<p><font face="Agency FB" size="+2" color="#FF0000">Dell Inspiron M5010 9 cell Battery</font></p>
<p><font face="Agency FB" size="+2" color="#FF0000">Dell Inspiron N5010 Battery</font></p>
<p><font face="Agency FB" size="+2" color="#FF0000">Dell Latitude E5500 Battery </font></p>
<p><font face="Agency FB" size="+2" color="#FF0000">Dell Vostro 1500 Battery</font></p>
<p><font face="Agency FB" size="+2" color="#FF0000">Dell Vostro 1510/1520/1310 Battery </font></p>
</td>
</tr>
<tr>
<td colspan="5" > </td>
</tr>
<tr>
<td colspan="5" > </td>
</tr>
<tr>
<td colspan="5" bgcolor="#663300"></td>
</tr>
</table>
</form>
</td>
<tr bgcolor="#F4EAEA">
<td>
<table border="0">
<tr>
<td ><font face="Agency FB" color="#FF0000" size="5">Company Info</font></td>
<td ><font face="Agency FB" color="#FF0000" size="5">Terms & Conditions</font></td>
<td ><font face="Agency FB" color="#FF0000" size="5"></font></td>
<td ><font face="Agency FB" color="#FF0000" size="5">Bangalore</font></td>
</tr>
<tr>
<td bgcolor="#FCF8F8"></td>
<td bgcolor="#FCF8F8"></td>
<td bgcolor="#FCF8F8"></td>
<td bgcolor="#FCF8F8"></td>
</tr>
<tr>
<td ><a href="About.html"><font face="Times New Roman, Times, serif" color="#333300">About Us </font></a></td>
<td ><font face="Times New Roman, Times, serif" color="#333300">Privacy Policy </font></td>
<td rowspan="5"> </td>
<td rowspan="5" valign="top"> </td>
</tr>
<tr>
<td ><a href="Contact.html"><font face="Times New Roman, Times, serif" color="#333300">Enter Details To Get Service </font></a></td>
<td ><font face="Times New Roman, Times, serif" color="#333300">Collection Centers </font></td>
</tr>
<tr>
<td ><a href="Service Expertise.html"><font face="Times New Roman, Times, serif" color="#333300">Service Expertise </font></a></td>
<td ><font face="Times New Roman, Times, serif" color="#333300">Services</font></td>
</tr>
<tr>
<td ><font face="Times New Roman, Times, serif" color="#333300">Customer Reviews </font></td>
<td > </td>
</tr>
<tr>
<td > </td>
<td > </td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td colspan="4" bgcolor="#FF9393"></td>
</tr>
</table></td>
</tr>
</tr>
</table>
</body>
</html>
【讨论】:
解释你所做的改变。【参考方案3】:这应该适用于您的情况..
#nav #subMenu
display:none;
#nav li:hover #subMenu
display: block;
虽然你应该使用类而不是 id。 请记住 :hover 仅适用于 IE6 中的链接。
【讨论】:
【参考方案4】:您可以使用以下适合您的方法:
#nav #subMenu
display:none;
#nav li:hover #subMenu
display: block;
position:absolute;
left:228px;
background:#D8D8D8;
padding:0;
【讨论】:
【参考方案5】:您可以使用嵌套列表和绝对定位来完成此操作:
nav
display: inline-block;
.submenu
display: none;
position: absolute;
left: 100%;
top: 0;
.menu > li
position: relative;
.menu li:hover .submenu
display: block;
这是一个肮脏的例子http://codepen.io/JKudla/pen/mWGwqp?editors=1100
【讨论】:
以上是关于如何仅使用 css 为垂直导航栏创建子菜单的主要内容,如果未能解决你的问题,请参考以下文章