css自动下拉菜单多种实现代码 css下拉菜单的例子

发布时间:2019-08-24编辑:脚本学堂
分享几个css下拉菜单的例子,css横向下拉菜单,css下拉导航菜单效果,css水平下拉菜单,css垂直下拉菜单等,需要的朋友参考下。

css自动下拉菜单多种实现代码

推荐:css下拉菜单

一、横向下拉菜单
 

复制代码 代码示例:
<!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" lang="zh-cn">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<meta http-equiv="content-language" content="zh-cn" />
<title>横向下拉菜单_www.yuju100.com</title>
<style type="text/css">
<!--
* {margin:0;padding:0;border:0;}
body {
font-family: arial, 宋体, serif;
font-size:12px;
}
#nav {
height: 24px;  list-style-type: none;  padding-left:200px;
line-height:24px;overflow:hidden;background:#999;
}
#nav a {
display: block; width: 80px; text-align:center;
}
#nav a:link  {
color:#eee; text-decoration:none;
}
#nav a:visited  {
color:#eee;text-decoration:none;
}
#nav a:hover  {
color:#fff;text-decoration:none;font-weight:bold;background:#ccc;
}
#nav li {
float: left; width: 80px;
}
#nav li ul {
line-height: 24px;
list-style-type: none;
text-align:left;
left: -999px;
width: 520px; //注意,这里一定要设置宽度;
position: absolute;
background:#ccc;
}
#nav li ul li{
float: left;width: 80px; //float就是子菜单横向的关键,可以继承父菜单而省略float
}
#nav li ul a{
display: block; width: 65px;text-align:left;padding-left:15px;
}
#nav li ul a:link  {
color:#f1f1f1; text-decoration:none;
}
#nav li ul a:visited  {
color:#f1f1f1;text-decoration:none;
}
#nav li ul a:hover  {
color:#fff;text-decoration:none;font-weight:normal;background:#c00;
}
#nav li:hover ul {
left:25%;
}
#nav li.sfhover ul {
left:25%;
}
#content {
clear: left;
}
-->
</style>
<script type=text/javascript><!--//--><![cdata[//><!--
function menufix() {
var sfels = document.getelementbyidx("nav").getelementsbytagname_r("li");
for (var i=0; i<sfels.length; i++) {
sfels[i].onmouseover=function() {
this.classname+=(this.classname.length>0? " ": "") + "sfhover";
}
sfels[i].onmousedown=function() {
this.classname+=(this.classname.length>0? " ": "") + "sfhover";
}
sfels[i].onmouseup=function() {
this.classname+=(this.classname.length>0? " ": "") + "sfhover";
}
sfels[i].onmouseout=function() {
this.classname=this.classname.replace(new regexp("( ?|^)sfhoverb"),
"");
}
}
}
window.onload=menufix;
//--><!]]></script>
</head>
<body>
<ul id="nav">
<li><a href="#">菜单一</a>
<ul>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
</ul>
</li>
<li><a href="#">菜单二</a>
<ul>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
</ul>
</li>
<li><a href="#">菜单三</a>
<ul>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
</ul>
</li>
<li><a href="#">菜单四</a>
<ul>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
</ul>
</li>
<li><a href="#">菜单五</a>
<ul>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
</ul>
</li>
<li><a href="#">菜单六</a>
<ul>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
 

二、css下拉导航菜单效果
 

