How to create new breakpoints in bootstrap 4 using CDN?

26,393

Solution 1

I'm very surprised that there was no developer able to answer my question! Even on the github no one dared to think about it!

In fact, everything turned out to be very simple!

Yes, using the CDN we get the compiled css file. Styles in the bootstrap are written using sass. Moreover, the styles are written separation and modular. So it means that I do not need to load the entire bootstrap to my server. I want to deliver cached version of Bootstrap’s compiled CSS and I only need to add my breakpoints. Fortunately, there is a specific bootstrap file which is responsible for the Grid. It is bootstrap-grid.scss:

/*!
 * Bootstrap Grid v4.0.0 (https://getbootstrap.com)
 * Copyright 2011-2018 The Bootstrap Authors
 * Copyright 2011-2018 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

@at-root {
  @-ms-viewport { width: device-width; } // stylelint-disable-line at-rule-no-vendor-prefix
}

html {
  box-sizing: border-box;
  -ms-overflow-style: scrollbar;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

@import "functions";
@import "variables";

@import "mixins/breakpoints";
@import "mixins/grid-framework";
@import "mixins/grid";

@import "grid";
@import "utilities/display";
@import "utilities/flex";

Now I just need to add sequentially the code from the files above adding my breakpoints. Add non-Grid code not necessary. For example, the code responsible for the color. Here is my mcve at codepen.

Solution 2

It can't be done entirely from CDN. To properly customize/override using SASS, you need to @import the necessary Bootstrap scss files in your custom.scss. To override the grid-breakpoints, at a minimum you need functions and variables. Then set the variables as needed, and finally @import bootstrap. Notice how default! has been removed as explained in the docs as the correct customization method.

/* import what we need to override */
@import "bootstrap/functions";
@import "bootstrap/variables";

/* set the overriding variables */
$grid-breakpoints: (
  xxxs: 0,
  xxs: 320px,
  xs: 568px,
  sm: 667px,
  md: 768px,
  lg: 992px,
  xl: 1200px,
  xxl: 1440px,
  xxxl: 1600px
);
$container-max-widths: (
  xxxs: 0,
  xxs: 320px,
  xs: 568px,
  sm: 667px,
  md: 768px,
  lg: 992px,
  xl: 1200px,
  xxl: 1440px,
  xxxl: 1600px
);

/* override the !default vars with the values we set above */
@import "bootstrap";

With this method, we've added the new grid breakpoints, and ensured these new breakpoints work everywhere in Bootstrap including the grid, responsive utilities for spacing, display, flexbox, alignment, positioning, etc...

https://codeply.com/go/BIgmm1XGc2

Also see:
How to extend/modify (customize) Bootstrap 4 with SASS
Twitter Bootstrap: add media queries for xxs breakpoint

Solution 3

As we already now - using the CDN we get the compiled .css.

Faced the same problem and made a little CSS extension.

For each additional breakpoint, in your .css stylesheet, which is loaded after original bootstrap 4 CDN .css files (to overwrite container max-width), adding a code below:

/* BS4 Grid XXL breakpoint */

/* adding col-xxl */
.col-xxl, .col-xxl-1, .col-xxl-2, .col-xxl-3, .col-xxl-4, .col-xxl-5, .col-xxl-6, .col-xxl-7, .col-xxl-8, .col-xxl-9, .col-xxl-10, .col-xxl-11, .col-xxl-12, .col-xxl-auto {position: relative; width: 100%; padding-right: 15px; padding-left: 15px;}

