CSS创建下拉菜单

How to Create a CSS3 Dropdown Menu [Tutorial]

Topic: CSS3
Difficulty: Beginner
Estimated Completion Time: 20 mins

In this tutorial we will code in pure CSS3 the Navigation Menu that you can find in Impressionist UI by Vladimir Kudinov.

Step 1 – HTML Markup

We will create an unordered list with a list item and an anchor tag for each menu link. To create the sub menu add another unordered list inside of the list.

1 <ul class="menu">
2  
3     <li><a href="#">My dashboard</a></li>
4     <li><a href="#">Likes</a></li>
5     <li><a href="#">Views</a>
6  
7         <ul>
8             <li><a href="#" class="documents">Documents</a></li>
9             <li><a href="#" class="messages">Messages</a></li>
10             <li><a href="#" class="signout">Sign Out</a></li>
11         </ul>
12  
13     </li>
14     <li><a href="#">Uploads</a></li>
15     <li><a href="#">Videos</a></li>
16     <li><a href="#">Documents</a></li>
17  
18 </ul>

Step 2 – Menu Layout

We will start to remove the margin, padding, border and outline from all the elements of the menu. Then we will add a fixed width and height to the menu, rounded corners and the CSS3 gradients. To align the links horizontally we will float the lists to left. We also need to set the position to relative because we will need that to align the sub menus.

