导航栏的下拉菜单 [英] A dropdown menu for a navbar

查看:108
本文介绍了导航栏的下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直没有尝试为我的导航栏创建下拉菜单,每个CSS方法似乎都无法提供我所需的效果.目前我的HTML看起来像....

I have been trying unsuccessfully to create a drop down menu for my navbar, every single CSS method doesnt seem to provide the desired effect that I require. Currently my HTML looks like....

<!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=utf-8" />
<title>Untitled</title>
</head>

<!-- Reset Sheet -->
<link href="reset.css" rel="stylesheet" type="text/css">
<!-- Main Sheet -->
<link href="main.css" rel="stylesheet" type="text/css">
<!-- Navigation Menu Sheet -->
<link href="navbar.css" rel="stylesheet" type="text/css">

<body>
<table id="header">
<table width="100%" style="height: 100%;" cellpadding="10" cellspacing="0" border="0">

<!-- Table containing logo -->
<tr>
<td colspan="2" valign="middle" height="30" align="center">
<a href="http://www.phonesrus.com.au"><img src="logo.JPG" alt="logo" width="570" ></a>
</td></tr>

<!-- Table containing NavBar -->
<tr>
<td colspan="2" valign="middle" height="55" bgcolor="#300000" align="center">
<div class="navbar">
<ul class="horizontal">               
   <li><a class="first" href="#">Home</a></li>
   <li><a href="#">About Us</a></li>
   <li><a href="#">Our Products</a></li>
   <li><a href="#">Environment</a></li>
   <li><a href="#">Latest News</a></li>
   <li><a class="last" href="#">Contact Us</a></li>
</ul>
</div> 
</td></tr>
</table>

</body>
</html>

并称赞我的导航栏CSS看起来像...

And to compliment that my CSS for the navbar looks like...

.navbar ul.horizontal 
{
list-style-type:none; 
margin:40 auto; 
width:640px; 
padding:0;  
overflow:hidden;
}

.navbar ul.horizontal > li 
{
float:left;
}

.navbar ul.horizontal li a 
{
display: block; 
text-decoration:none; 
text-align:center; 
padding:22px 20px 22px 20px;
font-family:Arial; 
font-size:8pt; 
font-weight:bold; 
color:#FFFFFF; 
text-transform:uppercase; 
border-right:1px solid #607987; 
background-color:#300000;  
letter-spacing:.08em
}

.navbar ul.horizontal li a:hover 
{
background-color:#680000; 
color:#a2becf
}

.navbar ul.horizontal li a.first 
{
border-left:0
}

.navbar ul.horizontal li a.last 
{
border-right:0
}

我要问的问题是,是否要使该菜单成为产品"按钮的下拉菜单,其样式样式与导航栏的其余部分类似(例如,悬停颜色和背景颜色).我将如何使用CSS来实现这一目标. 有问题的CSS的新HTML正在...

My question to the point is if I was to make this menu a drop down menu for the "The products" button following a similar style pattern (such as hover colours and background colours) to the rest of the navbar. how would I go about with CSS to achieve this. The new HTML for the css in question being...

<div class="navbar">
<ul class="horizontal">               
   <li><a class="first" href="#">Home</a></li>
   <li><a href="#">About Us</a></li>
   <li><a href="#">Our Products</a></li>
        <ul>
            <li><a href="#">Apple</a></li>
            <li><a href="#">HTC</a></li>
            <li><a href="#">Nokia</a></li>
            <li><a href="#">Samsung</a></li>
        </ul>
   <li><a href="#">Environment</a></li>
   <li><a href="#">Latest News</a></li>
   <li><a class="last" href="#">Contact Us</a></li>
</ul>
</div> 

我已经尝试了很多次,但是都没有获得正确的结果.对你的帮助表示感谢.谢谢

I have tried with so many attempts but have failed to achieve a proper result. Your help will be greatly appreciated. Thanks

推荐答案

我不太确定这是否是您的意思,也许它需要一些样式调整.

I am not quite sure if this is what you mean, maybe it needs some style adjustment.

问题是您的子菜单列表应位于主菜单的列表项内.像这样:

The thing is that your submenu list should be inside the list item of the main menu. Like this:

<ul>
    <li>Item
        <ul>
            <li>...</li>
        </ul>
    </li>
</ul>

在您的代码中(并优化了CSS),这是我想出的: HTML

Using that in your code (and optimized the CSS), this is what I came up with: HTML

<div class="navbar">
<ul class="horizontal">               
   <li><a href="#">Home</a></li>
   <li><a href="#">About Us</a></li>
   <li><a href="#">Our Products</a>
        <ul>
            <li><a href="#">Apple</a></li>
            <li><a href="#">HTC</a></li>
            <li><a href="#">Nokia</a></li>
            <li><a href="#">Samsung</a></li>
        </ul>
   </li>
   <li><a href="#">Environment</a></li>
   <li><a href="#">Latest News</a></li>
   <li><a href="#">Contact Us</a></li>
</ul>
</div>

CSS:

.horizontal {
    list-style-type:none; 
    margin:40 auto; 
    width:640px; 
    padding:0;  
    overflow:hidden;
}
.horizontal > li {
    float:left;
}
.horizontal li ul {
    display: none;
    margin: 0;
    padding: 0;
    list-style: none;
    position: relative;
    width: 100%;
}
.horizontal li:hover ul {
    display: block;
}
.horizontal li a {
    display: block; 
    text-decoration:none; 
    text-align:center; 
    padding:22px 10px;
    font-family:Arial; 
    font-size:8pt; 
    font-weight:bold; 
    color:#FFFFFF; 
    text-transform:uppercase; 
    border-right:1px solid #607987; 
    background-color:#300000;  
    letter-spacing:.08em
}

.horizontal li a:hover {
    background-color:#680000; 
    color:#a2becf
}

.horizontal li:first-child a { border-left:0; }
.horizontal li:last-child a { border-right:0; }

就像说的那样,您可能需要更改某些样式!

Like said, you may need to change some of the styles!

还要检查有效的 JSFiddle演示

这篇关于导航栏的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