CSS navigation bar
CSS Navigation Bar
Vertical
#Horizontal
Navigation Bar Proficient use of the navigation bar is very important for any website. Using CSS you can transform into a beautiful navigation bar instead of a boring HTML menu.
Navigation bar = link listAs the basis of standard HTML, a navigation bar is necessary. In our example we will create a standard HTML list navigation bar. The navigation bar is basically a list of links, so using the <ul> and <li> elements makes a lot of sense:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> <p>注意:我们用 href="#"作为测试连接.用在一个真正的web站点的url。</p> </body> </html>
Run instance?Click the "Run instance" button to view the online instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> ul { list-style-type:none; margin:0; padding:0; } </style> </head> <body> <ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> </body> </html>
Run the instance?Click the "Run Instance" button to view the online instance
- list-style-type:none - Remove the small mark in front of the list. A navigation bar does not require list tags
- Remove the browser's default settings and set margins and padding to 0
Vertical Navigation BarIn the above code, we only need the style of the <a> element to create a simple vertical navigation bar:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> ul { list-style-type:none; margin:0; padding:0; } a { display:block; width:60px; background-color:#dddddd; } </style> </head> <body> <ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> <p>A background color is added to the links to show the link area.</p> <p>Notice that the whole link area is clickable, not just the text.</p> </body> </html>
Run the instance?Click the "Run Instance" button to view the online instance
- display:block - displays links to block elements, making the entire link area clickable (not just text), it allows us to specify the width
- width:60px - Block elements are max-width by default. We need to specify a width of 60 pixels
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> ul { list-style-type:none; margin:0; padding:0; } a:link,a:visited { display:block; font-weight:bold; color:#FFFFFF; background-color:#98bf21; width:120px; text-align:center; padding:4px; text-decoration:none; text-transform:uppercase; } a:hover,a:active { background-color:#7A991A; } </style> </head> <body> <ul> <li><a href="#home">主页</a></li> <li><a href="#news">新闻</a></li> <li><a href="#contact">联系</a></li> <li><a href="#about">关于</a></li> </ul> </body> </html>
Run the instance?Click the "Run Instance" button to view the online instance
Note: Please be sure to specify the width of the <a> element in the vertical navigation bar. If you omit the width, IE6 may have unexpected effects.
Horizontal Navigation Bar
There are two ways to create a horizontal navigation bar. Use inline or floating for list items.
Both methods are fine, but if you want links to have the same size, you have to use the float method.
Inline list items
One of the ways to create a horizontal navigation bar is to specify elements. The above code is standard inline:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> ul { list-style-type:none; margin:0; padding:0; padding-top:6px; padding-bottom:6px; } li { display:inline; } a:link,a:visited { font-weight:bold; color:#FFFFFF; background-color:#98bf21; text-align:center; padding:6px; text-decoration:none; text-transform:uppercase; } a:hover,a:active { background-color:#7A991A; } </style> </head> <body> <ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> <p><b>注意:</b>如果您只为 a 元素设置内边距(而不设置 ul 元素),那么链接会出现在 ul 元素之外。所以,我们为 ul 元素添加了 top 和 bottom 内边距。 </p> </body> </html>
Run instance?
Click the "Run instance" button to view the online instance
Instance analysis:
display:inline; - By default, the <li> element is a block element. Here we remove the newlines before and after each list item to display a single line.
Floating list items
In the above example the links have different widths.
For all links to have equal width, float the <li> element and specify the width of the <a> element:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> ul { list-style-type:none; margin:0; padding:0; overflow:hidden; } li { float:left; } a:link,a:visited { display:block; width:120px; font-weight:bold; color:#FFFFFF; background-color:#98bf21; text-align:center; padding:4px; text-decoration:none; text-transform:uppercase; } a:hover,a:active { background-color:#7A991A; } </style> </head> <body> <ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> <p><b>注意:</b> 如果!DOCTYPE 没有定义, floating 可以产生意想不到的结果.</p> <p>A background color is added to the links to show the link area. The whole link area is clickable, not just the text.</p> <p><b>注意:</b> overflow:hidden 添加到ul元素,以防止li元素列表的外出。.</p> </body> </html>
Run instance?
Click the "Run instance" button to view the online instance
Instance analysis:
- ##float:left - slides using floating block elements next to each other
- display:block - displays links to block elements, making the entirety a clickable link area (not just the text ), which allows us to specify the width
- width:120px - the maximum width for block elements by default. We want to specify a width of 120 pixels
More examples
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> body {margin: 0;} ul.sidenav { list-style-type: none; margin: 0; padding: 0; width: 25%; background-color: #f1f1f1; position: fixed; height: 100%; overflow: auto; } ul.sidenav li a { display: block; color: #000; padding: 8px 16px; text-decoration: none; } ul.sidenav li a.active { background-color: #4CAF50; color: white; } ul.sidenav li a:hover:not(.active) { background-color: #555; color: white; } div.content { margin-left: 25%; padding: 1px 16px; height: 1000px; } @media screen and (max-width: 900px) { ul.sidenav { width: 100%; height: auto; position: relative; } ul.sidenav li a { float: left; padding: 15px; } div.content {margin-left: 0;} } @media screen and (max-width: 400px) { ul.sidenav li a { text-align: center; float: none; } } </style> </head> <body> <ul class="sidenav"> <li><a class="active" href="#home">主页</a></li> <li><a href="#news">新闻</a></li> <li><a href="#contact">联系</a></li> <li><a href="#about">关于</a></li> </ul> <div class="content"> <h2>响应式边栏导航实例</h2> <p>该实例在屏幕宽度小于 900px 时导航栏为顶部水平导航栏,如果大于 900px 导航栏会在左边,且是固定的。</p> <p>如果屏幕宽度小于 400px 会变为垂直导航栏。</p> <h3>重置浏览器窗口大小,查看效果。</h3> </div> </body> </html>
Run instance?Click the "Run instance" button to view the online instance
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> body {margin: 0;} ul.topnav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } ul.topnav li {float: left;} ul.topnav li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } ul.topnav li a:hover:not(.active) {background-color: #111;} ul.topnav li a.active {background-color: #4CAF50;} ul.topnav li.right {float: right;} @media screen and (max-width: 600px){ ul.topnav li.right, ul.topnav li {float: none;} } </style> </head> <body> <ul class="topnav"> <li><a class="active" href="#home">主页</a></li> <li><a href="#news">新闻</a></li> <li><a href="#contact">联系</a></li> <li class="right"><a href="#about">关于</a></li> </ul> <div style="padding:0 16px;"> <h2>响应式导航栏实例</h2> <p>在屏幕宽度小于 600px 会重置导航栏。</p> <h4>重置浏览器窗口大小,查看效果。</h4> </div> </body> </html>
Run instance?Click the "Run instance" button to view the online instance
<!DOCTYPE html> <html> <head> <title>下拉菜单实例</title> <meta charset="utf-8"> <style> ul{ list-style-type:none; margin:0; padding:0; overflow:hidden; background-color:#333; } li{ float:left; } li a, .dropbtn { display:inline-block; color:white; text-align:center; padding:14px 16px; text-decoration:none; } li a:hover, .dropdown:hover, .dropbtn { background-color:#111; } .dropdown { display:inline-block; } .dropdown-content { display:none; position:absolute; background-color:#f9f9f9; min-width:160px; box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2); } .dropdown-content a { color:black; padding:12px 16px; text-decoration:none; display:block; } .dropdown-content a:hover {background-color: #f1f1f1} .dropdown:hover .dropdown-content { display:block; } </style> </head> <body> <ul> <li><a class="active" href="#home">主页</a></li> <li><a href="#news">新闻</a></li> <div class="dropdown"> <a href="#" class="dropbtn">下拉菜单</a> <div class="dropdown-content"> <a href="#">链接 1</a> <a href="#">链接 2</a> <a href="#">链接 3</a> </div> </div> </ul> <h3>导航栏上的下拉菜单</h3> <p>鼠标移动到 "下拉菜单" 链接先显示下拉菜单。</p> </body> </html>
Run instance?Click the "Run Instance" button to view the online instance