- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 引入
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 定位
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 转换
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪辑
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块尺寸
- CSS - 最小块尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移
- CSS - 口音颜色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - 重要
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2d 变换
- CSS - 3d 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - 导航栏
导航栏是图形用户界面 (GUI) 的一部分,帮助用户浏览网站、应用程序或其他软件。对于用户快速轻松地导航到他们正在寻找的内容至关重要。
导航栏可以是水平的或垂直的,包含指向重要页面或功能的链接。
目录
如何设计响应式导航栏?
以下是使用 CSS 设计导航栏的常见步骤
- HTML 结构:在无序列表 ( <ul> ) 内使用锚标记 ( <a> ) 来获取导航栏的结构。
- 移除默认样式:使用属性 'list-style-type: none' 移除无序列表的默认样式和标签。
- Flex 布局:对 ul 元素使用属性 display: flex 并根据需要水平或垂直设置 flex-direction 为 row 或 column。
- 响应式设计:使用 CSS 媒体查询 根据用户屏幕宽度在导航栏的水平和垂直布局之间切换。
- 汉堡菜单:创建一个汉堡菜单图标,在较小的屏幕上切换列表的可见性。
导航栏还可以包含其他元素,例如网站或应用程序的徽标、搜索栏或社交媒体图标。可以使用 CSS 对导航栏进行样式设置以更改其外观。
CSS 水平导航栏
以下示例显示了一个水平导航栏,这是最常见的导航栏类型,显示在网页顶部,如下所示
示例
<!DOCTYPE html>
<html>
<head>
<style>
body{
color: #4CAF50;
}
ul {
background-color: #333;
overflow: hidden;
list-style-type: none;
margin: 0;
padding: 0;
width: 100%;
display: flex;
flex-direction: row;
}
li a {
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-size: 18px;
transition: background-color 0.3s, color 0.3s;
}
li a:hover {
background-color: #575757;
color: #ffffff;
}
.active-link {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<ul>
<li class="active-link">
<a href="#" >Tutorialspoint</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Articles</a>
</li>
<li>
<a href="#">Courses</a>
</li>
</ul>
<h1> Welcome to TutorialsPoint </h1>
<h3> Simple Easy Learning at your fingertips </h3>
</body>
</html>
CSS 垂直导航栏
垂直导航栏也称为侧边栏菜单。它通常位于屏幕的左侧或右侧。
示例
<!DOCTYPE html>
<html>
<head>
<style>
body{
color: #4CAF50;
display: flex;
flex-direction: row;
gap: 10px;
}
ul {
background-color: #333;
overflow: hidden;
list-style-type: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
li a {
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-size: 18px;
transition: background-color 0.3s, color 0.3s;
}
li a:hover {
background-color: #575757;
color: #ffffff;
}
.active-link {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<ul>
<li class="active-link">
<a href="#" >Tutorialspoint</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Articles</a>
</li>
<li>
<a href="#">Courses</a>
</li>
</ul>
<div>
<h1> Welcome to TutorialsPoint </h1>
<h3> Simple Easy Learning at your fingertips </h3>
</div>
</body>
</html>
CSS 下拉导航栏
下拉导航栏是一个带有下拉菜单的导航栏,当用户将鼠标悬停在链接上时会出现。下拉菜单是在狭小空间中显示相关项目列表的一种方式。
示例
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet"
href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.navbar {
background-color: #2c3e50;
overflow: hidden;
}
.navbar a {
display: block;
float: left;
color: #ecf0f1;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-size: 16px;
transition: background-color 0.3s, color 0.3s;
}
.navbar a:hover {
background-color: #34495e;
color: #ecf0f1;
}
.active-link {
background-color: #4CAF50;
color: white;
}
.dropdown {
float: left;
}
.dropdown .dropbtn {
border: none;
color: #ecf0f1;
padding: 14px 20px;
background-color: #2c3e50;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s, color 0.3s;
}
.dropdown .dropbtn:hover {
background-color: #4CAF50;
color: #ecf0f1;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #34495e;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: #ecf0f1;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
transition: background-color 0.3s, color 0.3s;
}
.dropdown-content a:hover {
background-color: #4CAF50;
color: white;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<nav class="navbar">
<a href="#" class="active-link">Tutorialspoint</a>
<a href="#">Home</a>
<div class="dropdown">
<button class="dropbtn">Articles
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">HTML</a>
<a href="#">CSS</a>
<a href="#">Bootstrap</a>
</div>
</div>
<a href="#">Courses</a>
</nav>
<h1> Welcome to TutorialsPoint </h1>
<h3> Simple Easy Learning at your fingertips </h3>
</body>
</html>
CSS 固定导航栏
固定导航栏是指当用户向下滚动页面时会粘贴到屏幕顶部的导航栏。要使导航栏固定,可以使用 position: fixed 属性。
示例
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
height: 2000px;
background-color: #e6e451;
}
.navbar {
position: fixed;
top: 0;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #2c3e50;
}
.navbar a {
display: block;
float: left;
color: #ecf0f1;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-size: 17px;
transition: background-color 0.3s, color 0.3s;
}
.navbar a:hover {
background-color: #34495e;
color: #ecf0f1;
}
.active-link {
background-color: #4CAF50;
color: white;
}
.heading {
padding-top: 70px;
text-align: center;
background-color: #e6e451;
padding-bottom: 300px;
}
</style>
</head>
<body>
<nav class="navbar">
<a href="#" class="active-link">
Tutorialspoint
</a>
<a href="#">Home</a>
<a href="#">Articles</a>
<a href="#">Courses</a>
</nav>
<div class="heading">
<h1> Welcome to TutorialsPoint </h1>
<h2> Tutorialspoint CSS Fixed Navbar </h2>
<h3>Scrolldown....</h3>
</div>
</body>
</html>
导航栏的分隔线
您还可以使用 border-right 属性在导航栏中的链接之间添加分隔线,如下所示
示例
<!DOCTYPE html>
<html>
<head>
<style>
body{
color: #4CAF50;
}
ul {
background-color: #333;
overflow: hidden;
list-style-type: none;
margin: 0;
padding: 0;
width: 100%;
display: flex;
flex-direction: row;
}
li a {
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-size: 18px;
border-right: 5px solid green;
transition: background-color 0.3s, color 0.3s;
}
li a:hover {
background-color: #575757;
color: #ffffff;
}
.active-link {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<ul>
<li class="active-link">
<a href="#" >Tutorialspoint</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Articles</a>
</li>
<li>
<a href="#">Courses</a>
</li>
</ul>
<h1> Welcome to TutorialsPoint </h1>
<h3> Simple Easy Learning at your fingertips </h3>
</body>
</html>
固定垂直导航栏
以下示例演示了如何使用 position: fixed 属性创建固定垂直导航栏,以确保导航栏始终位于屏幕左侧,即使用户向下滚动页面。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div{
height: 1000px;
margin-left: 35%;
}
ul {
background-color: #333;
overflow: hidden;
list-style-type: none;
position: fixed;
width: 30%;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
li a {
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-size: 18px;
transition: background-color 0.3s, color 0.3s;
}
li a:hover {
background-color: #575757;
color: #ffffff;
}
.active-link {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<ul>
<li class="active-link">
<a href="#" >Tutorialspoint</a>
</li>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Articles</a>
</li>
<li>
<a href="#">Courses</a>
</li>
</ul>
<div>
<h1> Welcome to TutorialsPoint </h1>
<h3> Simple Easy Learning at your fingertips </h3>
</div>
</body>
</html>
广告