@media (min-width: 1570px){
  /* expanding BS4 max-width */
  .container {max-width: 1540px;}

  /* grid columns xxl */
  .col-xxl {-ms-flex-preferred-size: 0; flex-basis: 0; -webkit-box-flex: 1; -ms-flex-positive: 1; flex-grow: 1; max-width: 100%;}
  .col-xxl-auto {-webkit-box-flex: 0; -ms-flex: 0 0 auto; flex: 0 0 auto; width: auto; max-width: none;}  
  .col-xxl-1 {-webkit-box-flex: 0; -ms-flex: 0 0 8.333333%; flex: 0 0 8.333333%; max-width: 8.333333%;}
  .col-xxl-2 {-webkit-box-flex: 0; -ms-flex: 0 0 16.666667%; flex: 0 0 16.666667%; max-width: 16.666667%;}
  .col-xxl-3 {-webkit-box-flex: 0; -ms-flex: 0 0 25%; flex: 0 0 25%; max-width: 25%;}
  .col-xxl-4 {-webkit-box-flex: 0; -ms-flex: 0 0 33.333333%; flex: 0 0 33.333333%; max-width: 33.333333%;}
  .col-xxl-5 {-webkit-box-flex: 0; -ms-flex: 0 0 41.666667%; flex: 0 0 41.666667%; max-width: 41.666667%;}
  .col-xxl-6 {-webkit-box-flex: 0; -ms-flex: 0 0 50%; flex: 0 0 50%; max-width: 50%;}
  .col-xxl-7 {-webkit-box-flex: 0; -ms-flex: 0 0 58.333333%; flex: 0 0 58.333333%; max-width: 58.333333%;}
  .col-xxl-8 {-webkit-box-flex: 0; -ms-flex: 0 0 66.666667%; flex: 0 0 66.666667%; max-width: 66.666667%;}
  .col-xxl-9 {-webkit-box-flex: 0; -ms-flex: 0 0 75%; flex: 0 0 75%; max-width: 75%;}
  .col-xxl-10 {-webkit-box-flex: 0; -ms-flex: 0 0 16.666667%; flex: 0 0 16.666667%; max-width: 16.666667%;}
  .col-xxl-11 {-webkit-box-flex: 0; -ms-flex: 0 0 16.666667%; flex: 0 0 16.666667%; max-width: 16.666667%;}
  .col-xxl-12 {-webkit-box-flex: 0; -ms-flex: 0 0 16.666667%; flex: 0 0 16.666667%; max-width: 16.666667%;}

  /* order xxl */
  .order-xxl-0 {-webkit-box-ordinal-group: 1; -ms-flex-order: 0; order: 0;}
  .order-xxl-1 {-webkit-box-ordinal-group: 2; -ms-flex-order: 1; order: 1;}
  .order-xxl-2 {-webkit-box-ordinal-group: 3; -ms-flex-order: 2; order: 2;}
  .order-xxl-3 {-webkit-box-ordinal-group: 4; -ms-flex-order: 3; order: 3;}
  .order-xxl-4 {-webkit-box-ordinal-group: 5; -ms-flex-order: 4; order: 4;}
  .order-xxl-5 {-webkit-box-ordinal-group: 6; -ms-flex-order: 5; order: 5;}
  .order-xxl-6 {-webkit-box-ordinal-group: 7; -ms-flex-order: 6; order: 6;}
  .order-xxl-7 {-webkit-box-ordinal-group: 8; -ms-flex-order: 7; order: 7;}
  .order-xxl-8 {-webkit-box-ordinal-group: 9; -ms-flex-order: 8; order: 8;}
  .order-xxl-9 {-webkit-box-ordinal-group: 10; -ms-flex-order: 9; order: 9;}
  .order-xxl-10 {-webkit-box-ordinal-group: 11; -ms-flex-order: 10; order: 10;}
  .order-xxl-11 {-webkit-box-ordinal-group: 12; -ms-flex-order: 11; order: 11;}
  .order-xxl-12 {-webkit-box-ordinal-group: 13; -ms-flex-order: 12; order: 12;}

  /* text-align xxl */
  .text-xxl-center {text-align: center!important;}
  .text-xxl-left {text-align: left!important;}
  .text-xxl-right {text-align: right!important;}  

  /* float xxl */
  .float-xxl-left {float: left!important;}
  .float-xxl-right {float: right!important;}
  .float-xxl-none {float: none!important;}

  /* display xxl */
  .d-xxl-none {display: none!important;}
  .d-xxl-inline {display: inline!important;}
  .d-xxl-inline-block {display: inline-block!important;}
  .d-xxl-block {display: block!important;}
  .d-xxl-table {display: table!important;}
  .d-xxl-table-cell {display: table-cell!important;}
  .d-xxl-table-row {display: table-row!important;}
  .d-xxl-flex {display: flex!important;}
  .d-xxl-inline-flex {display: inline-flex!important;}

  /* offsets xxl */
  .offset-xxl-1 {margin-left: 8.333333%;}
  .offset-xxl-2 {margin-left: 16.666667%;}
  .offset-xxl-3 {margin-left: 25%;}
  .offset-xxl-4 {margin-left: 33.333333%;}
  .offset-xxl-5 {margin-left: 41.666667%;}
  .offset-xxl-6 {margin-left: 50%;}
  .offset-xxl-7 {margin-left: 58.333333%;}
  .offset-xxl-8 {margin-left: 66.666667%;}
  .offset-xxl-9 {margin-left: 75%;}
  .offset-xxl-10 {margin-left: 83.333333%;}
  .offset-xxl-11 {margin-left: 91.666667%;}

  /* spacing xxl */
  .m-xxl-0{margin:0!important}.mt-xxl-0,.my-xxl-0{margin-top:0!important}.mr-xxl-0,.mx-xxl-0{margin-right:0!important}.mb-xxl-0,.my-xxl-0{margin-bottom:0!important}.ml-xxl-0,.mx-xxl-0{margin-left:0!important}.m-xxl-1{margin:.25rem!important}.mt-xxl-1,.my-xxl-1{margin-top:.25rem!important}.mr-xxl-1,.mx-xxl-1{margin-right:.25rem!important}.mb-xxl-1,.my-xxl-1{margin-bottom:.25rem!important}.ml-xxl-1,.mx-xxl-1{margin-left:.25rem!important}.m-xxl-2{margin:.5rem!important}.mt-xxl-2,.my-xxl-2{margin-top:.5rem!important}.mr-xxl-2,.mx-xxl-2{margin-right:.5rem!important}.mb-xxl-2,.my-xxl-2{margin-bottom:.5rem!important}.ml-xxl-2,.mx-xxl-2{margin-left:.5rem!important}.m-xxl-3{margin:1rem!important}.mt-xxl-3,.my-xxl-3{margin-top:1rem!important}.mr-xxl-3,.mx-xxl-3{margin-right:1rem!important}.mb-xxl-3,.my-xxl-3{margin-bottom:1rem!important}.ml-xxl-3,.mx-xxl-3{margin-left:1rem!important}.m-xxl-4{margin:1.5rem!important}.mt-xxl-4,.my-xxl-4{margin-top:1.5rem!important}.mr-xxl-4,.mx-xxl-4{margin-right:1.5rem!important}.mb-xxl-4,.my-xxl-4{margin-bottom:1.5rem!important}.ml-xxl-4,.mx-xxl-4{margin-left:1.5rem!important}.m-xxl-5{margin:3rem!important}.mt-xxl-5,.my-xxl-5{margin-top:3rem!important}.mr-xxl-5,.mx-xxl-5{margin-right:3rem!important}.mb-xxl-5,.my-xxl-5{margin-bottom:3rem!important}.ml-xxl-5,.mx-xxl-5{margin-left:3rem!important}
  .p-xxl-0{padding:0!important}.pt-xxl-0,.py-xxl-0{padding-top:0!important}.pr-xxl-0,.px-xxl-0{padding-right:0!important}.pb-xxl-0,.py-xxl-0{padding-bottom:0!important}.pl-xxl-0,.px-xxl-0{padding-left:0!important}.p-xxl-1{padding:.25rem!important}.pt-xxl-1,.py-xxl-1{padding-top:.25rem!important}.pr-xxl-1,.px-xxl-1{padding-right:.25rem!important}.pb-xxl-1,.py-xxl-1{padding-bottom:.25rem!important}.pl-xxl-1,.px-xxl-1{padding-left:.25rem!important}.p-xxl-2{padding:.5rem!important}.pt-xxl-2,.py-xxl-2{padding-top:.5rem!important}.pr-xxl-2,.px-xxl-2{padding-right:.5rem!important}.pb-xxl-2,.py-xxl-2{padding-bottom:.5rem!important}.pl-xxl-2,.px-xxl-2{padding-left:.5rem!important}.p-xxl-3{padding:1rem!important}.pt-xxl-3,.py-xxl-3{padding-top:1rem!important}.pr-xxl-3,.px-xxl-3{padding-right:1rem!important}.pb-xxl-3,.py-xxl-3{padding-bottom:1rem!important}.pl-xxl-3,.px-xxl-3{padding-left:1rem!important}.p-xxl-4{padding:1.5rem!important}.pt-xxl-4,.py-xxl-4{padding-top:1.5rem!important}.pr-xxl-4,.px-xxl-4{padding-right:1.5rem!important}.pb-xxl-4,.py-xxl-4{padding-bottom:1.5rem!important}.pl-xxl-4,.px-xxl-4{padding-left:1.5rem!important}.p-xxl-5{padding:3rem!important}.pt-xxl-5,.py-xxl-5{padding-top:3rem!important}.pr-xxl-5,.px-xxl-5{padding-right:3rem!important}.pb-xxl-5,.py-xxl-5{padding-bottom:3rem!important}.pl-xxl-5,.px-xxl-5{padding-left:3rem!important}
  .m-xxl-auto{margin:auto!important}.mt-xxl-auto,.my-xxl-auto{margin-top:auto!important}.mr-xxl-auto,.mx-xxl-auto{margin-right:auto!important}.mb-xxl-auto,.my-xxl-auto{margin-bottom:auto!important}.ml-xxl-auto,.mx-xxl-auto{margin-left:auto!important}

}

