侧边栏菜单11个选项仅显示9个,如何适配不同屏幕尺寸?
侧边栏适配解决方案
问题原因
当前侧边栏固定占满全屏高度,每个菜单项固定20px内边距,总高度超出了小屏幕的可视区域,且未做溢出处理,所以底部2个条目被隐藏。
推荐解决方案(按优先级排序)
- 方案1:侧边栏加垂直滚动(兼容性最好,适配任意菜单数量)
给#sidebarMenu添加溢出滚动规则,同时修正高度计算,避开顶部固定的头部栏:
可添加滚动条美化规则,和侧边栏风格统一:#sidebarMenu { height: calc(100% - 60px); /* 减去顶部header的60px高度,避免内容溢出视口 */ overflow-y: auto; /* 内容超出时自动出现垂直滚动条 */ /* 原有其他属性保留 */ }#sidebarMenu::-webkit-scrollbar { width: 6px; } #sidebarMenu::-webkit-scrollbar-thumb { background-color: rgba(255,255,255,0.2); border-radius: 3px; } - 方案2:自适应缩小菜单尺寸(适合希望一屏展示所有条目的场景)
结合媒体查询根据屏幕高度动态调整菜单的内边距和字体大小,同时可添加文字截断规则避免折行占用高度:/* 原有li属性保留,添加以下媒体查询 */ @media (max-height: 700px) { .sidebarMenuInner li { padding: 16px 20px; font-size: 14px; } } @media (max-height: 600px) { .sidebarMenuInner li { padding: 12px 20px; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } } - 方案3:二级菜单折叠(适合后续菜单数量继续增加的场景)
把功能相近的菜单归为一组,点击父菜单展开子菜单,减少一屏展示的条目数量。
完整修改后的CSS代码
/* Add application styles & imports to this file! */ @import '~bootstrap/dist/css/bootstrap.css'; @import url('https://fonts.googleapis.com/css?family=Varela+Round'); @import '~font-awesome/css/font-awesome.css'; html, body { overflow-x: hidden; height: 100%; } body { background: #fff; padding: 0; margin: 0; font-family: 'Varela Round', sans-serif; } .header { display: block; margin: 0 auto; width: 100%; max-width: 100%; box-shadow: none; background-color: cornflowerblue; position: fixed; height: 60px !important; overflow: hidden; z-index: 10; } .main { margin: 0 auto; display: block; height: 100%; margin-top: 60px; } .mainInner { display: table; height: 100%; width: 100%; text-align: center; } .mainInner div { display: table-cell; vertical-align: middle; font-size: 3em; font-weight: bold; letter-spacing: 1.25px; } #sidebarMenu { height: calc(100% - 60px); overflow-y: auto; position: fixed; left: 0; width: 250px; margin-top: 60px; transform: translateX(-250px); transition: transform 250ms ease-in-out; background-color: cornflowerblue; } /* 滚动条美化 */ #sidebarMenu::-webkit-scrollbar { width: 6px; } #sidebarMenu::-webkit-scrollbar-thumb { background-color: rgba(255,255,255,0.2); border-radius: 3px; } .sidebarMenuInner { margin: 0; padding: 0; border-top: 1px solid rgba(255, 255, 255, 0.1); } .sidebarMenuInner li { list-style: none; color: #fff; text-transform: uppercase; font-weight: bold; padding: 20px; cursor: pointer; border-bottom: 1px solid rgba(255, 255, 255, 0.1); } .sidebarMenuInner li span { display: block; font-size: 14px; color: rgba(255, 255, 255, 0.5); } .sidebarMenuInner li a { color: #fff; text-transform: uppercase; font-weight: bold; cursor: pointer; text-decoration: none; } input[type='checkbox']:checked ~ #sidebarMenu { transform: translateX(0); } input[type='checkbox'] { transition: all 0.3s; box-sizing: border-box; display: none; } .sidebarIconToggle { transition: all 0.3s; box-sizing: border-box; cursor: pointer; position: absolute; z-index: 99; height: 100%; width: 100%; top: 22px; left: 15px; height: 22px; width: 22px; } .spinner { transition: all 0.3s; box-sizing: border-box; position: absolute; height: 3px; width: 100%; background-color: #fff; } .horizontal { transition: all 0.3s; box-sizing: border-box; position: relative; float: left; margin-top: 3px; } .diagonal.part-1 { position: relative; transition: all 0.3s; box-sizing: border-box; float: left; } .diagonal.part-2 { transition: all 0.3s; box-sizing: border-box; position: relative; float: left; margin-top: 3px; } input[type='checkbox']:checked ~ .sidebarIconToggle > .horizontal { transition: all 0.3s; box-sizing: border-box; opacity: 0; } input[type='checkbox']:checked ~ .sidebarIconToggle > .diagonal.part-1 { transition: all 0.3s; box-sizing: border-box; transform: rotate(135deg); margin-top: 8px; } input[type='checkbox']:checked ~ .sidebarIconToggle > .diagonal.part-2 { transition: all 0.3s; box-sizing: border-box; transform: rotate(-135deg); margin-top: -9px; } /* 小屏幕高度自适应缩小菜单 */ @media (max-height: 650px) { .sidebarMenuInner li { padding: 14px 20px; font-size: 13px; } }
修改后优先使用滚动方案,所有条目都可正常访问,同时搭配小屏幕自适应缩小规则,在高度足够的设备上可以一屏展示所有内容,无需滚动。
内容的提问来源于stack exchange,提问作者user17313117
相关产品推荐
相关产品推荐