复制代码 代码示例:
<!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" lang="zh-cn">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>css下拉菜单演示</title>
<style type="text/css">
<!--
*{margin:0;padding:0;border:0;}
body {
font-family: arial, 宋体, serif;
font-size:12px;
}
#nav {
line-height: 24px; list-style-type: none; background:#666;
}
#nav a {
display: block; width: 80px; text-align:center;
}
#nav a:link {
color:#666; text-decoration:none;
}
#nav a:visited {
color:#666;text-decoration:none;
}
#nav a:hover {
color:#fff;text-decoration:none;font-weight:bold;
}
#nav li {
float: left; width: 80px; background:#ccc;
}
#nav li a:hover{
background:#999;
}
#nav li ul {
line-height: 27px; list-style-type: none;text-align:left;
left: -999em; width: 180px; position: absolute;
}
#nav li ul li{
float: left; width: 180px;
background: #f6f6f6;
}
#nav li ul a{ wedisplay: block; width: 156px;text-align:left;padding-left:24px;
}
#nav li ul a:link {
color:#666; text-decoration:none;
}
#nav li ul a:visited {
color:#666;text-decoration:none;
}
#nav li ul a:hover {
color:#f3f3f3;text-decoration:none;font-weight:normal;
background:#c00;
}
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}
#content {
clear: left;
}
-->
</style>
<script type=text/javascript>
<!--//--><![cdata[//><!--
function menufix() {
var sfels = document.getelementbyidx("nav").getelementsbytagname_r("li");
for (var i=0; i<sfels.length; i++) {
sfels[i].onmouseover=function() {
this.classname+=(this.classname.length>0? " ": "") + "sfhover";
}
sfels[i].onmousedown=function() {
this.classname+=(this.classname.length>0? " ": "") + "sfhover";
}
sfels[i].onmouseup=function() {
this.classname+=(this.classname.length>0? " ": "") + "sfhover";
}
sfels[i].onmouseout=function() {
this.classname=this.classname.replace(new regexp("( ?|^)sfhoverb"),
"");
}
}
}
window.onload=menufix;
//--><!]]>
</script>
</head>
<body>
<ul id="nav">
<li><a href="#">产品介绍</a>
<ul>
<li><a href="#">产品一</a></li>
<li><a href="#">产品一</a></li>
<li><a href="#">产品一</a></li>
<li><a href="#">产品一</a></li>
<li><a href="#">产品一</a></li>
<li><a href="#">产品一</a></li>
</ul>
</li>
<li><a href="#">服务介绍</a>
<ul>
<li><a href="#">服务二</a></li>
<li><a href="#">服务二</a></li>
<li><a href="#">服务二</a></li>
<li><a href="#">服务二服务二</a></li>
<li><a href="#">服务二服务二服务二</a></li>
<li><a href="#">服务二</a></li>
</ul>
</li>
<li><a href="#">成功案例</a>
<ul>
<li><a href="#">案例三</a></li>
<li><a href="#">案例</a></li>
<li><a href="#">案例三案例三</a></li>
<li><a href="#">案例三案例三案例三</a></li>
</ul>
</li>
<li><a href="#">关于我们</a>
<ul>
<li><a href="#">我们四</a></li>
<li><a href="#">我们四</a></li>
<li><a href="#">我们四</a></li>
<li><a href="#">我们四111</a></li>
</ul>
</li>
<li><a href="#">在线演示</a>
<ul>
<li><a href="#">演示</a></li>
<li><a href="#">演示</a></li>
<li><a href="#">演示</a></li>
<li><a href="#">演示演示演示</a></li>
<li><a href="#">演示演示演示</a></li>
<li><a href="#">演示演示</a></li>
<li><a href="#">演示演示演示</a></li>
<li><a href="#">演示演示演示演示演示</a></li>
</ul>
</li>
<li><a href="#">联系我们</a>
<ul>
<li><a href="#">联系联系联系联系联系</a></li>
<li><a href="#">联系联系联系</a></li>
<li><a href="#">联系</a></li>
<li><a href="#">联系联系</a></li>
<li><a href="#">联系联系</a></li>
<li><a href="#">联系联系联系</a></li>
<li><a href="#">联系联系联系</a></li>
</ul>
</li>
</ul>
</body>
</html>

三、css水平下拉菜单
 

复制代码 代码示例:

