Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create new breakpoints in bootstrap 4 using CDN?

Tags:

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.

like image 885
kizoso Avatar asked Feb 22 '18 10:02

kizoso


People also ask

How do I create a media query in Bootstrap?

The general syntax of the media queries in the Bootstrap framework is @media (min-width: ~ breakpoint in pixels here ~) ~ some CSS rules to be applied ~ which narrows the CSS rules defined down to a specific viewport size but eventually the opposite query might be used like @media (max-width: ~ breakpoint in pixels ...

What is Bootstrap 4's smallest breakpoint?

Bootstrap has 4 breakpoints that you can use: . col-sm for larger mobile phones (devices with resolutions ≥ 576px); . col-md for tablets (≥768px);


2 Answers

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

like image 60
Zim Avatar answered Oct 20 '22 05:10

Zim


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.

like image 20
kizoso Avatar answered Oct 20 '22 05:10

kizoso