Responsive 导航 Bar
你将构建什么
HTML page 使用导航 bar 包含 logo on 左 and 4 链接 on 正确, using flexbox. On mobile, 链接 stack vertically below logo.
学习目标
- Use 语义 <nav> 元素 for 导航
- Apply flexbox 到 align logo and 链接 horizontally
- 添加 media 查询 for mobile vertical 布局
- 样式链接使用 hover 状态 for interactivity
前置要求
- HTML 结构 and 语义标签
- 基础 CSS 包含 flexbox
步骤
- 1
编写 HTML 结构
创建 nav.HTML 使用 nav 元素包含 logo (as 文本) and ul 的 4 链接.
代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Navigation</title> <link rel="stylesheet" href="nav.css"> </head> <body> <nav> <div class="logo">MySite</div> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </body> </html>预期结果
page 使用 nav 包含 logo and 列表的 链接使用默认样式.
- 2
样式 nav 使用 flexbox
创建 nav.CSS. Use flexbox on nav 到推送 logo 左 and 链接正确使用 justify-内容: 空间-之间.
代码nav { display: flex; justify-content: space-between; align-items: center; padding: 1rem 2rem; background: #1e293b; } .logo { font-size: 1.5rem; font-weight: bold; color: #fff; }预期结果
logo appears on 左 and 列表 on 正确, vertically centered.
- 3
样式链接
移除列表样式 and 制作链接 horizontal 使用 flexbox. 添加 hover 状态.
代码nav ul { display: flex; gap: 1.5rem; list-style: none; margin: 0; padding: 0; } nav a { color: #cbd5e1; text-decoration: none; transition: color 0.2s; } nav a:hover { color: #fff; }预期结果
链接是 horizontal 使用否 bullets, and turn white on hover.
- 4
添加 mobile responsive 布局
添加 media 查询 for 最大值-宽度: 600px 到 stack nav vertically on mobile.
代码@media (max-width: 600px) { nav { flex-direction: column; gap: 1rem; } nav ul { flex-direction: column; gap: 0.5rem; align-items: center; } }预期结果
On mobile, logo and 链接 stack vertically and 中心.
提示
Resize 浏览器到 see 布局切换.
- 5
添加响应式样式
使用媒体查询为移动端调整布局:在小屏幕上将多列改为单列、缩小字号、调整间距。移动优先意味着小屏幕体验同样重要。
代码@media (max-width: 640px) { .container { flex-direction: column; padding: 1rem; } h1 { font-size: 1.5rem; } .card { width: 100%; margin-bottom: 1rem; } }预期结果
在 640px 以下视口,布局变为单列,字号和间距缩小,内容不溢出。
自己动手试试
<!-- nav.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Navigation</title>
<link rel="stylesheet" href="nav.css">
</head>
<body>
<nav>
<!-- TODO: logo + links -->
</nav>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Navigation</title>
<link rel="stylesheet" href="nav.css">
</head>
<body>
<nav>
<div class="logo">MySite</div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
</html>
/* nav.css */
nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
background: #1e293b;
}
.logo { font-size: 1.5rem; font-weight: bold; color: #fff; }
nav ul { display: flex; gap: 1.5rem; list-style: none; margin: 0; padding: 0; }
nav a { color: #cbd5e1; text-decoration: none; transition: color 0.2s; }
nav a:hover { color: #fff; }
@media (max-width: 600px) {
nav { flex-direction: column; gap: 1rem; }
nav ul { flex-direction: column; gap: 0.5rem; align-items: center; }
}你学到了什么
你构建 responsive 导航 bar 使用 flexbox, 语义 HTML, and mobile breakpoint — 导航模式 used on virtually 每个 website.
相关资源
相关课程
- CSS Flexbox
Flexbox for nav 布局.
- HTML 语义标签
nav 元素.
相关练习
- Flexbox 方向测验
练习 flexbox 属性.
相关速查表
- CSS Cheatsheet
Flexbox 引用.
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。