bootstrap4 网格布局实例_使用Scss敲一个响应式网格布局

使用Scss敲一个响应式网格布局

bootstarp 许多人都用过,是一个非常丰富的框架,那么,现在闲来无事,模仿 bootstrap 敲一个简单的响应式布局框架。

1. 确定响应

确立需要响应的屏幕设备宽度,仿 bootstrap 设计

  1. 超小屏幕的手机 576px
  2. 小型屏幕的平板电脑 768px
  3. 中型屏幕的台式电脑 992px
  4. 大型屏幕的台式电脑 1200px
// 需响应屏幕宽度的变量
$xs: 576px;
$sm: 768px;
$md: 992px;
$lg: 1200px;

2. 确定网格数

bootstrap 是有 12 列的网格,但我往往在开发的时候需要用到更加细致的布局,所以现在我要整个 24 列的。

$column-count: 24;

3. 设置网格 DIV 和每个列的占比

// 设置网格 DIV
.grid {
    display: grid;
    width: 100%;
    grid-template-columns: repeat($column-count,1fr);
}

// 每个列的占比
@for $i from 1 through $column-count {
    .grid-col-#{$i} {
        grid-column: span $i;
        width: 100%;
        box-sizing: border-box;
    }
}

4. 响应式设置, xs sm md lg 四个屏幕的响应式

// xs
@media (min-width: $xs) {
    @for $i from 1 through $column-count {
        .grid-col-xs-#{$i} {
            grid-column: span $i;
        }
    }
}
// sm
@media (min-width: $sm) {
    @for $i from 1 through $column-count {
        .grid-col-sm-#{$i} {
            grid-column: span $i;
        }
    }
}
// md
@media (min-width: $md) {
    @for $i from 1 through $column-count {
        .grid-col-md-#{$i} {
            grid-column: span $i;
        }
    }
}
// lg
@media (min-width: $lg) {
    @for $i from 1 through $column-count {
        .grid-col-lg-#{$i} {
            grid-column: span $i;
        }
    }
}

5. 编译并实践

使用 class

<div class="grid">
<div class="grid-col-24 grid-col-xs-20 grid-col-sm-12 grid-col-md-6 grid-col-lg-1" style="background-color: blueviolet;"></div>
<div class="grid-col-24 grid-col-xs-4 grid-col-sm-12 grid-col-md-18 grid-col-lg-23" style="background-color: red;"></div>
<div class="grid-col-24 grid-col-xs-4 grid-col-sm-12 grid-col-md-18 grid-col-lg-23" style="background-color: yellow;"></div>
<div class="grid-col-24 grid-col-xs-20 grid-col-sm-12 grid-col-md-6 grid-col-lg-1" style="background-color: blue;"></div>
</div>

效果

默认 效果 grid-col

bf7f412f26acc23e378d25a8bfa99ee8.png

1. 超小屏幕的手机 < 576px

537775f72ec80fba018d8ed6a367c9cf.png

2. 小型屏幕的平板电脑 < 768px

fc0dcea055bbfceb9df43365334dbc4e.png

3. 中型屏幕的台式电脑 < 992px

38e9710871a341e2eee679e9fa6024fe.png

4. 大型屏幕的台式电脑 < 1200px

7cf20a813c258683e67b5258741248bb.png

End

GitHub 下载实例​github.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值