CSS创建下拉菜单

简介:

How to Create a CSS3 Dropdown Menu [Tutorial]



Preview

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 1


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     margin: 0;
6     padding: 0;
7     border: none;
8     outline: none;
9 }
10  
11 .menu {
12     height: 40px;
13     width: 505px;
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     position: relative;
29     list-style: none;
30     float: left;
31     display: block;
32     height: 40px;
33 }

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

1 .menu ul { display: none; }

Step 2

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     display: block;
3     padding: 0 14px;
4     margin: 6px 0;
5     line-height: 28px;
6     text-decoration: none;
7  
8     border-left: 1px solid #393942;
9     border-right: 1px solid #4f5058;
10  
11     font-family: Helvetica, Arial, sans-serif;
12     font-weight: bold;
13     font-size: 13px;
14  
15     color: #f3f3f3;
16     text-shadow: 1px 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-left: none; }
26 .menu li:last-child a{ border-right: none; }
27  
28 .menu li:hover > a { color: #8fde62; }

Step 3

Step 4 – Sub Menu

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

1 .menu ul { display: none; }

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     position: absolute;
3     top: 40px;
4     left: 0;
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     height: 0;
24     overflow: hidden;
25     padding: 0;
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     height: 36px;
36     overflow: visible;
37     padding: 0;
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     width: 100px;
3     padding: 4px 0 4px 40px;
4     margin: 0;
5  
6     border: none;
7     border-bottom: 1px solid #353539;
8 }
9  
10 .menu ul li:last-child a { border: none; }

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 { background: url(../img/docs.png) no-repeat 6px center; }
2 .menu a.messages { background: url(../img/bubble.png) no-repeat 6px center; }
3 .menu a.signout { background: url(../img/arrow.png) no-repeat 6px center; }

Step 4

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.






==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/05/13/2497724.html,如需转载请自行联系原作者

相关文章
|
前端开发
css实现一级下拉菜单
css实现一级下拉菜单
|
XML 前端开发 小程序
css实例(一)鼠标hover下拉菜单
红框标注位置,有一个小尖尖角,这个使用样式是这样实现的。 css 复制代码
489 0
|
前端开发
【我的前端】前端项目小练习:CSS创建3D圆柱旋转效果
学习css,我们必须模仿圆柱体,因为我们没有用于圆形物体的css原生盒子。
【我的前端】前端项目小练习:CSS创建3D圆柱旋转效果
|
前端开发
css3实现摇动效果的下拉菜单
使用css3的动画效果,实现下来菜单的摇动效果。
css3实现摇动效果的下拉菜单
|
前端开发
HTML和CSS实现鼠标下拉菜单
HTML和CSS实现鼠标下拉菜单
347 0
|
前端开发 容器
web前端学习(二十八)——CSS3下拉菜单的相关设置
web前端学习(二十八)——CSS3下拉菜单的相关设置
web前端学习(二十八)——CSS3下拉菜单的相关设置
html+css实战38-下拉菜单
html+css实战38-下拉菜单
207 0
html+css实战38-下拉菜单
|
前端开发
css实现一级下拉菜单
css实现一级下拉菜单
|
存储 前端开发 JavaScript
使用 HTML、CSS、JavaScript 创建一个简单的井字游戏(1)
使用 HTML、CSS、JavaScript 创建一个简单的井字游戏
498 0
使用 HTML、CSS、JavaScript 创建一个简单的井字游戏(1)
|
前端开发 JavaScript
使用 HTML 和 CSS 创建响应式猫猫图片库
使用 HTML 和 CSS 创建响应式猫猫图片库
412 0
使用 HTML 和 CSS 创建响应式猫猫图片库

热门文章

最新文章

  • 1
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(九):强势分析Animation动画各类参数;从播放时间、播放方式、播放次数、播放方向、播放状态等多个方面,完全了解CSS3 Animation
    817
  • 2
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(八):学习transition过渡属性;本文学习property模拟、duration过渡时间指定、delay时间延迟 等多个参数
    700
  • 3
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(七):学习ransform属性;本文学习 rotate旋转、scale缩放、skew扭曲、tanslate移动、matrix矩阵 多个参数
    694
  • 4
    (CSS)使用Flex布局,帮助你快速了解各种基本的Flex布局属性以及帮你让元素快速达到布局中的指定位置!
    423
  • 5
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(六):全方面分析css的Flex布局,从纵、横两个坐标开始进行居中、两端等元素分布模式;刨析元素间隔、排序模式等
    759
  • 6
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(五):背景属性;float浮动和position定位;详细分析相对、绝对、固定三种定位方式;使用浮动并清除浮动副作用
    948
  • 7
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(四):元素盒子模型;详细分析边框属性、盒子外边距
    1845
  • 8
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(三):元素继承关系、层叠样式规则、字体属性、文本属性;针对字体和文本作样式修改
    478
  • 9
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(二):CSS伪类:UI伪类、结构化伪类;通过伪类获得子元素的第n个元素;创建一个伪元素展示在页面中;获得最后一个元素;处理聚焦元素的样式
    1553
  • 10
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(一):CSS发展史;CSS样式表的引入;CSS选择器使用,附带案例介绍
    798