实现一个简易的栅格系统

比较简单,但是坑比较多,最好自己动手实现一下。

效果图:

1200px以上的屏幕下

1000px的屏幕

800的屏幕

(解析写在代码注释里好了)

html代码:

<div class="row">
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12 ">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
    <div class="col-lg-1 col-md-6 col-sm-12">.col-md-1</div>
</div>

css代码:

*{
    margin: 0;
    padding: 0;
}

.row:after{                    /* 解决父元素塌陷问题,可以参考我的另一篇博客*/
    content: '';
    display: block;
    clear: both;
}
.row {
    height: auto;
}
div{
    box-sizing: border-box;          /* 把border包含在width中*/
    height: 40px;
    border: 1px solid red;
}

[class*="col-"]{                     /* 匹配所有"col-" 开头的类*/
    float: left;
}

@media (min-width:768px) {           /* 顺序很重要! 不要会覆盖掉*/
    .col-sm-1 {width: 8.33%;}        /* 十二分之一/
    .col-sm-2 {width: 16.66%;}
    .col-sm-3 {width: 25%;}
    .col-sm-4 {width: 33.33%;}
    .col-sm-5 {width: 41.66%;}
    .col-sm-6 {width: 50%;}
    .col-sm-7 {width: 58.33%;}
    .col-sm-8 {width: 66.66%;}
    .col-sm-9 {width: 75%;}
    .col-sm-10 {width: 83.33%;}
    .col-sm-11 {width: 91.66%;}
    .col-sm-12 {width: 100%;}
}

@media (min-width:992px){
   
    .col-md-1 {width: 8.33%;}
    .col-md-2 {width: 16.66%;}
    .col-md-3 {width: 25%;}
    .col-md-4 {width: 33.33%;}
    .col-md-5 {width: 41.66%;}
    .col-md-6 {width: 50%;}
    .col-md-7 {width: 58.33%;}
    .col-md-8 {width: 66.66%;}
    .col-md-9 {width: 75%;}
    .col-md-10 {width: 83.33%;}
    .col-md-11 {width: 91.66%;}
    .col-md-12 {width: 100%;}
}

@media (min-width:1200px){
    .col-lg-1 {width: 8.33%;}
    .col-lg-2 {width: 16.66%;}
    .col-lg-3 {width: 25%;}
    .col-lg-4 {width: 33.33%;}
    .col-lg-5 {width: 41.66%;}
    .col-lg-6 {width: 50%;}
    .col-lg-7 {width: 58.33%;}
    .col-lg-8 {width: 66.66%;}
    .col-lg-9 {width: 75%;}
    .col-lg-10 {width: 83.33%;}
    .col-lg-11 {width: 91.66%;}
    .col-lg-12 {width: 100%;}
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值