1 .menu,
2 .menu ul,
3 .menu li,
4 .menu a {
5     margin0;
6     padding0;
7     bordernone;
8     outlinenone;
9 }
10  
11 .menu {
12     height40px;
13     width505px;
14  
15     background#4c4e5a;
16     background: -webkit-linear-gradient(top#4c4e5a 0%,#2c2d33 100%);
17     background: -moz-linear-gradient(top#4c4e5a 0%,#2c2d33 100%);
18     background: -o-linear-gradient(top#4c4e5a 0%,#2c2d33 100%);
19     background: -ms-linear-gradient(top#4c4e5a 0%,#2c2d33 100%);
20     background: linear-gradient(top#4c4e5a 0%,#2c2d33 100%);
21  
22     -webkit-border-radius: 5px;
23     -moz-border-radius: 5px;
24     border-radius: 5px;
25 }
26  
27 .menu li {
28     positionrelative;
29     list-stylenone;
30     floatleft;
31     displayblock;
32     height40px;
33 }

We will hide the sub menu temporarily to be easier to style the first level.

1 .menu ul { displaynone; }

Step 3 – Menu Links

To style the menu links we will add some basic CSS properties like font, color, padding, etc. Then we will add a dark text shadow and a color transition to create a smooth effect when the color changes on hover state. To create the links separator add a border to the left and right and then we will remove the left border from the first link and the right border from the last link. For the hover state we will only change the text color.

1 .menu li a {
2     displayblock;
3     padding0 14px;
4     margin6px 0;
5     line-height28px;
6     text-decorationnone;
7  
8     border-left1px solid #393942;
9     border-right1px solid #4f5058;
10  
11     font-familyHelveticaArialsans-serif;
12     font-weightbold;
13     font-size13px;
14  
15     color#f3f3f3;
16     text-shadow1px 1px 1px rgba(0,0,0,.6);
17  
18     -webkit-transition: color .2s ease-in-out;
19     -moz-transition: color .2s ease-in-out;
20     -o-transition: color .2s ease-in-out;
21     -ms-transition: color .2s ease-in-out;
22     transition: color .2s ease-in-out;
23 }
24  
25 .menu li:first-child a { border-leftnone; }
26 .menu li:last-child a{ border-rightnone; }
27  
28 .menu li:hover > a { color#8fde62; }

Step 4 – Sub Menu

First let’s remove this line of code that we have added on the second step.

1 .menu ul { displaynone; }

Now we will style the sub menu. We will start to position the sub menu 40px from the top and 0px from the left of the menu item and add bottom rounded corners. We will set the opacity to 0 and on hover state to 1 to create the fade in/out effect. For the slide down/up effect we need to set the list height to 0px when is hidden and to 36px on hover state.

1 .menu ul {
2     positionabsolute;
3     top40px;
4     left0;
5  
6     opacity: 0;
7     background#1f2024;
8  
9     -webkit-border-radius: 0 0 5px 5px;
10     -moz-border-radius: 0 0 5px 5px;
11     border-radius: 0 0 5px 5px;
12  
13     -webkit-transition: opacity .25s ease .1s;
14     -moz-transition: opacity .25s ease .1s;
15     -o-transition: opacity .25s ease .1s;
16     -ms-transition: opacity .25s ease .1s;
17     transition: opacity .25s ease .1s;
18 }
19  
20 .menu li:hover > ul { opacity: 1; }
21  
22 .menu ul li {
23     height0;
24     overflowhidden;
25     padding0;
26  
27     -webkit-transition: height .25s ease .1s;
28     -moz-transition: height .25s ease .1s;
29     -o-transition: height .25s ease .1s;
30     -ms-transition: height .25s ease .1s;
31     transition: height .25s ease .1s;
32 }
33  
34 .menu li:hover > ul li {
35     height36px;
36     overflowvisible;
37     padding0;
38 }

We will set the width of the sub menu links to 100px. Instead of left and right borders we will add a bottom one and remove it from the last link.

1 .menu ul li a {
2     width100px;
3     padding4px 0 4px 40px;
4     margin0;
5  
6     bordernone;
7     border-bottom1px solid #353539;
8 }
9  
10 .menu ul li:last-child a { bordernone; }

To finish it we only need to add an icon to each sub menu link. To do it we will create a custom class for each one and add a background image.

1 .menu a.documents { backgroundurl(../img/docs.png) no-repeat 6px center; }
2 .menu a.messages { backgroundurl(../img/bubble.png) no-repeat 6px center; }
3 .menu a.signout { backgroundurl(../img/arrow.png) no-repeat 6px center; }

Conclusion

We’ve successfully created this pure CSS3 dropdown menu. If you have any question let me know in the comments. Also don’t forget to leave some feedback and share it with your friends. Follow us if you want to be the first to know about the latest tutorials and articles.

时间: 2024-07-31 22:37:52

CSS创建下拉菜单的相关文章

纯CSS实现下拉菜单及下拉容器等(纯CSS实现导航条及导航下拉容器)

原文:纯CSS实现下拉菜单及下拉容器等(纯CSS实现导航条及导航下拉容器)  虽然网上类似甚至相同的案例有很多,但是我还是写下,以记下笔记,也可供大家参考 希望大家可以指导批评~~ 首先我们以列表ul li 来开始我们菜单也可以说导航条的制作: 在页面中我们首先构建以下XHTML结构: <body> <ul id="navWrapper"> <li> <a href="#">Menu A</a> <u

求大神解答一下-div+css的下拉菜单怎么让初始状态时所有菜单都是收起来的

问题描述 div+css的下拉菜单怎么让初始状态时所有菜单都是收起来的 div+css的下拉菜单怎么让初始状态时所有菜单都是收起来的不是进去第一个是展开的 解决方案 初始时让菜单所在的div隐藏,待需要显示时用js改变这个div的显示状态. div + css 只能做出静态的页面,页面如同一个图片,不能与用户交互,通过js事件改变css的值来实现交互. 解决方案二: css统一设置菜单display:none 你要展开那个设置dom元素的style为display:block显示或者写个高级别样

纯CSS的下拉菜单

 代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html>      <head>   <meta http-equiv="Content-Type" c />   <ti

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

CSS3 创建下拉菜单

预览 下图展示了不兼容 CSS3 情况下的菜单样式. 使用了一个渐变图像 白色透明的图像用于实现渐变效果.因为新的 CSS3 渐变特性还没有得到所有浏览器的支持,使用简便背景图片更为安全. 渐变的强度可以通过切换背景图像的上移或下移来改变.此外,渐变颜色可以很容易地通过改变背景颜色来调整. CSS 代码 我不打算解释逐行解释 CSS 代码.下面的图片很好地解释了编写下拉菜单的关键点.

动态创建下拉菜单

<html> <head> <title>动态下拉菜单</title> <style> SELECT.smallSel {   BACKGROUND-COLOR: white;   COLOR: #000080;   FONT-SIZE: 9pt } </style> <script language=javascript> <!-- hide from old browsers... /*   动态下拉菜单 --

精美Css+Xhtml下拉菜单特效代码

站酷首页 下拉菜单 网页制作教程 我的酷站 我的网站 js特效 我的收藏 社区圈子 我的CHINAY 我的首页 我的网站 js特效 我的收藏 我的短信 我的CHINAY js特效 我的收藏 账户管理 我的CHINAY 我的首页 我的网站 js特效 我的收藏 我的网站 js特效 我的收藏

纯Css实现下拉菜单的简单例子

大家可能会经常用到hover这属性,用hover实现鼠标经过的颜色改变等等之类,其实hover非常强大. 分享一个鼠标经过,弹出下拉菜单的小技巧: 当我们建好一个列表: <ul> <li class="home"><a href="javascript:;" target="_blank" title="">一级菜单</a></li> <li class=&qu

jquery CSS导航下拉菜单代码

 代码如下 复制代码   <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html> <head> <title>伸缩的菜单,用toggle()重写</title> <style> <!-- b