You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CSS调整汉堡菜单至右侧并保持其功能正常?

问题描述

我用HTML和CSS做了个网站,带有简易视差效果、透明导航栏,现在正在做响应式导航栏。功能都正常,但汉堡菜单位置偏左,调位置后就失效了。汉堡菜单靠近屏幕顶部还超出logo左侧,我想让它一直待在屏幕右侧,跟着屏幕尺寸自适应。

解决方法

1. 调整导航栏布局,让汉堡容器靠右

给.navigation设置浮动和垂直居中样式,让它和左侧logo分开展示,确保在导航栏右侧:

.navigation {
    float: right;
    display: flex;
    align-items: center;
    height: 100%;
}

2. 修复汉堡菜单与复选框的定位

调整透明复选框的位置和尺寸,确保点击区域完全覆盖汉堡图标,避免调整位置后功能失效:

.toggle-menu {
    position: absolute;
    width: 30px;
    height: 40px;
    z-index: 3;
    cursor: pointer;
    opacity: 0;
    right: 90px;
    top: 20px;
}

3. 修正移动端导航栏内边距

原代码错误使用.header选择器,改为nav调整移动端内边距,同时适配复选框的右侧位置:

@media screen and (max-width: 1062px) {
    nav {
        padding: 10px 20px;
    }
    .toggle-menu {
        right: 20px;
    }
}

4. 修正viewport拼写错误

HTML中meta标签的veiwport拼写错误,改为viewport确保移动端适配正常:

<meta name="viewport" content="width=device-width, initial-scale=1.0">
修改后的完整代码

CSS代码

@font-face {
    font-family: 'Poppins';
    src: url(Fonts/Poppins-Regular.ttf)
}

@font-face {
    font-family: 'Comfortaa';
    src: url(Fonts/Comfortaa-VariableFont_wght.ttf);
}

@font-face {
    font-family: 'DancingScript';
    src: url(Fonts/DancingScript-VariableFont_wght.ttf);
}

* {
    padding: 0;
    margin: 0;
    color: #A6808C;
    box-sizing: border-box;
}

body {
    background-color: #565264;
    font-family: Poppins;
}

html {
    overflow: scroll;
    overflow-y: hidden;
} 

::-webkit-scrollbar {
    width: 0%;
} 

nav {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 80px;
    padding: 10px 90px;
    box-sizing: border-box;
    background: rgba(0, 0, 0, 0.4);
    border-bottom: 0px solid #fff;
    z-index: 9999
}

nav .logo {
    padding: -22px 20px;
    height: 50px;
    float: left;
    display: flex;
    align-items: center;
}

nav ul {
    list-style: none;
    float: right;
    margin: 0;
    padding: 0;
    display: flex;
}

nav ul li a {
    line-height: 60px;
    color: #fff;
    padding: 12px 30px;
    text-decoration: none;
    font-size: 25px;
}

nav ul li a:hover {
    background: rgba(0,0,0,0.1);
    border-radius: 5px;
}

.text {
    font-size: 2rem;
    padding: 2rem;
    background-color: #565264;
    color: whitesmoke;
}

.title {
    font-size: 7rem;
    color: whitesmoke;
    text-shadow: 0 0 5px black;
}

.background {
    position: absolute;
    height: 100%;
    width: 100%;
    object-fit: cover;
    z-index: -1;
    transform: translateZ(-10px) scale(3);
    background-repeat: no-repeat;
}

header {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    transform-style: preserve-3d;
    z-index: -1;
}

.wrapper {
    height: 100vh;
    overflow-y: auto;
    overflow-x: hidden;
    perspective: 10px;
}

.navigation {
    float: right;
    display: flex;
    align-items: center;
    height: 100%;
}

.hamburger {
    position: relative;
    width: 30px;
    height: 4px;
    background: #fff;
    border-radius: 10px;
    cursor: pointer;
    z-index: 2;
    transition: 0.3s;
}

.hamburger:before, .hamburger:after {
    content: "";
    position: absolute;
    height: 4px;
    right: 0;
    background: #fff;
    border-radius: 10px;
    transition: 0.3s;
}

.hamburger:before {
    top: -10px;
    width: 20px;
}

.hamburger:after {
    top: 10px;
    width: 20px;
}

.toggle-menu {
    position: absolute;
    width: 30px;
    height: 40px;
    z-index: 3;
    cursor: pointer;
    opacity: 0;
    right: 90px;
    top: 20px;
}

.hamburger, .toggle-menu {
    display: none;
}

.navigation input:checked ~ .hamburger {
    background: transparent;
}

.navigation input:checked ~ .hamburger::before {
   top: 0;
   transform: rotate(-45deg);
   width: 30px;
}

.navigation input:checked ~ .hamburger::after {
    top: 0;
    transform: rotate(45deg);
    width: 30px;
 }
 
 .navigation input:checked ~ .menu {
    right: 0;
    box-shadow: -20px 0 40px rgba(0,0,0,0.3);
 }
 
 @media screen and (max-width: 1062px) {
    .hamburger, .toggle-menu {
        display: block;
    }
    
    nav {
        padding: 10px 20px;
    }
    
    .toggle-menu {
        right: 20px;
    }
    
    nav ul {
        justify-content: start;
        flex-direction: column;
        align-items: center;
        position: fixed;
        top: 0;
        right: -300px;
        background-color: #565264;
        width: 300px;
        height: 100%;
        padding-top: 65px;
    }
    
    .menu li {
        width: 100%;
    }
    
    .menu li a, .menu li a:hover {
       padding: 30px; 
       font-size: 24px;
       box-shadow: 0 1px 0 rgba(112,102,119,0.5) inset;
    }
 }

HTML代码

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <Title>Test</Title>
    <link rel="stylesheet" href="Style.css">
</head>

<body>

    <nav>
        <div class="logo">
            <a href="index.html">
                <img src="Pictures\Logo DesignK whitegreen.png" alt="DesignK" height="50px" width="200px">
            </a>
        </div>
        
        <div class="navigation">
        <input type="checkbox" class="toggle-menu">
        <div class="hamburger"></div>
        
        <ul class="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Feedback</a></li>
        </ul>
    </div>    
    </nav>

    <div class="wrapper">
        <header>
            <img src="Pictures/LakeandMoutains.jpg" class="background">
            <h1 class="title">Welcome!</h1>
        </header>

    <section class="text">
        <h3>Essay on Mountains</h3>

    </section>

        </div>
</body>

内容的提问来源于stack exchange,提问作者Atrageous

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:15:51