Now you will get full-supported XXL breakpoint. I know it's not the clearest solution, because we just overwriting Sass compiled code. If you have an ability to use Sass - do it, this way is more correct. But using CDN, like a quic-fix - can use my solution.

Solution 4

According to Github it seems you're running into a 'bug' here. See here: https://github.com/sass/sass/issues/1166

That being said you'd have to write your variable definition in a single line just like that.

$grid-breakpoints: (xxxs: 0, xxs: 320px, xs: 568px, sm: 667px, md: 768px, lg: 992px, xl: 1200px, xxl: 1440px, xxxl: 1600px) !default

Solution 5

If you need just some part of CDN bootstrap adjusted and don't want to recompile you can add only relevant media boundaries. It's easier if you're adding breakpoints higher than -xl- (you load your .css update after you load bootstrap .css) or lower than xs (you load your .css update before bootstrap). Inserting a breakpoint in the middle is a little bit more tricky.

See https://www.codeply.com/p/ROF99teYDd where I've added -xxl- starting at 1550px and -mde- for 840-992px, purely in .css by duplicating required bootstrap media csss.

@media (min-width: 840px) and (max-width: 992px) {
 ...
 .col-mde-1 {
 -ms-flex: 0 0 8.333333%;
 flex: 0 0 8.333333%;
 max-width: 8.333333%;
 }
 .col-mde-2 {
   -ms-flex: 0 0 16.666667%;
   flex: 0 0 16.666667%;
   max-width: 16.666667%;
 }
 ...
 .col-mde-12 {
  -ms-flex: 0 0 100%;
  flex: 0 0 100%;
  max-width: 100%;
 }
}

