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

引言

我们在做官网类项目的时候,设计稿通常是使用按照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);
}

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

相关推荐
哀木2 小时前
一个简单的套壳方案,就能让你的 Agent 少做重复初始化
前端
问心无愧05132 小时前
ctf show web入门27
前端
小村儿2 小时前
给 AI Agent 装上"长期记忆":Karpathy 的 LLM Wiki 思想,我做成了工具
前端·后端·ai编程
竹林8182 小时前
用ethers.js连接MetaMask实现Web3钱包登录:从踩坑到稳定运行的完整记录
前端·javascript
heyCHEEMS2 小时前
如何用 Recast 实现静态配置文件源码级读写
前端·node.js
心连欣2 小时前
从零开始,学习所有指令!
前端·javascript·vue.js
review445432 小时前
大模型和function calling分别是如何工作的
前端
东东同学2 小时前
耗时一个月,我把 Nuxt 首屏性能排障经验做成了一个 AI Skill
前端·agent
冴羽3 小时前
超越 Vibe Coding —— AI 辅助编程指南
前端·ai编程·vibecoding
梦想的颜色3 小时前
一天一个SKILL——前端最佳自动化测试 webapp-testing
前端·web app