当前位置: 首页 > news >正文

不用bootstrap,只用CSS创建网格布局

本文译自【http://j4n.co/blog/Creating-your-own-css-grid-system】,英语好的,可直接查看原网页,不需要翻墙。

翻译拿不准的地方会有英文原文,方便大家理解。

一般的网格布局如下

grid-elements

可以看出主要有以下几个部分

  • a container(容器)
  • rows(行)
  • columns(列)
  • gutters (the space between columns)(列边距)

容器

container

容器的目的就是设置整个网格的宽度,通常设置为100%,但可能要给大屏显示器设置一个最大宽度。

.grid-container {
        width : 100%;
        max-width : 1200px; 
    }

row

 

行是为了将列放在里面,并防止其溢出到其他行中。为了实现这个目的,我们采用清楚浮动的hack技术来确保所有的内容都在行中。

  /*-- our cleafix hack -- */ 
    .row:before, 
    .row:after {
        content:"";
        display: table ;
         clear:both;
    }

column

 

毫无疑问,列是网格布局中最复杂的一部分。首先,在CSS当中,有好几种不同的列的定位方式,而且还要考虑不同的宽度,比如响应式设计。在这里我们将列设定位置,并给出宽度。下一部分再讲响应式设计。

列定位

浮动,行内块,块级表格(display-table),flex,有各种各样的方法。从我个人经验出发,最不容易出错和使用最广泛的就是float了。如果列是空的,将会漂浮在其他元素上面,为了防止这个,我们可以设定一个最小的宽度。

 [class*='col-'] {
        float: left;
        min-height: 1px; 
    }

列宽

为了确定一列的大小,我们可用容器的宽度来除以总的列数。在这里,容器的宽度是100%,假设是6列,那么一列就是100%/6=16.66%。

 [class*='col-'] {
        float: left;
        min-height: 1px; 
        width: 16.66%; 
    }

当然,这只是开始,如果我们想要其他的宽度,可以这样设置:

.col-1{
        width: 16.66%; 
    }
    .col-2{
        width: 33.33%; 
    }
    .col-3{
        width: 50%; 
    }
    .col-4{
        width: 66.664%;
    }
    .col-5{
        width: 83.33%;
    }
    .col-6{
        width: 100%;
    }

我们唯一要注意的就是,不管你最终多少列,各列加起来不能超过容器的宽度。

列边距

 

column-gutters

 

在'border-box' box-sizing模型出现之前,给一个百分比元素一个不可改变的宽度是一件很痛苦的事。幸运的是,用'border-box' box-sizing可以轻易的做到。

Before the 'border-box' box-sizing model, giving percentage-width elements a static padding was a real pain. Luckily, using the 'border-box' model, we can create gutters with ease.

/*-- setting border box on all elements inside the grid --*/
    .grid-container *{
        box-sizing: border-box; 
    }

    [class*='col-'] {
        float: left;
        min-height: 1px; 
        width: 16.66%; 
        /*-- our gutter --*/
        padding: 12px;
    }

(Personally, I use * {box-sizing: border-box;} in my CSS to apply border-box to everything on the page).

(这句不好翻译,主要是还不理解box-sizing)

基本网格布局如下:

<div class="grid-container outline">
    <div class="row">
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
    </div> 
    <div class="row">
        <div class="col-2"><p>col-2</p></div> 
        <div class="col-2"><p>col-2</p></div> 
        <div class="col-2"><p>col-2</p></div> 
    </div> 
    <div class="row">
        <div class="col-3"><p>col-3</p></div> 
        <div class="col-3"><p>col-3</p></div> 
    </div> 
</div>

CSS

.grid-container{
        width: 100%; 
        max-width: 1200px;      
    }

    /*-- our cleafix hack -- */ 
    .row:before, 
    .row:after {
        content:"";
          display: table ;
        clear:both;
    }

    [class*='col-'] {
        float: left; 
        min-height: 1px; 
        width: 16.66%; 
        /*-- our gutter -- */
        padding: 12px; 
        background-color: #FFDCDC;
    }

    .col-1{ width: 16.66%; }
    .col-2{ width: 33.33%; }
    .col-3{ width: 50%;    }
    .col-4{ width: 66.66%; }
    .col-5{ width: 83.33%; }
    .col-6{ width: 100%;   }

    .outline, .outline *{
        outline: 1px solid #F6A1A1; 
    }

    /*-- some extra column content styling --*/
    [class*='col-'] > p {
     background-color: #FFC2C2; 
     padding: 0;
     margin: 0;
     text-align: center; 
     color: white; 
    }

