带鱼屏页面该怎么适配?看我的

引言

我们在做官网类项目的时候,设计稿通常是使用按照1920*1080(16:9)的分辨率来设计的,但是电脑端的浏览器窗口宽度是不固定的,用户可能会根据使用场景随意拖动浏览器窗口宽高比,就会导致本来应该好好的布局出现遮挡、错位等情况,特别的

作为一名强迫症前端,绝对不允许这么丑的页面在我的coding下出现!!

1K(1920 * 1080)、2K(2560 * 1440)、4K(3840 * 2160)、带鱼屏(3440 * 1440)的屏幕,宽高比分别是16:9、16:9、16:9、21:9,首先考虑21:9的带鱼屏,如果保持比例缩放,底部会被遮挡不保持比例缩放又会使上下元素挤在一起,非常棘手! 再或者用户改变浏览器窗口整出来一个1:1的窗口,不就炸了嘛。

实现思路

找到一个和我想象适配方案的几乎一模一样的网站,绝区零 官网,

  1. 设置一个21:9的容器,上下左右居中
  2. 在容器中再添加一个16:9容器,在父级容器中左右居中
  3. 在16:9的容器中进行开发内容

动图展示

参考代码

html 复制代码
<body>
    <div class="box">
        <div class="container"></div>
    </div>
</body>
css 复制代码
html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
}

.box {
    width: auto;
    height: auto;
    max-width: 100vw;
    max-height: 100vh;
    aspect-ratio: 21 / 9;
    background-color: #e0e0e0;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

@media (max-aspect-ratio: 21/9) {
    .box {
        width: 100vw;
        height: calc(100vw * 9 / 21);
    }
}
@media (min-aspect-ratio: 21/9) {
    .box {
        height: 100vh;
        width: calc(100vh * 21 / 9);
    }
}

.container {
    margin: 0 auto;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 16 / 9;
    background-color: #ffffff;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

成果: 借绝区零官网效果展示(侵权删):

相关推荐
huohaiyu34 分钟前
从URL到页面的完整解析流程
前端·网络·chrome·url
阿星AI工作室3 小时前
一个简单Demo彻底理解前后端怎么连的丨Figma + Supabase + Vercel
前端·人工智能
aircrushin3 小时前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海5 小时前
JSBridge 原理详解
前端
孟健5 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion5 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计
IT枫斗者7 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
N***p3657 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
享誉霸王8 小时前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5
a1117768 小时前
飞机躲避炸弹 网页游戏
前端·开源·html·threejs