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

引言

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

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

相关推荐
|晴 天|4 小时前
Vue 3 + TypeScript + Element Plus 博客系统开发总结与思考
前端·vue.js·typescript
猫3285 小时前
v-cloak
前端·javascript·vue.js
旷世奇才李先生5 小时前
Vue 3\+Vite\+Pinia实战:企业级前端项目架构设计
前端·javascript·vue.js
SoaringHeart7 小时前
Flutter进阶:用OverlayEntry 实现所有弹窗效果
前端·flutter
IT_陈寒8 小时前
Vite静态资源加载把我坑惨了
前端·人工智能·后端
herinspace8 小时前
管家婆实用贴-如何分离和附加数据库
开发语言·前端·javascript·数据库·语音识别
小码哥_常9 小时前
从MVC到MVI:一文吃透架构模式进化史
前端
嗷o嗷o9 小时前
Android BLE 的 notify 和 indicate 到底有什么区别
前端
豹哥学前端9 小时前
别再背“var 提升,let/const 不提升”了:揭开暂时性死区的真实面目
前端·面试
lar_slw9 小时前
k8s部署前端项目
前端·容器·kubernetes