如何将响应式导航栏(navbar)拆分为左中右三个区域?
导航栏三分区域布局修改方案
我正在搭建一个评测网站,已实现响应式导航栏,现需调整布局为三个区域:左侧放置Logo,中间展示网站页面/项目入口,右侧设置登录和注册入口。以下是修改后的HTML和CSS代码:
修改后的HTML代码
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <header> <nav> <ul class="nav-bar"> <!-- 左侧Logo区域 --> <li class="logo"> <a href="#"><img src="youreview_logo.png" alt="logo"></a> </li> <!-- 中间页面入口菜单 --> <ul class="main-menu"> <li><a href="#">Home</a></li> <li><a href="#">Products</a></li> <li><a href="#">Services</a></li> <li><a href="#">About</a></li> </ul> <!-- 右侧登录注册区域 --> <ul class="auth-menu"> <li><a href="#">登录</a></li> <li><a href="#">注册</a></li> </ul> <!-- 移动端菜单控制 --> <input type="checkbox" id="check"> <span class="mobile-menu"> <li><a href="#">Home</a></li> <li><a href="#">Products</a></li> <li><a href="#">Services</a></li> <li><a href="#">About</a></li> <li><a href="#">登录</a></li> <li><a href="#">注册</a></li> <label for="check" class="close-menu"><i class="fas fa-times"></i></label> </span> <label for="check" class="open-menu"><i class="fas fa-bars"></i></label> </ul> </nav> </header>
修改后的CSS代码
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@700&display=swap'); * { box-sizing: border-box; margin: 0; padding: 0; } html { height: 100%; overflow: hidden; background: radial-gradient(ellipse at bottom, #1b2735 0%, #090a0f 100%); color: white; } body { font-family: "Poppins", sans-serif; --colour1: #fff; --colour2: #1b2735; } .nav-bar { display: flex; justify-content: space-between; align-items: center; width: 100%; list-style: none; position: relative; background: radial-gradient(ellipse at bottom, #1b2735 0%, #090a0f 100%); padding: 12px 20px; z-index: 1; } .logo img { width: 60px; } /* 中间主菜单样式 */ .main-menu { display: flex; list-style: none; } .main-menu li { padding: 0 30px; } /* 右侧登录注册菜单样式 */ .auth-menu { display: flex; list-style: none; } .auth-menu li { padding: 0 15px; } /* 通用链接样式 */ .nav-bar li a { display: inline-block; text-decoration: none; color: white; text-align: center; transition: 0.15s ease-in-out; position: relative; text-transform: uppercase; } .nav-bar li a::after { content: ""; position: absolute; bottom: 0; left: 0; width: 0%; height: 1px; background-color: white; transition: 0.15s ease-in-out; } .nav-bar li a:hover:after { width: 100%; } /* 移动端菜单控制 */ .open-menu, .close-menu { position: absolute; color: white; cursor: pointer; font-size: 1.5rem; top: 50%; right: 20px; transform: translateY(-50%); display: none; } .close-menu { top: 20px; right: 20px; } #check { display: none; } .mobile-menu { display: none; } @media(max-width: 610px) { /* 隐藏桌面端菜单 */ .main-menu, .auth-menu { display: none; } /* 移动端菜单样式 */ .mobile-menu { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 80%; height: 100vh; position: fixed; top: 0; right: -100%; z-index: 100; background-color: var(--colour2); transition: all 0.2s ease-in-out; list-style: none; } .mobile-menu li { margin-top: 40px; } .mobile-menu li a { padding: 10px; } .open-menu, .close-menu { display: block; } #check:checked ~ .mobile-menu { right: 0; } }
核心改动说明
- 将原有的单一
menu拆分为三个独立区域:.logo(左侧)、.main-menu(中间)、.auth-menu(右侧),通过justify-content: space-between实现三分布局 - 统一导航链接样式,减少代码冗余
- 移动端单独维护整合式菜单,保证移动端导航体验的完整性
- 修复原代码中
columns的拼写错误(改为color)
内容的提问来源于stack exchange,提问作者JKey
相关产品推荐
相关产品推荐