@media (min-width: 1550px) {
 .col-xxl-1 {
   -ms-flex: 0 0 8.333333%;
   flex: 0 0 8.333333%;
   max-width: 8.333333%;
 }
...
}

Because of using min and max-width for the inserted elements, I need to specify the official bootstrap dimension for -lg- (min-width: 992px) to ensure the design doesn't break after this breakpoint.

Share:
26,393

Related videos on Youtube

kizoso
Author by

kizoso

Updated on July 09, 2022

Comments

  • kizoso
    kizoso almost 2 years

    I use BootstrapCDN. Other styles written in sass and built by gulp. I need to create my own breakpionts. Is it possible to make them if I use CDN? I can't figure out how to do it. I have to create these breakpoints:

    --breakpoint-xxxs: 0;
    --breakpoint-xxs: 320px;
    --breakpoint-xs: 568px;
    --breakpoint-sm: 667px;
    --breakpoint-md: 768px;
    --breakpoint-lg: 992px;
    --breakpoint-xl: 1200px;
    --breakpoint-xxl: 1440px;
    --breakpoint-xxxl: 1600px;
    

    I want to get something like this:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <div class="container">
    	<div class="row">
    		<div class="col col-xxxs-1 col-xxs-2 col-xs-3 col-sm-4 col-md-5 col-lg-6 col-xl-7 col-xxl-8 col-xxxl-9">
    			<div style="height:100vh;background:purple">text</div>
    		</div><!--col-->
    	</div><!--.row-->
    </div><!--.container-->

    I found the manual and I'm trying this:

    $grid-breakpoints: (
      xxxs: 0,
      xxs: 320px,
      xs: 568px,
      sm: 667px,
      md: 768px,
      lg: 992px,
      xl: 1200px,
      xxl: 1440px,
      xxxl: 1600px
    )  !default;
    
    $container-max-widths: (
      xxxs: 0,
      xxs: 320px,
      xs: 568px,
      sm: 667px,
      md: 768px,
      lg: 992px,
      xl: 1200px,
      xxl: 1440px,
      xxxl: 1600px
    ) !default;
    
    :root {
      --breakpoint-xxxs: 0;
      --breakpoint-xxs: 320px;
      --breakpoint-xs: 568px;
      --breakpoint-sm: 667px;
      --breakpoint-md: 768px;
      --breakpoint-lg: 992px;
      --breakpoint-xl: 1200px;
      --breakpoint-xxl: 1440px;
      --breakpoint-xxxl: 1600px;
    }
    

    But it doesn't produce results, and generates bug:

    Illegal nesting: Nothing may be nested beneath variable declarations.

    Codepen mcve.

    What I'm doing wrong?
    Thank you in advance for your help.


    UPD: if that is not possible... Is there any alternative? Can I easily edit my code to simulate bootstrap grid with my breakpoints?


    UPD2: I fixed the bugs thanks to @aer0:

    $grid-breakpoints: (xxxs: 0, xxs: 320px, xs: 568px, sm: 667px, md: 768px, lg: 992px, xl: 1200px, xxl: 1440px, xxxl: 1600px)!default
    
    $container-max-widths: (xxxs: 0, xxs: 320px, xs: 568px, sm: 667px, md: 768px, lg: 992px, xl: 1200px, xxl: 1440px, xxxl: 1600px)!default
    
    \:root
      --breakpoint-xxxs: 0
      --breakpoint-xxs: 320px
      --breakpoint-xs: 568px
      --breakpoint-sm: 667px
      --breakpoint-md: 768px
      --breakpoint-lg: 992px
      --breakpoint-xl: 1200px
      --breakpoint-xxl: 1440px
      --breakpoint-xxxl: 1600px
    

    But it doesn't solve my problem.

  • kizoso
    kizoso about 6 years
    Thank you! it is fixing the bug, but doesn't solve my problem
  • Zim
    Zim about 6 years
    You did your research on this, although I thought your question meant literally from CDN, not using SASS. This is a solution using SASS (not entirely from CDN). It's not the correct way to customize Bootstrap since it's altering the default breakpoint values. The correct method is to @import bootstrap, and then override the variable/maps in your own custom.scss which is already explained in this answer
  • Zim
    Zim about 6 years
    You'll find the smallest breakpoint isn't working, and none of the supporting grid utility classes (display, spacing, flexbox, etc...) will work when customizing this way.
  • kizoso
    kizoso about 6 years
    @ZimSystem, At the very beginning of the article, I wrote that I use sass
  • kizoso
    kizoso about 6 years
    @ZimSystem, You are right about the .xxxs class. Because in myxin, the smallest size removes the prefix. I fixed it, html instead of col-xxxs-1, I wrote col-1.
  • kizoso
    kizoso about 6 years
    Thanks for the answer! But I can not load the entire bootstrap on the job. It must necessarily be downloaded via CDN. "The customer is always right". Although this may be useful to someone else
  • Dustin Graham
    Dustin Graham about 5 years
    You must start grid breakpoints at 0, but I'd suggest removing the container max width for xxxs otherwise it needlessly breaks the layout between 0 and 319px.
  • Burndog
    Burndog almost 5 years
    It's important to know that using this method relieves the need to link to the original bootstrap.css. Basically the @import "bootstrap"; pulls in the original bootstrap.css and appends it prior to any custom code you have in the css file doing the import.
  • Ripper
    Ripper over 3 years
    thx, there is small error at .col-xxl-10, 11, 12 And it is missing some container stuff: .container-xxl { width: 100%; padding-right: 15px; padding-left: 15px; margin-right: auto; margin-left: auto; } @media (min-width: 1570px){ .container, .container-sm, .container-md, .container-lg, .container-xl, .container-xxl {max-width: 1540px;} }