bootstrap extend grid system
scroll

Bootstrap 3.3 Extend Grid System (LESS)

 

Did you know that adding XL breakpoint to grid system without touching source code of Bootstrap is possible? Import grid.less-, define some variables, and create one mixin. Let’s dive in.

In almost all cases, xs, sm, md, and lg sizes are enough for the application. You just need to investigate, design, and plan what breakpoints size we need — lg will be the largest one. But unexpected situations happens and some elements can be in need of a grid that adapts to larger screens.

Method 1: We can solve this problem by changing the values of current breakpoints and refactor all markup. It can be painful for developers and the QA team.

Method 2: Extend grid system with one more breakpoint, XL, and start to use col-xl to new elements.

The most uncomfortable are columns (col-sm-1 … col-lg-12) that generate by mixin .make-grid-columns(). This mixin has hardcoded collection and no mechanism for passing new items to this array.

@item: ~”.col-xs-@{index}, .col-sm-@{index}, .col-md-@{index}, .col-lg-@{index}”;

That’s why we can’t extend grid system without editing core files or duplicating some logic in code. Let’s choose the lesser evil — duplicating some code but not editing library source.

Extends grid in LESS without touching lib code

Declare XL variables for breakpoints:

@screen-xl:                  1500px;
@screen-xl-min:              @screen-xl;
@screen-xl-desktop:          @screen-xl-min;
@screen-xl-max:              @screen-xl-min - 1;
@container-xlarge-desktop:   1400px + @grid-gutter-width;
@container-xl:               @container-xlarge-desktop;

Set a new breakpoint for container and generate column, push, pull, offset selectors for XL.

.container {
  @media (min-width: @screen-xl-min) {
    width: @container-xl;
  }
}
@media (min-width: @screen-xl-min) {
  .make-grid(xl);
}

Till now everything is great and elegant! We have declared variables and compile selectors with mixins frombootstrap/mixins/grid-framework.less

Web Solutions

 

But in Grid Framework .make-grid-columns-additional() we can’t use without duplication because it has hard-coded values in @item. 

.make-grid-columns-additional() {
  .col(@index) {
    @item: ~".col-xl-@{index}";
    .col((@index + 1), @item);
  }
  .col(@index, @list) when (@index =< @grid-columns) {
    @item: ~".col-xl-@{index}";
    .col((@index + 1), ~"@{list}, @{item}");
  }
  .col(@index, @list) when (@index > @grid-columns) {
    @{list} {
      position: relative;
      min-height: 1px;
      padding-left:  ceil((@grid-gutter-width / 2));
      padding-right: floor((@grid-gutter-width / 2));
    }
  }
  .col(1);
}
.make-grid-columns-additional();

Inside .make-grid-columns-additional() we can populate @item with as many breakpoints as we want. 

We can now check it importing from bootstrap just variables, mixins and grid.less.

@import "../bower_components/bootstrap/less/variables";
@import "../bower_components/bootstrap/less/mixins";
@import "../bower_components/bootstrap/less/grid";
@screen-xl:                  1500px;
@screen-xl-min:              @screen-xl;
@screen-xl-desktop:          @screen-xl-min;
@screen-xl-max:              @screen-xl-min - 1;
@container-xlarge-desktop:   1400px + @grid-gutter-width;
@container-xl:               @container-xlarge-desktop;
.container {
  @media (min-width: @screen-xl-min) {
    width: @container-xl;
  }
}
@media (min-width: @screen-xl-min) {
  .make-grid(xl);
}
.make-grid-columns-additional() {
  .col(@index) {
    @item: ~".col-xl-@{index}";
    .col((@index + 1), @item);
  }
  .col(@index, @list) when (@index =< @grid-columns) {
    @item: ~".col-xl-@{index}";
    .col((@index + 1), ~"@{list}, @{item}");
  }
  .col(@index, @list) when (@index > @grid-columns) {
    @{list} {
      position: relative;
      min-height: 1px;
      padding-left:  ceil((@grid-gutter-width / 2));
      padding-right: floor((@grid-gutter-width / 2));
    }
  }
  .col(1);
}
.make-grid-columns-additional();

As output, we get just extended grid styles to enjoy.

You can check source code on GitHub: bootstrap-grid-extend

by Svitla Team

Related articles

Web programming toolbox
An Essential Web Programming Toolbox
by Svitla Team
September 26, 2022
article
Modern methodologies of software development
article
System Development Life Cycle - Svitla
SDLC: System Development Life Cycle
by Svitla Team
December 10, 2021
article

Let's discuss your project

We look forward to learning more and consulting you about your product idea or helping you find the right solution for an existing project.

Thank you! We will contact very shortly.

Your message is received. Svitla's sales manager of your region will contact you to discuss how we could be helpful.