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

HTML+CSS响应式header导航图标容器适配header尺寸问题求解

响应式导航布局修复方案

核心修改点

  • 给.navContainer新增height: 100%、align-items: center、justify-content: space-between属性,让容器占满header高度,内部元素自动垂直居中、左右两端对齐
  • 删除.logo的浮动属性,flex布局下无需额外浮动规则
  • 将.navIcon的高度改为固定值,避免百分比高度计算异常
  • 合并重复的媒体查询规则,在查询内新增nav {display: none;}隐藏整个导航栏,避免空元素占用布局空间
  • 移除.navIcon多余的flex布局属性

修改后完整CSS代码

body{
    width: 100%;
    height: 100%;
    margin: 0;
    background: #fff;
}

.navContainer {
    width: 90%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 auto;
}

header{
    height: 150px;
    background:#414141;
}

.logo {
    width: 80px;
    height: 80px;
    border-radius: 50%;
}

.navIcon{
    background-color: rgb(223, 196, 196);
    height: 40px;
    width: auto;
}

.navIconContainer{
    display: none;
}

.navListSmall{
    display: none;
}

@media screen and (max-width: 1400px) {
    nav {
        display: none;
    }
    .navIconContainer {
        background-color: red;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}

修改后即可实现小屏幕下logo垂直居左、导航图标垂直居右的效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 06:45:05