<!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>css menu - horizontal</title>
<style type="text/css">
<!--
@import"dhtml-horiz.css";
-->
body {
margin: 0;
padding: 30px;
background: #fff;
color: #666;
}
h1 {
font: bold 16px arial, helvetica, sans-serif;
}
p {
font: 11px arial, helvetica, sans-serif;
}
a {
color: #900;
text-decoration: none;
}
a:hover {
background: #900;
color: #fff;
}
ul#navmenu {
margin: 0;
border: 0 none;
padding: 0;
width: 500px;
list-style: none;
height: 24px;
}
ul#navmenu li {
margin: 0;
border: 0 none;
padding: 0;
float: left;
display: inline;
list-style: none;
position: relative;
height: 24px;
}
ul#navmenu ul {
margin: 0;
border: 0 none;
padding: 0;
width: 160px;
list-style: none;
display: none;
position: absolute;
top: 24px;
left: 0;
}
ul#navmenu ul li {
float: none;
display: block !important;
display: inline;
}

ul#navmenu a {
border: 1px solid #fff;
border-right-color: #ccc;
border-bottom-color: #ccc;
padding: 0 6px;
float: none !important;
float: left;
display: block;
background: #eee;
color: #666;
font: bold 10px/22px verdana, arial, helvetica, sans-serif;
text-decoration: none;
height: auto !important;
height: 1%;
}

ul#navmenu a:hover,
ul#navmenu li:hover a,
ul#navmenu li.iehover a {
background: #ccc;
color: #fff;
}

ul#navmenu li:hover li a,
ul#navmenu li.iehover li a {
float: none;
background: #eee;
color: #666;
}

ul#navmenu li:hover li a:hover,
ul#navmenu li:hover li:hover a,
ul#navmenu li.iehover li a:hover,
ul#navmenu li.iehover li.iehover a {
background: #ccc;
color: #fff;
}

ul#navmenu li:hover li:hover li a,
ul#navmenu li.iehover li.iehover li a {
background: #eee;
color: #666;
}

ul#navmenu li:hover li:hover li a:hover,
ul#navmenu li:hover li:hover li:hover a,
ul#navmenu li.iehover li.iehover li a:hover,
ul#navmenu li.iehover li.iehover li.iehover a {
background: #ccc;
color: #fff;
}

ul#navmenu li:hover li:hover li:hover li a,
ul#navmenu li.iehover li.iehover li.iehover li a {
background: #eee;
color: #666;
}

ul#navmenu li:hover li:hover li:hover li a:hover,
ul#navmenu li.iehover li.iehover li.iehover li a:hover {
background: #ccc;
color: #fff;
}
ul#navmenu ul ul,
ul#navmenu ul ul ul {
display: none;
position: absolute;
top: 0;
left: 160px;
}

ul#navmenu li:hover ul ul,
ul#navmenu li:hover ul ul ul,
ul#navmenu li.iehover ul ul,
ul#navmenu li.iehover ul ul ul {
display: none;
}
ul#navmenu li:hover ul,
ul#navmenu ul li:hover ul,
ul#navmenu ul ul li:hover ul,
ul#navmenu li.iehover ul,
ul#navmenu ul li.iehover ul,
ul#navmenu ul ul li.iehover ul {
display: block;
}
</style>
<script type="text/javascript">
navhover = function() {
var lis = document.getelementbyidx("navmenu").getelementsbytagname_r("li");
for (var i=0; i<lis.length; i++) {
lis[i].onmouseover=function() {
this.classname+=" iehover";
}
lis[i].onmouseout=function() {
this.classname=this.classname.replace(new regexp(" iehoverb"), "");
}
}
}
if (window.attachevent) window.attachevent("onload", navhover);
</script>
</head>
<body>
<h1>css menu - horizontal</h1>
<hr />
<ul id="navmenu">
<li><a href="#">blog</a></li>
<li><a href="#">work +</a>
<ul>
<li><a href="#">websites +</a>
<ul>
<li><a href="#">qrayg</a></li>
<li><a href="#">qarcade</a></li>
<li><a href="#">qlom</a></li>
<li><a href="#">qdt</a></li>
</ul>
</li>
<li><a href="#">pen and ink</a></li>
<li><a href="#">free interfaces</a></li>
</ul>
</li>
<li><a href="#">learn +</a>
<ul>
<li><a href="#"><a href="http://www.jb200.com/list/list_10_1.htm" target="_blank"><font color=red>fireworks</font></a> +</a>
<ul>
<li><a href="#">aquabutton</a></li>
<li><a href="#">aquabutton2</a></li>
<li><a href="#">waterdrop</a></li>
<li><a href="#">lightfx</a></li>
<li><a href="#">lightfx2</a></li>
</ul>
</li>
<li><a href="#">css +</a>
<ul>
<li><a href="#">footerstick</a></li>
<li><a href="#">spritenav</a></li>
<li><a href="#">@import</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">info</a></li>
<li><a href="#">contact</a></li>
</ul>
</body>
</html>

