CSS中使用grid布局实现一套模板多种布局
如上图,在日常开发中我们可能会遇到对页面进行不同布局配置的需求,实现方法也有很多,例如:
1.写多个页面,不同布局拥有不同的页面模板与样式代码,这种方法看起来是最麻烦也最没必要的,而且布局一但多起来编码会变得十分难受且冗余难以维护,特别当业务代码基本一致时,修改时也会变得繁琐,修改一种布局中的业务代码也要考虑到其他布局,显然这种方式是极其不推荐的。
2.写一个页面,页面内编写多套模板,通过条件控制实现不同布局风格。这种方法相比方法1的好处是使得业务代码可以在一处地方编写,并且相同的部分也只需编写一次,后期业务代码维护起来也变得更加容易。当然,方法1也可以通过引入外部文件实现同一套业务代码。然而这种方法问题在于模板跟样式都要编写几套,如果能只控制模板或只控制样式就可以实现的话无疑是更佳的方案。方法3将详细介绍通过grid布局方法编写一套模板多种样式实现布局布局风格控制。
3.写一个页面,通过grid布局将页面划分为合适的网格单元,即根据多种布局风格统一起来选择一个合适的行列分割数量。
页面模板,按照各种布局中网格数最多的编写(即4个)
<template> <div :class="['page-wrap', layoutClass]"> <!-- 布局一 --> <div class="wrap-layout1"> <play-component :loading="loading" :program-info="programsInfo.area1" /> </div> <!-- 布局二 --> <div class="wrap-layout2"> <play-component :loading="loading" :program-info="programsInfo.area2" /> </div> <!-- 布局三 --> <div class="wrap-layout3"> <play-component :loading="loading" :program-info="programsInfo.area3" /> </div> <!-- 布局四 --> <div class="wrap-layout4"> <play-component :loading="loading" :program-info="programsInfo.area4" /> </div> </div> </template>
将页面划分为12行12列共144个网格单元
css样式中编写不同布局的行列划分原则
.page-wrap { //width: 1920px; //height: 1080px; width: 100%; height: 100%; display: grid; grid-gap: 1px 1px; grid-template-columns: repeat(12, 8.333333%); grid-template-rows: repeat(12, 8.333333%); position: relative; background: #FFFFFF; } .wrap-layout1, .wrap-layout2, .wrap-layout3, .wrap-layout4 { background: #D8D8D8; } // 默认布局 .layout-default { .wrap-layout1 { grid-column: 1 / 13; grid-row: 1 / 13; } .wrap-layout2, .wrap-layout3, .wrap-layout4 { display: none; } } // 布局一 .layout1 { .wrap-layout1 { grid-column: 1 / 9; grid-row: 1 / 13; } .wrap-layout2 { grid-column: 9 / 13; grid-row: 1 / 5; } .wrap-layout3 { grid-column: 9 / 13; grid-row: 5 / 9; } .wrap-layout4 { grid-column: 9 / 13; grid-row: 9 / 13; } } // 布局二 .layout2 { .wrap-layout1 { grid-column: 1 / 3; grid-row: 1 / 13; } .wrap-layout2 { grid-column: 3 / 11; grid-row: 1 / 13; } .wrap-layout3 { grid-column: 11 / 13; grid-row: 1 / 13; } .wrap-layout4 { display: none; } } // 布局三 .layout3 { .wrap-layout1 { grid-column: 1 / 13; grid-row: 1 / 3; } .wrap-layout2 { grid-column: 1 / 13; grid-row: 3 / 13; } .wrap-layout3 { display: none; } .wrap-layout4 { display: none; } } // 布局四 .layout4 { .wrap-layout1 { grid-column: 1 / 7; grid-row: 1 / 7; } .wrap-layout2 { grid-column: 7 / 13; grid-row: 1 / 7; } .wrap-layout3 { grid-column: 1 / 7; grid-row: 7 / 13; } .wrap-layout4 { grid-column: 7 / 13; grid-row: 7 / 13; } }
到此这篇关于CSS中使用grid布局实现一套模板多种布局的文章就介绍到这了,更多相关css grid布局内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
相关文章
如何使用CSS的object-position实现图片在img标签中的定位
该文章介绍了CSS中的object-position属性,用于精确控制替换元素在容器内的位置,通过指定水平和垂直方向的偏移量,可以实现精准定位2024-11-08- 文章主要探讨了CSS Grid布局在Internet Explorer(IE)中的不兼容问题,并提供了具体的解决方案和最佳实践,文章首先介绍了CSS Grid布局的基本概念和与传统布局方法的区别,然2024-11-08
- 本文介绍了CSS实现圆角渐变边框的方法,首先设置元素边框为1像素宽度,样式为实线,颜色为透明,然后设置元素边框圆角为10像素,再设置背景剪裁区域和背景绘制区域为内边距和边2024-10-29
- 在CSS布局中,实现元素左右排列有多种方式,Flex布局通过设置margin-left:auto或margin-right:auto实现元素靠右或靠左排列,Grid布局利用grid-template-columns和justify-self2024-10-29
- 在CSS中,隐藏滚动条同时保留滚动功能可以通过设置overflow属性和使用特定的CSS伪元素实现,例如,使用::-webkit-scrollbar针对WebKit浏览器,-ms-overflow-style适用于IE和Edg2024-10-29
- 本文详细介绍了CSS中的border属性及其相关特性,包括border-width(宽度)、border-style(样式)和border-color(颜色)等,此外,还讲述了如何独立控制元素的四个边的边框,2024-10-28
- 本文主要介绍了在网页开发中如何实现“回到顶部”的功能,通过HTML和CSS的编写,可以实现一个浮动在页面右下角的小图标,点击后即可回到页面顶部,这种设计可以提高网站的可用2024-10-28
- 盒子模型是网页布局的基础,包括边框、外边距、内边距和实际内容,通过CSS可以控制盒子之间的距离及其外观,如边框样式、边框颜色等,重要属性包括padding和margin,分别控制内2024-10-18
- 盒子模型是网页布局的基础,包括边框、外边距、内边距和实际内容,通过CSS可以控制盒子之间的距离及其外观,如边框样式、边框颜色等,重要属性包括padding和margin,分别控制内2024-10-18
CSS使用filter和backdrop-filter实现高斯模糊效果(示例代码)
本文详细介绍了CSS3中的两个实现高斯模糊效果的API:filter和backdrop-filter,filter可以直接在图像或背景图上添加多种效果,而backdrop-filter则用于在元素后的区域添加效2024-09-26
最新评论