


This article brings you relevant knowledge about css, which mainly introduces related issues such as the implementation properties of basic css navigation bar and drop-down menu. Using CSS, you can convert it into a good-looking one. Navigation bar instead of boring HTML menu, let’s take a look at it, I hope it will be helpful to everyone.
(Learning video sharing: css video tutorial, html video tutorial)
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 list
As a standard HTML foundation, 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></ul>
and <li>
elements makes a lot of sense, common HTML:
Now, let’s remove margins and padding from the list:
ul { list-style-type: none; margin: 0; padding: 0;}
Example parsing: list-style-type:none
– Remove the small tag in front of the list, a navigation bar does not need the list tag.
Remove the browser's default setting of margins and padding to 0.
The code in the above example is the standard code used for vertical and horizontal navigation bars.
nbsp;html> <meta> <title>显示</title> <style> ul { list-style-type: none; margin: 0; padding: 0; } </style>
Vertical navigation bar
For the above code, we only need the style of the <a></a>
element to create a vertical navigation bar:
li a { display: block; width: 60px; background-color: #dddddd;}
Example description:
display:block
Display the link of the block element, making the entire clickable link area (not just the text), which allows us to specify the width width: 60px.
Block elements are max-width by default. We're going to specify a width of 60 pixels.
Note: Please be sure to specify the width of the <a></a>
element in the vertical navigation bar. If you omit the width, IE6 may have unexpected effects.
nbsp;html> <meta> <title>显示</title> <style> ul { list-style-type: none; margin: 0; padding: 0; } li a { display: block; width: 60px; background-color: #dddddd; } </style>
Vertical navigation bar instance
Create a simple vertical navigation bar instance and modify the background color when the mouse moves to the option:
nbsp;html> <meta> <title>显示</title> <style> ul { list-style-type: none; margin: 0; padding: 0; width: 200px; background-color: #f1f1f1; } li a { display: block; color: #000; padding: 8px 16px; text-decoration: none; } /* 鼠标移动到选项上修改背景颜色 */ li a:hover { background-color: #555; color: white; } </style>
Active/current navigation bar instance
After clicking an option, we can add the "active"
class to standardize which option is selected:
li a.active { background-color: #4CAF50; color: white;}li a:hover:not(.active) { background-color: #555; color: white;}
Sample code:
nbsp;html> <meta> <title>显示</title> <style> ul { list-style-type: none; margin: 0; padding: 0; width: 200px; background-color: #f1f1f1; } li a { display: block; color: #000; padding: 8px 16px; text-decoration: none; } /* 鼠标移动到选项上修改背景颜色 */ li a:hover { background-color: #555; color: white; } li a.active { background-color: #4CAF50; color: white; } li a:hover:not(.active) { background-color: #555; color: white; } </style><script> function removeActiveClass(node) { node.className = ''; } let menus = document.querySelectorAll('#nav'); menus.forEach(function (value, index) { value.addEventListener('click', function (e) { var target = e.target; Array.prototype.forEach.call(document.querySelectorAll('#nav li a'), removeActiveClass); target.className = 'active'; }) }); </script>
Create a link and add a border
Can be found in <li> or <a> </a>
</li>
Add the text-align:center
style to center the link.
You can add the border attribute on border <ul></ul>
to make the navigation bar have a border.
If you want to add a border to each option, you can add border-bottom to each <li>
element:
nbsp;html> <meta> <title>显示</title> <style> ul { list-style-type: none; margin: 0; padding: 0; width: 200px; background-color: #f1f1f1; border: 1px solid #555; } li a { display: block; color: #000; padding: 8px 16px; text-decoration: none; } li { text-align: center; border-bottom: 1px solid #555; } li:last-child { border-bottom: none; } li a.active { background-color: #4CAF50; color: white; } li a:hover:not(.active) { background-color: #555; color: white; } </style><script> function removeActiveClass(node) { node.className = ''; } let menus = document.querySelectorAll('#nav'); menus.forEach(function (value, index) { value.addEventListener('click', function (e) { var target = e.target; Array.prototype.forEach.call(document.querySelectorAll('#nav li a'), removeActiveClass); target.className = 'active'; }) }); </script>
Fixed navigation bar with full screen height
Next we create a fixed navigation bar with full screen height on the left and scrollable content on the right.
ul { list-style-type: none; margin: 0; padding: 0; width: 25%; background-color: #f1f1f1; height: 100%; /* 全屏高度 */ position: fixed; overflow: auto; /* 如果导航栏选项多,允许滚动 */}
Note: This example can be used on mobile devices.
Source code
nbsp;html> <meta> <title>显示</title> <style> *{ margin: 0; padding: 0; } ul { list-style-type: none; margin: 0; padding: 0; width: 25%; background-color: #f1f1f1; height: 100%; /* 全屏高度 */ position: fixed; overflow: auto; /* 如果导航栏选项多,允许滚动 */ } li a { display: block; color: #000; padding: 8px 16px; text-decoration: none; } li { text-align: center; border-bottom: 1px solid #555; } li:last-child { border-bottom: none; } li a.active { background-color: #4CAF50; color: white; } li a:hover:not(.active) { background-color: #555; color: white; } </style><script> function removeActiveClass(node) { node.className = ''; } let menus = document.querySelectorAll('#nav'); menus.forEach(function (value, index) { value.addEventListener('click', function (e) { var target = e.target; Array.prototype.forEach.call(document.querySelectorAll('#nav li a'), removeActiveClass); target.className = 'active'; }) }); </script>
Horizontal navigation bar
There are two ways to create a horizontal navigation bar. Use inline (inline)
or float (float)
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:
ul { list-style-type:none; margin:0; padding:0;}li { display:inline;}
Example analysis:
display:inline
; - By default, <li>
elements are block elements.
Here we remove the newlines before and after each list item to display a single line.
Floating list items
In the example above the links have different widths.
For all links to be of equal width, float the <li>
element and specify the width of the <a></a>
element:
ul { list-style-type:none; margin:0; padding:0; overflow:hidden;}li { float:left;}li a { display: block; color: #000; padding: 8px 16px; text-decoration: none;}
Example analysis:
float:left – 使用浮动块元素的幻灯片彼此相邻。 display:block – 显示块元素的链接,让整体变为可点击链接区域(不只是文本),它允许我们指定宽度。 width:60px – 块元素默认情况下是最大宽度。我们要指定一个60像素的宽度。
Horizontal navigation bar example
Create a horizontal navigation bar and modify the background color after moving the mouse to the option.
ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333;} li { float: left;} li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none;} /*鼠标移动到选项上修改背景颜色 */li a:hover { background-color: #111;}
链接右对齐
将导航条最右边的选项设置右对齐 (float:right;):
添加分割线
<li>
通过 border-right 样式来添加分割线:
/* 除了最后一个选项(last-child) 其他的都添加分割线 */ li { border-right: 1px solid #bbb; } li:last-child { border-right: none; }
固定导航条
可以设置页面的导航条固定在头部或者底部。
固定在头部:
ul { position: fixed; top: 0; width: 100%;}
固定在底部:
ul { position: fixed; bottom: 0; width: 100%;}
注意: 该实例可以在移动设备上使用。
源码
nbsp;html> <meta> <title>显示</title> <style> *{ margin: 0; padding: 0; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; border-right: 1px solid #bbb; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } /*鼠标移动到选项上修改背景颜色 */ li a:hover { background-color: #111; } li a.active { background-color: #4CAF50; color: white; } li a:hover:not(.active) { background-color: #555; color: white; } </style><script> function removeActiveClass(node) { node.className = ''; } let menus = document.querySelectorAll('#nav'); menus.forEach(function (value, index) { value.addEventListener('click', function (e) { var target = e.target; Array.prototype.forEach.call(document.querySelectorAll('#nav li a'), removeActiveClass); target.className = 'active'; }) }); </script>
示例 1
nbsp;html> <meta> <title>原生js实现菜单动态添加active类</title> <style> *{ margin: 0; padding: 0; } ul{ margin: 0 auto; display: flex; justify-content: center; align-items: center; height: 50px; list-style: none; box-shadow: 0 2px 4px #eeeeee; } ul > li { padding: 6px 16px; margin: 0 5px; border-right: 1px solid #f7f7f7; border-bottom: 1px solid transparent; cursor: pointer; } ul > li:last-child{ border-right: none; } li:hover, li:focus, .active { color: #ff6615; border-bottom: 1px solid #ff6615; } </style>
-
<li>首页
<li>产品中心
<li>新闻资讯
<li>文档下载
<li>联系我们
CSS 下拉菜单
使用 CSS 创建一个鼠标移动上去后显示下拉菜单的效果。
基本下拉菜单
当鼠标移动到指定元素上时,会出现下拉菜单。
nbsp;html> <meta> <title>下拉菜单</title> <style> /*鼠标下拉菜单*/ .dropdown { position: relative; 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); padding: 12px 16px; } .dropdown:hover .dropdown-content { display: block; } </style> <div> <span>鼠标移动到我这看到下拉菜单!</span> <div> <p>CSDN博客</p> <p>wgchen.blog.csdn.net</p> </div> </div>
实例解析
HTML 部分
我们可以使用任何的 HTML 元素来打开下拉菜单,如:<span></span>
, 或 a <button></button>
元素。
使用容器元素 (如: <p></p>
) 来创建下拉菜单的内容,并放在任何你想放的位置上。
使用 <p> </p>
元素来包裹这些元素,并使用 CSS 来设置下拉内容的样式。
CSS 部分
.dropdown
类使用 position:relative
:
这将设置下拉菜单的内容放置在下拉按钮 (使用 position:absolute
) 的右下角位置。
.dropdown-content
类中是实际的下拉菜单。
默认是隐藏的,在鼠标移动到指定元素后会显示。 注意 min-width
的值设置为 160px。你可以随意修改它。
注意:如果你想设置下拉内容与下拉按钮的宽度一致,可设置 width 为 100%
( overflow:auto 设置可以在小尺寸屏幕上滚动)。
我们使用 box-shadow
属性让下拉菜单看起来像一个”卡片”。
:hover
选择器用于在用户将鼠标移动到下拉按钮上时显示下拉菜单。
下拉菜单
创建下拉菜单,并允许用户选取列表中的某一项:
nbsp;html> <meta> <title>下拉菜单</title> <style> /* 下拉按钮样式 */ .dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } /* 容器 <div> - 需要定位下拉内容 */ .dropdown { position: relative; 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; } /* 当下拉内容显示后修改下拉按钮的背景颜色 */ .dropdown:hover .dropbtn { background-color: #3e8e41; } </style> <div> <button>下拉菜单</button> <div> <a>CSDN博客 1</a> <a>CSDN博客 2</a> <a>CSDN博客 3</a> </div> </div>
The above is the detailed content of Simple understanding of basic CSS navigation bar and CSS drop-down menu through examples. For more information, please follow other related articles on the PHP Chinese website!

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