四、css垂直下拉菜单
 

复制代码 代码示例:

<!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>css menu - vertical</title>
<style type="text/css">
body {
margin: 0;
padding: 30px;
background: #fff;
color: #666;
}
h1 {
font: bold 16px arial, helvetica, sans-serif;
}
p {
font: 11px arial, helvetica, sans-serif;
}
a {
color: #900;
text-decoration: none;
}
a:hover {
background: #900;
color: #fff;
}


ul#navmenu,
ul#navmenu li,
ul#navmenu ul {
margin: 0;
border: 0 none;
padding: 0;
width: 160px;
list-style: none;
}
ul#navmenu li {
display: block !important;
display: inline;
position: relative;
}

ul#navmenu a {
border: 1px solid #fff;
border-right-color: #ccc;
border-bottom-color: #ccc;
padding: 0 6px;
display: block;
background: #eee;
color: #666;
font: bold 10px/22px verdana, arial, helvetica, sans-serif;
text-decoration: none;
height: auto !important;
height: 1%;
}

ul#navmenu a:hover,
ul#navmenu li:hover a,
ul#navmenu li.iehover a {
background: #ccc;
color: #fff;
}

ul#navmenu li:hover li a,
ul#navmenu li.iehover li a {
background: #eee;
color: #666;
}

ul#navmenu li:hover li a:hover,
ul#navmenu li:hover li:hover a,
ul#navmenu li.iehover li a:hover,
ul#navmenu li.iehover li.iehover a {
background: #ccc;
color: #fff;
}

ul#navmenu li:hover li:hover li a,
ul#navmenu li.iehover li.iehover li a {
background: #eee;
color: #666;
}

ul#navmenu li:hover li:hover li a:hover,
ul#navmenu li:hover li:hover li:hover a,
ul#navmenu li.iehover li.iehover li a:hover,
ul#navmenu li.iehover li.iehover li.iehover a {
background: #ccc;
color: #fff;
}

ul#navmenu li:hover li:hover li:hover li a,
ul#navmenu li.iehover li.iehover li.iehover li a {
background: #eee;
color: #666;
}

ul#navmenu li:hover li:hover li:hover li a:hover,
ul#navmenu li.iehover li.iehover li.iehover li a:hover {
background: #ccc;
color: #fff;
}
ul#navmenu ul,
ul#navmenu ul ul,
ul#navmenu ul ul ul {
display: none;
position: absolute;
top: 0;
left: 160px;
}

