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.
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-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!