Bootstrap 3: How to create a new grid option for 480-767px

I have been working with responsive frameworks for quite a while and without any doubt, Bootstrap is my favorite responsive framework so far. But there are always a few occasions when you need to extend the capability of Bootstrap yourself to meet your special requirements. For example, creating an extra grid for 480-767px. But why would I need this extra grid? As of Bootstrap 3, the smallest grid, Extra Small (class prefix .col-xs- ) extends from 1 to 767 px in width. That is a very large range compared to other grids: Small (class prefix .col-sm- ) extending from 768-991px, Medium (class prefix .col-md- ) extending from 992-1199px and Large (class prefix .col-lg- ) extending beyond 1200px. Now, consider two scenarios:

1. If you want to create more than one columns for Extra Small, they may look great in screen width close to 767px, but when you go below 480px, they look really clumsy.

2. If you use small (e.g. 500px wide) preview images with 100% width that look good in smaller resolution, then they start looking too big and not to mention, pixelated. Again, if you use max-width and center align to adjust for them, the space on both sides ruins the aesthetic value of the design.

Those being said, I once got into a situation where I needed to define an extra grid for 480-767px to avoid adjusting the widths of elements with unnecessary media queries at every step. So, if any of you want this extra Bootstarp grid as well, here is how to achieve this.

Defining the grid structure: First of all, let’s define the grid. Our new grid will be for 480-767px width. We will push the default Bootstrap Extra Small (class prefix .col-xs- ) to support 1-479px instead of 1-767px. Now, to make it easier to remember, we will name the grid as Medium Small, which customizes the Bootstrap grids as Extra Small, Medium Small, Small, Medium, Large.

Writing up the CSS: To make our new column classes similar to other Bootstarp column classes, we will use the class prefix .col-ms- for our new classes. Let’s create a new CSS file in the css directory of your project, name it as bootstrap-480-767-grid.css , and the following code inside it.


/**
* Custom Bootstrap 3 grid for 480-767px layout
* Author: Iftakhar Hasan
*
* A new set of column classes (with class prefix .col-ms- ) for Bootstrap 3
* to make Medium Small grids for supporting 480-767px
*
* This will push the default Bootstrap 3 Extra Small column (class prefix .col-xs- )
* to support 1-479px instead of 1-767px
*/
.col-ms-1,
.col-ms-2,
.col-ms-3,
.col-ms-4,
.col-ms-5,
.col-ms-6,
.col-ms-7,
.col-ms-8,
.col-ms-9,
.col-ms-10,
.col-ms-11,
.col-ms-12 {position:relative; padding-left:15px; padding-right:15px;}
@media (min-width: 480px) and (max-width: 767px) {
.container {max-width:748px;}
.col-ms-1,
.col-ms-2,
.col-ms-3,
.col-ms-4,
.col-ms-5,
.col-ms-6,
.col-ms-7,
.col-ms-8,
.col-ms-9,
.col-ms-10,
.col-ms-11 {float:left;}
.col-ms-1 {width:8.33333333%;}
.col-ms-2 {width:16.66666667%;}
.col-ms-3 {width:25%;}
.col-ms-4 {width:33.33333333%;}
.col-ms-5 {width:41.66666667%;}
.col-ms-6 {width:50%;}
.col-ms-7 {width:58.33333333%;}
.col-ms-8 {width:66.66666667%;}
.col-ms-9 {width:75%;}
.col-ms-10 {width:83.33333333%;}
.col-ms-11 {width:91.66666667%;}
.col-ms-12 {width:100%;}
.col-ms-pull-0 {right:auto;}
.col-ms-pull-1 {right:8.33333333%;}
.col-ms-pull-2 {right:16.66666667%;}
.col-sm-pull-3 {right:25%;}
.col-sm-pull-4 {right:33.33333333%;}
.col-sm-pull-5 {right:41.66666667%;}
.col-sm-pull-6 {right:50%;}
.col-sm-pull-7 {right:58.33333333%;}
.col-ms-pull-8 {right:66.66666667%;}
.col-ms-pull-9 {right:75%;}
.col-ms-pull-10 {right:83.33333333%;}
.col-ms-pull-11 {right:91.66666667%;}
.col-ms-pull-12 {right:100%;}
.col-ms-push-0 {left:auto;}
.col-ms-push-1 {left:8.33333333%;}
.col-ms-push-2 {left:16.66666667%;}
.col-sm-push-3 {left:25%;}
.col-sm-push-4 {left:33.33333333%;}
.col-sm-push-5 {left:41.66666667%;}
.col-sm-push-6 {left:50%;}
.col-sm-push-7 {left:58.33333333%;}
.col-ms-push-8 {left:66.66666667%;}
.col-ms-push-9 {left:75%;}
.col-ms-push-10 {left:83.33333333%;}
.col-ms-push-11 {left:91.66666667%;}
.col-ms-push-12 {left:100%;}
.col-ms-offset-0 {margin-left:0;}
.col-ms-offset-1 {margin-left:8.33333333%;}
.col-ms-offset-2 {margin-left:16.66666667%;}
.col-ms-offset-3 {margin-left:25%;}
.col-ms-offset-4 {margin-left:33.33333333%;}
.col-ms-offset-5 {margin-left:41.66666667%;}
.col-ms-offset-6 {margin-left:50%;}
.col-ms-offset-7 {margin-left:58.33333333%;}
.col-ms-offset-8 {margin-left:66.66666667%;}
.col-ms-offset-9 {margin-left:75%;}
.col-ms-offset-10 {margin-left:83.33333333%;}
.col-ms-offset-11 {margin-left:91.66666667%;}
.col-ms-offset-12 {margin-left:100%;}
}

Using this CSS: Just add this new CSS file right after you call the Bootstrap CSS in your HTML.

Remember when you are making columns with these new classes, you have to use the classes .col-ms-1 to .col-ms-12 etc. So there you go. You got your new custom grid for Bootstrap. Show me how you used this snippet for your project in the comment section. Good luck!

A few websites that are using this snippet:
Hawksland Associates
Enosis Creative

11 thoughts on “Bootstrap 3: How to create a new grid option for 480-767px

    • That’s a very good finding, Alfred. To be honest, I never had to use pushing, pulling or offsetting at that small viewport. But I’ve updated the gist. Let me know in the comment if it works for you. 🙂

      Like

  1. Is it also possible to NOT show the mobile menu using this new grid?
    When using this new grid in landscape, I’d like to show menu items and not the mobile pulldown menu
    How do I change this behaviour?

    Like

    • Hi thegeekandjock, no, it is not possible with this. The navigation menu in Bootstrap does not use its grids for showing dropdown/inline menu items. You have to write custom media-query CSS based on your requirement.

      I just gave some thought on this. Actually, what you want is very easy with a few lines on CSS. For example, you can use something like the following. But you have to change min-width value in the media-query and do some adjustments based on your design.

      @media (min-width: 480px) and (max-width: 767px) {
        .navbar-header {
          float: left;
        }
        .navbar-toggle {
          display: none !important;
        }
        .navbar-collapse.collapse {
          display: block !important;
        }
        .navbar-nav > li,
        .navbar-nav {
          float: left !important;
        }
        .navbar-nav.navbar-right:last-child {
          margin-right: -15px !important;
        }
        .navbar-right {
          float: right !important;
        }
        .container > .navbar-header {
          margin-right: 15px;
        }
      }
      

      Like

Ask a question or leave a reply!