ul#navmenu li:hover ul ul,
ul#navmenu li:hover ul ul ul,
ul#navmenu li.iehover ul ul,
ul#navmenu li.iehover ul ul ul {
display: none;
}
ul#navmenu li:hover ul,
ul#navmenu ul li:hover ul,
ul#navmenu ul ul li:hover ul,
ul#navmenu li.iehover ul,
ul#navmenu ul li.iehover ul,
ul#navmenu ul ul li.iehover ul {
display: block;
}
</style>
<script type="text/javascript">
<!--
navhover = function() {
var lis = document.getelementbyidx("navmenu").getelementsbytagname_r("li");
for (var i=0; i<lis.length; i++) {
lis[i].onmouseover=function() {
this.classname+=" iehover";
}
lis[i].onmouseout=function() {
this.classname=this.classname.replace(new regexp(" iehoverb"), "");
}
}
}
if (window.attachevent) window.attachevent("onload", navhover);
-->
</script>
</head>
<body>
<h1>css menu - vertical</h1>
<hr />
<ul id="navmenu">
<li><a href="#">blog</a></li>
<li><a href="#">work +</a>
<ul>
<li><a href="#">websites +</a>
<ul>
<li><a href="#">qrayg</a></li>
<li><a href="#">qarcade</a></li>
<li><a href="#">qlom</a></li>
<li><a href="#">qdt</a></li>
</ul>
</li>
<li><a href="#">pen and ink</a></li>
<li><a href="#">free interfaces</a></li>
</ul>
</li>
<li><a href="#">learn +</a>
<ul>
<li><a href="#"><a href="http://www.jb200.com/list/list_10_1.htm" target="_blank"><font color=red>fireworks</font></a> +</a>
<ul>
<li><a href="#">aquabutton</a></li>
<li><a href="#">aquabutton2</a></li>
<li><a href="#">waterdrop</a></li>
<li><a href="#">lightfx</a></li>
<li><a href="#">lightfx2</a></li>
</ul>
</li>
<li><a href="#">css +</a>
<ul>
<li><a href="#">footerstick</a></li>
<li><a href="#">spritenav</a></li>
<li><a href="#">@import</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">info</a></li>
<li><a href="#">contact</a></li>
</ul>
</body>
</html>

五、css实现的中英文双语导航菜单
 

复制代码 代码示例:
<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<meta name="keywords" content="bilingual menu,中英文双语菜单" />
<meta http-equiv="description" content="完全用css实现的中英文双语导航菜单" />
<meta content="all" name="robots" />
<meta name="author" content="forestgan" />
<title>完全用css实现的中英文双语导航菜单_www.yuju100.com</title>
<style type="text/css">
a{
color: #ffff99;
text-decoration: none;
}
a:hover{
color: #ffffff;
text-decoration: underline;
}
#nav{
padding: 10px 10px 0;
font-size: 12px;
font-weight: bold;
margin: 1em 0 0;
list-style:none;
}
#nav li{
float: left;
margin-right: 1px;
}
.bi{
position: relative;
z-index: 0;
}
.bi:hover{
z-index: 99;
}
.bi:hover span{
visibility: visible;
top: 0;
left: 0;
cursor: pointer;
}
.bi span{
position: absolute;
left: -999em;
visibility: hidden;
}
#nav li a,.bi:hover span{
line-height: 20px;
text-decoration: none;
background: #dddddd;
color: #666666;
display: block;
width: 80px;
text-align: center;
}
#nav li a:hover,.bi:hover span{
color: #ffffff;
background: #dc4e1b;
}
.bi:hover span{
padding-top: 2px;
}
#navbar{
background: #dc4e1b;
height: 8px;
overflow: hidden;
clear: both;
}
</style>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top">
<ul id="nav">
<li><a class="bi" href="http://www.jb200.com/">home<span>首 页</span></a></li>
<li><a class="bi" href="http://www.jb200.com/blog">blog<span>日志</span></a></li>
<li><a class="bi" href="http://www.jb200.com/guestbook">guestbook<span>留言本</span></a></li>
<li><a class="bi" href="http://www.yuju100.com/">语句百分网<span>精品吧</span></a></li>
<li><a class="bi" href="http://www.leduz.com/">乐读斋<span>leduzhai.com</span></a></li>
</ul>
<div id="navbar"></div>
</body>
</html>