


Pure CSS to achieve multi-level navigation linkage (with pictures and text examples)
Preface
Navigation bars have been done before...but they were all very simple first-level navigation bars or Secondary navigation bar implemented with JQ. But the things displayed on the page should still be implemented by CSS, and JavaScript should be more responsible for the actions. [Recommended: css video tutorial]
Basics
The first is the secondary navigation bar, for example:
##Before I’ve never understood it… Actually, this kind of thing is very simple to put it bluntly….
The main points are:
1. What should the structure of the entire navigation look like 2. Without JS , how to make the second-level navigation appear when the mouse moves to the first-level navigation. 3. Positioning of the secondary navigation bar.Analysis:
1. The more mainstream approach should be to use the ul tag. Each ul tag is a navigation level, the li inside is a sub-project, and the li contains a tag and ul tag. The a tag is used to click to jump, ul is the navigation bar of the next level, and so on...2. Move the mouse to the first-level navigation bar to display the second-level navigation bar. The first reaction is the hover pseudo-class. According to previous understanding, hover is used to control its own changes. How can we control the sub-elements? What about style? In fact, this is enough:#nav li:hover ul{ display: block; }When the li of the first-level navigation is covered by the mouse, the ul in the child element is displayed. ......
li:hover was just a state, but in fact it is also an element.
I'm so witty.
Code above:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>吃货的导航栏</title> </head> <style type="text/css"> *{ margin:0px; padding:0px; } #nav{ width: 600px; height: 40px; background: gray; margin: 0 auto; } #nav li{ line-height: 40px; float: left; list-style: none; height: 40px; position: relative; } #nav a{ padding: 0 20px; color: black; display: block; text-decoration: none; height: 40px; } #nav a:hover{ background: #058; color:white; } #nav li ul{ display: none; position: absolute; top: 40px; left:0px; } #nav li ul li{ float: none; margin: 2px; width:100px; text-align: center; } #nav li ul li a{ background: #ccc; } #nav li ul li a:hover{ background: deeppink; } #nav li:hover ul{ display: block; } </style> <body> <ul id="nav"> <li><a href="#">首页</a></li> <li><a href="#">肉类</a> <ul> <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> </ul> </li> <li><a href="#">零食</a></li> <li><a href="#">蔬菜</a> <ul> <li><a href="#">白菜</a></li> </ul> </li> </ul> </body> </html>This is the most basic secondary navigation bar, but with this foundation, you have a basis for making multi-level navigation bars and various effects. Multi-level navigation barBased on the above principles, you can easily create a multi-level navigation bar. Take the three-level one as an example to learn to make a good-looking one.
It is also a structure of li embedded with ul, using relative positioning and a little transition effect, and using small triangles made by border. The principle is to change the color of other borders to transparent. .
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>吃货的动画导航栏</title> </head> <style type="text/css"> *{ margin: 0px; padding: 0px; } .top-nav{ width: 960px; margin: 0 auto; list-style: none; background-image: linear-gradient(#444, #111); border-radius: 6px; box-shadow: 0 1px 10px #777; } .top-nav:before,.top-nav:after{ content: ""; display: table; } .top-nav:after{ clear: both; } .top-nav>li{ float: left; border-right: 1px solid saddlebrown; position: relative; line-height: 40px; } .top-nav li{ position: relative; } .top-nav>li a{ font: "微软雅黑" 12px; text-decoration: none; color: goldenrod; padding: 12px 30px; } .top-nav>li a:hover{ color:#fafafa; } .top-nav li ul{ position: absolute; border-radius: 6px; z-index: 1; top: 40px; left: 0px; list-style: none; background-image: linear-gradient(#444, #111); box-shadow: 0 -1 0 rgba(255,255,255,0.3); visibility: hidden; /*这里只能用hidden 不能display*/ opacity: 0; margin: 20px 0 0 0; transition: all .2s ease-in-out; } .top-nav ul ul{ margin-left: 20px; margin-top: 20px; } .top-nav ul li:hover>ul{ margin-left: 0px; } .top-nav li:hover>ul{ opacity: 1; visibility: visible; margin: 0; } .top-nav ul a{ padding: 15px; width: 70px; display: block; } .top-nav ul a:hover{ background-image: linear-gradient(#04acec, #0186ba); } .top-nav ul li:first-child>a{ border-radius: 6px 6px 0 0; }/*第一个跟最后一个a标签设置圆角*/ .top-nav ul li:last-child>a{ border-radius: 0 0 6px 6px; } .top-nav ul li{ box-shadow: 0 1px 0 #111, 0 2px 0 #666; }/*两个阴影叠加产生间隔*/ .top-nav ul li:first-child>a:before{ content: "";/*这句不可少,少了没效果*/ display: block; width: 0; border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 6px solid #444; position: absolute; top: -6px; left: 40px; } .top-nav ul li:first-child>a:hover:before{ border-bottom: 6px solid #04acec; } .top-nav ul ul{ top: 0px; left: 100px; } .top-nav ul ul li:first-child>a:before{ border-top: 6px solid transparent; border-right: 6px solid #444; border-bottom: 6px solid transparent; position: absolute; top: 20px; left: -12px; } .top-nav ul ul li:first-child>a:hover:before{ border-bottom: 6px solid transparent; border-right: 6px solid #04acec; } </style> <body> <ul class="top-nav"> <li><a href="#">什么都吃</a></li> <li><a href="#">肉类</a> <ul> <li><a href="#">铁板牛肉</a> <ul> <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> </ul> </li> <li><a href="#">坩埚田鸡</a></li> </ul> </li> <li><a href="#">中餐</a> <ul> <li><a href="#">家常菜</a> <ul> <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> </ul> </li> </ul> </li> <li><a href="#">水果</a></li> <li><a href="#">甜点</a></li> </ul> </body> </html>Because there are many levels...be careful when using selectors. . When should you add > and when should you use spaces. . Otherwise it will take a long time to change, don’t ask me how I know.
The above is the detailed content of Pure CSS to achieve multi-level navigation linkage (with pictures and text examples). For more information, please follow other related articles on the PHP Chinese website!

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
