用css制作多级菜单

html-css020

用css制作多级菜单,第1张

这个是我写的代码:

<!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">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=GB2312" />

<meta name="robots" content="all" />

<title>纯CSS的下拉菜单 支持IE6 IE7 Firefox</title>

<style type="text/css">

*{margin:0padding:0}

body {

background: #fff

font-family: "Lucida Grande", Helvetica, Arial, sans-serif

font-size: 12px

}

/* 定义样式的宽度和其他样式 */

.menu {font-size:12pxpadding-bottom:5pxwidth:750pxmargin:0 auto}

/* 清楚默认的padding和margin以及list的样式 */

.menu ul {padding:0margin:0list-style-type:noneheight:3embackground:transparent}

.menu ul ul{width:7em}

/* 定义float属性使得你可以定义下一级菜单的位置 */

.menu ul li {float:leftheight:3emline-height:3emdisplay:block}

/* 二级菜单样式 */

.menu ul ul li {display:blockwidth:7emheight:autoline-height:1em}

/* 一级菜单的默认样式 */

.menu a, .menu a:visited {display:blockfloat:leftheight:100%width:7emfont-size:1emtext-

decoration:nonecolor:#fffbackground:#949e7cpadding:0 1emborder-left:1px solid #eeeborder-right:1px solid #000}

/* hack IE5.x 定义以及菜单的宽度,这样可以使得获取或者设置当前宽度 */

* html .menu a, * html .menu a:visited { width:7emtext-decoration:none}

/* 下一级的链接默认外观样式 */

.menu ul ul a, .menu ul ul a:visited

{display:blockbackground:#5b8686

color:#fffwidth:7emheight:100%line-height:1empadding:0.5em 1emborder-bottom:1px solid #7aa}

/*定义下级菜单的宽度*/

* html .menu ul ul a, * html .menu ul ul a:visited {width:7em}

/* 定义IE6下table而做的样式 */

.menu table {border-collapse:collapsepadding:0margin:-1pxwidth:0height:0font-size:1emz-index:1}

/* 二级菜单的背景 */

/* 一级链接样式 */

* html .menu a:hover {color:#0066FFposition:relativez-index:100}

/**/

.menu li:hover {position:relative}

/**/

.menu :hover a {color:#ff0background:#d4d8bd}

/* 二级链接样式 */

* html .menu ul ul a:hover{color:#FF0000background:#7aaposition:relativez-index:110}

/*定位二级菜单的位置,设为相关的就是在一级菜单的下方显示,如果是absolute的话大家可以试试*/

.menu ul ul li:hover {position:relative}

.menu ul ul :hover a {color:#fffbackground:#7aa}

.menu ul ul {visibility:hiddenposition:absoluteheight:0top:3emleft:0width:8em}

/* 使得移动到一级定义是否显示下一级菜单,如果把 visibility:visible改成visibility:hidden则就隐藏了二级以上的菜单*/

.menu ul :hover ul{visibility:visibleheight:autopadding:0 3em 3em 3emleft:-3em}

</style>

</head>

<body>

<div class="menu">

<ul>

<li><a href="#">酒店主页<!--[if IE 7]><!--></a><!--<![endif]-->

<!--[if lte IE 6]><table><tr><td><![endif]-->

<ul>

<li><a href="#" title="酒店环境">酒店环境</a></li>

<li><a href="#" title="酒店历史">酒店历史</a></li>

</ul>

<!--[if lte IE 6]></td></tr></table></a><![endif]-->

</li>

<li><a href="#">酒店预订<!--[if IE 7]><!--></a><!--<![endif]-->

<!--[if lte IE 6]><table><tr><td><![endif]-->

<ul>

<li><a href="#" title="房间查询">房间查询</a></li>

<li><a href="#" title="新增客房信息">新增客房信息</a></li>

</ul>

<!--[if lte IE 6]></td></tr></table></a><![endif]-->

</li>

<li><a href="#">业务管理<!--[if IE 7]><!--></a><!--<![endif]-->

<!--[if lte IE 6]><table><tr><td><![endif]-->

<ul>

<li><a href="#" title="入 住">入 住</a></li>

<li><a href="#" title="退 房">退 房</a></li>

</ul>

<!--[if lte IE 6]></td></tr></table></a><![endif]-->

</li>

<li><a href="#">酒店论坛<!--[if IE 7]><!--></a><!--<![endif]-->

<!--[if lte IE 6]><table><tr><td><![endif]-->

<ul>

<li><a href="#" title="我的论坛">我的论坛</a></li>

</ul>

<!--[if lte IE 6]></td></tr></table></a><![endif]-->

</li>

<li><a href="#">访客留言<!--[if IE 7]><!--></a><!--<![endif]-->

<!--[if lte IE 6]><table><tr><td><![endif]-->

<ul>

<li><a href="#" title="我的留言">我的留言</a></li>

</ul>

<!--[if lte IE 6]></td></tr></table></a><![endif]-->

</li>

<li><a href="#">今日活动<!--[if IE 7]><!--></a><!--<![endif]-->

<!--[if lte IE 6]><table><tr><td><![endif]-->

<ul>

<li><a href="#" title="colour wheel">用户积分</a></li>

</ul>

<!--[if lte IE 6]></td></tr></table></a><![endif]-->

</li>

</ul>

</div>

</body>

</html>

希望能帮到你。。

web标准联盟

<!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6     <style> 7         ul{ 8             list-style: none 9         }10         .nav>li{11             float: left12         }13         ul a{14             display: block15             text-decoration: none16             width: 100px17             height: 50px18             text-align: center19             line-height: 50px20             color: white21             background-color: #2f3e4522         }23         .nav>li:first-child a{24             border-radius: 10px 0 0 10px25         }26         .nav>li:last-child a{27             border-radius: 0 10px 10px 028         }29         .drop-down{30             /*position: relative*/31         }32         .drop-down-content{33             padding: 034             display: none35             /*position: absolute*/36         }37         38         h3{39             font-size: 30px40             clear: both41         }42         .drop-down-content li:hover a{43             background-color:red44         }45         .nav .drop-down:hover .drop-down-content{46             display: block47         }48 </style>49 </head>50 <body>51     <ul class="nav">52         <li><a href="#">下拉菜单</a></li>53         <li class="drop-down"><a href="#">下拉菜单</a>54             <ul class="drop-down-content">55                 <li><a href="#">我是1</a></li>56                 <li><a href="#">我是2</a></li>57                 <li><a href="#">我是3</a></li>58             </ul>59             </li>60         <li><a href="#">下拉菜单</a></li>61         <li><a href="#">下拉菜单</a></li>62         <li><a href="#">下拉菜单</a></li>63     </ul>64     <h3>我是测试文字</h3>65 </body>66 </html>

1.boder-radis圆角的制作

2.linear-gradient渐变背景的制作

3.box-shadow阴影效果的制作

4.transition转换效果的制作

5.transparent透明效果

6.CCS3中before的作用

实现的效果图如下:

搭建HTML容器

一级菜单ul的样式修饰:

设置一级菜单的宽度、整体居中显示、用border设置颜色为#222的1像素宽的边框、用background-color设置背景颜色为#111、用background-image和linear-gradient设置背景图片为#444和#111的渐变效果、用border-radius设置圆角边框为6px、用before和after的方式给一级ul填充内容是清除二级浮动的一种方式保证背景能够包裹所有ul内的元素

.top-nav{width:969pxmargin:60px autoborder:1px solid #222background-color:#111               background-image:linear-gradient(#444,#111)border-radius:6pxbox-shadow:0 1px 1px #777               padding:0list-style:none} .top-nav:before,.top-nav:after{content: ""display: table} .top-nav:after{clear: both}

一级菜单内li的样式修饰:

用float将所有的li左浮动、用boder-right给每一个li制作一个左边框效果、用box-shadow制作阴影效果、position:relative是为了下面二级菜单的显示二级菜单会以absolute的方式定位

.top-nav li{float: leftborder-right:1px solid #222box-shadow:1px 0 0 #444position: relative}

一级菜单内li中a元素以及鼠标移动到a元素上之后的样式修饰

这些属性比较简单,就不作一一介绍了

.top-nav li a{float:lefttext-decoration: nonepadding:12px 30pxfont-size:12pxfont-weight:boldtext-shadow: 0 2px 0 #000color: #999} .top-nav li a:hover{color: #fafafa}

二级菜单ul的样式修饰

二级菜单默认是隐藏状态,用绝对定位的方式(相对于一级菜单)将二级菜单定位在top:38pxleft:0的位置上、同样给二级菜单设置渐变背景色和阴影的效果box-shadow中的rgba属性可以制作透明度的效果、用opacity这是透明级别为0、用transition制作一个过度显示和隐藏的效果

.top-nav li ul{visibility: hiddenposition: absolutelist-style: nonetop:38pxleft: 0z-index: 1padding: 0                     background-color: #444background-image: linear-gradient(#444,#111)box-shadow: 0 -1px 0                     rgba(255,255,255,.3)border-radius: 3pxopacity: 0margin: 20px 0 0 0transition: all .2s ease-in-out                    }

当鼠标移动到一级菜单栏的时候二级菜单栏显示

hover属性的应用

.top-nav li:hover >ul{opacity: 1visibility: visiblemargin: 0}

设置二级菜单li和a的样式

方法和一级菜单的li和a的修饰差不多,不做过多的解释

.top-nav ul li{float: nonedisplay: blockborder: 0box-shadow: 0 1px 0 #111,0 2px 0 #666} .top-nav ul a{padding: 10pxwidth: 130pxdisplay: blockfloat: none} .top-nav ul a:hover{background-color: #0186babackground-image: linear-gradient(#04acec,#0186ba)} .top-nav ul li:first-child >a{border-radius: 3px 3px 0 0} .top-nav ul li:last-child >a{border-radius: 0 0 3px 3px}

三角形的制作

用css3制作三角形的的方法就是四边设置同样宽度的border,将要保留的那一边的border设置颜色,其他三边的颜色设置为透明(transparent),则保留颜色的那一边的三角形就制作出来了

.top-nav ul li:first-child >a:before{  content: ""position: absoluteleft:40pxtop: -6px          display: blockwidth: 0border-bottom: 6px solid #444border-left: 6px solid transparent border-right: 6px solid transparent } .top-nav ul li:first-child >a:hover:before{border-bottom-color:#04acec}

三级菜单位置的变换

将三级菜单定位到二级菜单的右边显示

.top-nav ul ul{top:0left: 150pxmargin: 0 0 0 20pxbox-shadow: -1px 0 0 rgba(255,255,255,.3)} .top-nav ul ul li:first-child a:before{left: -6pxtop: 50%margin-top: -6pxborder-left: 0border-bottom: 6px solid transparent                     border-top: 6px solid transparentborder-right: 6px solid #3b3b3b} .top-nav ul ul li:first-child a:hover:before{border-right-color:#0299d3border-bottom-color:transparent}

完整代码(其中有针对IE6的兼容性处理)

<!DOCTYPE html><html><head>  <meta charset="UTF-8">  <!--<script src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>-->  <title>用CSS3实现动画效果的制作</title>  <style type="text/css">      .top-nav{width:969pxmargin:60px autoborder:1px solid #222background-color:#111               background-image:linear-gradient(#444,#111)border-radius:6pxbox-shadow:0 1px 1px #777               padding:0list-style:none}       .top-nav:before,.top-nav:after{content: ""display: table}       .top-nav:after{clear: both}       /*针对IE6,_margin,_height是针对IE6的,其他浏览器中不需要*/       .top-nav{z-index: 1}       .top-nav li{float: leftborder-right:1px solid #222box-shadow:1px 0 0 #444position: relative}       .top-nav li a{float:lefttext-decoration: nonepadding:12px 30pxfont-size:12pxfont-weight:bold          text-shadow: 0 2px 0 #000color: #999}       .top-nav li a:hover{color: #fafafa}       .top-nav li ul{visibility: hiddenposition: absolutelist-style: nonetop:38pxleft: 0z-index: 1padding: 0                     background-color: #444background-image: linear-gradient(#444,#111)box-shadow: 0 -1px 0 rgba(255,255,255,.3)                     border-radius: 3pxopacity: 0margin: 20px 0 0 0transition: all .2s ease-in-out                     _margin:0 }       .top-nav li:hover >ul{opacity: 1visibility: visiblemargin: 0}       .top-nav ul li{float: nonedisplay: blockborder: 0box-shadow: 0 1px 0 #111,0 2px 0 #666}       .top-nav ul a{padding: 10pxwidth: 130pxdisplay: blockfloat: none_height:10px}       .top-nav ul a:hover{background-color: #0186babackground-image: linear-gradient(#04acec,#0186ba)}       .top-nav ul li:first-child >a{border-radius: 3px 3px 0 0}       .top-nav ul li:last-child >a{border-radius: 0 0 3px 3px}       .top-nav ul li:first-child >a:before{  content: ""position: absoluteleft:40pxtop: -6px          display: blockwidth: 0border-bottom: 6px solid #444border-left: 6px solid transparent border-right: 6px solid transparent }       .top-nav ul li:first-child >a:hover:before{border-bottom-color:#04acec}       .top-nav ul ul{top:0left: 150pxmargin: 0 0 0 20px_margin:0box-shadow: -1px 0 0 rgba(255,255,255,.3)}       .top-nav ul ul li:first-child a:before{left: -6pxtop: 50%margin-top: -6pxborder-left: 0border-bottom: 6px solid transparent                     border-top: 6px solid transparentborder-right: 6px solid #3b3b3b}       .top-nav ul ul li:first-child a:hover:before{border-right-color:#0299d3border-bottom-color:transparent}   </style>  <script type="text/javascript">      <!--针对IE6处理hover的问题-->//        $(function () { //            if($.browser.msie &&$.broswer.version.substr(0,1)<7){ //            $("li").has("ul").mouseover(function () { //                $(this).children("ul").css("visibility","visible")//            }).mouseout(function () { //                $(this).children("ul").css("visibility","hidden")//            })//            } //        })   </script></head><body><ul>  <li><a href="#">首页</a></li>  <li><a href="#">课程大厅</a></li>  <li><a href="#">学习中心 +</a>      <ul>          <li><a href="#">视频学习</a>              <ul>                  <li><a href="#">css</a></li>                  <li><a href="#">js</a></li>                  <li><a href="#">jquery</a></li>              </ul>          </li>          <li><a href="#">案例学习</a></li>          <li><a href="#">交流平台</a></li>      </ul>  </li>  <li><a href="#">关于我们</a></li></ul></body>