HTML

    <div class="grid-container outline">
        <div class="row">
            <div class="col-1"><p>col-1</p></div> 
            <div class="col-1"><p>col-1</p></div> 
            <div class="col-1"><p>col-1</p></div> 
            <div class="col-1"><p>col-1</p></div> 
            <div class="col-1"><p>col-1</p></div> 
            <div class="col-1"><p>col-1</p></div> 
        </div> 
        <div class="row">
            <div class="col-2"><p>col-2</p></div> 
            <div class="col-2"><p>col-2</p></div> 
            <div class="col-2"><p>col-2</p></div> 
        </div> 
        <div class="row">
            <div class="col-3"><p>col-3</p></div> 
            <div class="col-3"><p>col-3</p></div> 
        </div> 
    </div>

<hr/>

 

制作网格布局

针对手机,调整我们的网格布局非常简单,只需要调整列的宽度就够了。为简单起见,在屏幕小于800px时,将宽度增大一倍。要注意的就是当.col-2 , .col-1,col-5在一行的时候,这时候我们将.col-2 , .col-1占满整行。

The only thing to watch out for is a few exceptions where the last column in the row hangs off the end. Such as in the case of the .col-2 columns and the.col-1 beside the .col-5 column. 
To counter this, we'll make the last .col-2 and .col-1 in the row 100% wide.

@media all and (max-width:800px){
        .col-1{ width: 33.33%;    }
        .col-2{ width: 50%;        }
        .col-3{ width: 83.33%;    }
        .col-4{ width: 100%;    }
        .col-5{ width: 100%;    }
        .col-6{ width: 100%;      }

        .row .col-2:last-of-type{
            width: 100%; 
        }

        .row .col-5 ~ .col-1{
            width: 100%; 
        }
    }

如果屏幕更小,我们继续调整。

 @media all and (max-width:650px){
        .col-1{ width: 50%;        }
        .col-2{ width: 100%;    }
        .col-3{ width: 100%;    }
        .col-4{ width: 100%;    }
        .col-5{ width: 100%;    }
        .col-6{ width: 100%;      }
    }

到这里,我们就创建害了我们的响应式布局:

<div class="grid-container outline">
    <div class="row">
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
        <div class="col-1"><p>col-1</p></div> 
    </div> 
    <div class="row">
        <div class="col-2"><p>col-2</p></div> 
        <div class="col-2"><p>col-2</p></div> 
        <div class="col-2"><p>col-2</p></div> 
    </div> 
    <div class="row">
        <div class="col-3"><p>col-3</p></div> 
        <div class="col-3"><p>col-3</p></div> 
    </div> 
    <div class="row">
        <div class="col-4"><p>col-4</p></div> 
        <div class="col-2"><p>col-2</p></div> 
    </div> 
    <div class="row">
        <div class="col-5"><p>col-5</p></div> 
        <div class="col-1"><p>col-1</p></div> 
    </div> 
    <div class="row">
        <div class="col-6"><p>col-6</p></div> 
    </div> 
</div>

要提醒的是这只是创建网格布局的开始,这还算不上是一个框架或完美的解决方法,但至少让我们觉得采用CSS创建网格布局的过程并不神秘。

转载于:https://www.cnblogs.com/huansky/p/5315528.html

相关文章:

  • 次小生成树模板
  • 最大非连续子序列
  • MongoDB 数据库安装
  • 返回一个整数数组中最大子数组的和
  • 魔兽登录系统
  • 任务栏托盘不消失的问题-有启示
  • OAuth2 基于TP 搭建简单案例
  • __OSX_AVAILABLE_STARTING
  • simpson公式求定积分
  • hdu 1166 敌兵布阵(线段树详解)
  • java获取获得Timestamp类型的当前系统时间。
  • 在SQLServer使用触发器实现数据完整性
  • 软件测试学习日志3 ————软件测试作业之控制流图
  • 【bzoj1046】[HAOI2007]上升序列
  • 关于网站优化
  • Golang-长连接-状态推送
  • iOS | NSProxy
  • js数组之filter
  • Linux gpio口使用方法
  • Redis学习笔记 - pipline(流水线、管道)
  • spark本地环境的搭建到运行第一个spark程序
  • SQL 难点解决:记录的引用
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 产品三维模型在线预览
  • 对象管理器(defineProperty)学习笔记
  • 后端_MYSQL
  • 跨域
  • 如何合理的规划jvm性能调优
  • 微服务核心架构梳理
  • 一道面试题引发的“血案”
  • 我们雇佣了一只大猴子...
  • ​2020 年大前端技术趋势解读
  • ​HTTP与HTTPS:网络通信的安全卫士
  • ​业务双活的数据切换思路设计(下)
  • #Spring-boot高级
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • #前后端分离# 头条发布系统
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (Python第六天)文件处理
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (十一)手动添加用户和文件的特殊权限
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • ... 是什么 ?... 有什么用处?
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?