Over a million developers have joined DZone.

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.

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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

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

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
bootstrap 3 ,less ,soft skills training ,web dev

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}