【问题标题】:How to customize Bootstrap 4 using SASS for different grid columns and breakpoints?如何使用 SASS 为不同的网格列和断点自定义 Bootstrap 4?
【发布时间】:2019-03-02 07:01:54
【问题描述】:

各位开发者,

我试图在大尺寸上获得 12 列和 24 个装订线尺寸,而在一个容器视图中中等和较低时获得 6 列和 18 个装订线,而另一个容器视图在大尺寸上将是 15 列和 12 个装订线在较低的视图上相应地调整大小??

这是我可以在前者上实现的(12 col 24 gutters,6 columns 18 gutters):

background-grid {

@include make-row();

@include make-col-ready;



@each $breakpoint in map-keys($grid-breakpoints) {

    @include media-breakpoint-up($breakpoint) {

        //@include make-col(map-get($dani-number-col, $breakpoint ));

        .row.no-gutters {

            margin-right: map-get($dani-row-no-gutters, $breakpoint );

            margin-left: map-get($dani-row-no-gutters, $breakpoint );

        }         

        .row > .col,

        .row > [class*="col-"] {

            margin-right: map-get($dani-gutters, $breakpoint );

            margin-left: map-get($dani-gutters , $breakpoint );

        }

    }

}

}

虽然我尝试创建另一个容器类来处理另一个视图(15 列,6 个排水沟),但我无法理解它。主要是因为我对 CSS 和 SASS 的理解可能比较稀疏,尝试了谷歌搜索,但似乎没有合适的教程?

.foreground-container {

width: 1428px;

@include make-foreground-container();



@each $breakpoint in map-keys($grid-breakpoints) {

    @include media-breakpoint-up($breakpoint) {

        //@include make-col(map-get($dani-number-col, $breakpoint ));

        // @include make-col(15);

        .foreground-row {

            margin-right: map-get($dani-row-no-gutters, $breakpoint );

            margin-left: map-get($dani-row-no-gutters, $breakpoint );

        }         

        .foreground-row > .foreground-col,

        .foreground-row > [class*="foreground-col"] {

            margin-right: map-get($dani-row-no-gutters, $breakpoint );

            margin-left: map-get($dani-row-no-gutters, $breakpoint );

        }

    }

}

}

.foreground-row {

    @include make-foreground-row();

    // @include make-col(12);

}



.foreground-col {

    @include make-col-ready();

    @include make-col(15);

}

HTML、CSS

<div class="background-container">
  <div class="container">
    <div class="row">
      <div class="col">col1</div>
      <div class="col">col2</div>
      <div class="col">col3</div>
    </div>
  </div>
</div>

<div class="foreground-container">
  <div class="container">
    <div class="row">
      <div class="col">col1</div>
      <div class="col">col2</div>
      <div class="col">col3</div>
    </div>
  </div>
</div>

这甚至可能吗?如果是这样,如何以及如果不是实现我想要实现的目标的好方法?

谢谢。

【问题讨论】:

    标签: css twitter-bootstrap sass bootstrap-4


    【解决方案1】:

    您可以制作自定义 mixin 以在自定义容器内重新生成适当的断点列...

    @mixin make-grid-columns-custom($columns: $grid-columns, $gutter: $grid-gutter-width, $breakpoints: $grid-breakpoints) {
    
      @each $breakpoint in map-keys($breakpoints) {
        $infix: breakpoint-infix($breakpoint, $breakpoints);
    
        // only override lg and up
        @include media-breakpoint-up('lg', $breakpoints) {
          @for $i from 1 through $columns {
            .col#{$infix}-#{$i} {
              padding-right: ($gutter / 2);
              padding-left: ($gutter / 2);
              @include make-col($i, $columns);
            }
          }
    
        }
      }
    }
    
    @import "bootstrap";
    
    $foreground-container-max-widths: (
      sm: 600px,
      md: 800px,
      lg: 1000px,
      xl: 1428px
    );
    
    /* make the container for the custom grid */
    .foreground-container > .container {
        @include make-container();
        @include make-container-max-widths($foreground-container-max-widths, $grid-breakpoints);
    }
    
    .foreground-container {
        /*  custom col for all breakpoints */
        @include make-grid-columns(6, 3px, $grid-breakpoints);
    
        /* override lg and up using custom mixin */
        @include make-grid-columns-custom(15, 6px, $grid-breakpoints);
    }
    

    演示:https://www.codeply.com/go/SLmHas8zEZ


    相关:Define different number of Bootstrap 4 columns for some part (div) of page only?

    【讨论】:

    • 哇,这很好,不是吹毛求疵,但我看到列之间的排水沟仍然采用 15px 左右的默认引导排水沟,而我看到你指定 6px 网格排水沟宽度。不生效吗?
    • 是的,您需要在自定义 mixin 中将 padding- 设置为 $gutter/2。我更新了答案和演示:codeply.com/go/SLmHas8zEZ
    • 哦,有道理。似乎我对使用带有引导程序的 SASS 尤其是自定义它的理解严重不足。你对阅读有什么建议可以加强我的知识吗?
    • 谢谢,这个article I wrote 至少可以从 Bootstrap 的角度帮助您入门,并且还展示了一些其他 SASS 资源。
    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2020-11-03
    • 2018-10-18
    • 2017-11-13
    • 1970-01-01
    • 2015-09-25
    相关资源
    最近更新 更多