Tuesday, July 15, 2014

Bootstrap Grid System

Bootstrap Grid System is an amazing tutorial to manage content in grid shape. In this tutorial we shall discuss the Bootstrap Grid System.



What is a Grid?


As written on wikepedia:


In graphic design, a grid is a structure (usually two-dimensional) made up of a series of intersecting straight (vertical, horizontal) lines used to structure content. It is widely used to design layout and content structure in print design. In web design, it is a very effective method to create a consistent layout rapidly and effectively using HTML and CSS.


In the other simple words grids organize and structure content in web design, makes websites easy to scan and trim down cognitive load on users.


What is Bootstrap Grid System?


As written by the official documentation of Bootstrap for grid system:


Bootstrap includes a responsive, mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. It includes predefined classes for easy layout options, as well as powerful mixins for generating more semantic layouts.


Let us realize the above declaration. Bootstrap 3 is mobile first in the intellect that the code for Bootstrap now starts by targeting smaller screens like mobile devices, tablets, and then “expands” components and grids for larger screens such as laptops, desktops.


Mobile First Strategy



  • Content

    • Find out what is most important in your content.



  • Layout

    • Start your design to smaller widths first.

    • First base CSS address for mobile device and then media queries address for desktops and tablets.



  • Progressive Enhancement


    • Add/Remove elements as screen size increases and decreases.






Working of Bootstrap Grid System


Grid systems in Bootstrap are used for creating page layouts through a series of rows and columns that house your content. Here’s how the Bootstrap grid system works:




  • Rows must be placed within a .container class for proper alignment and padding.




  • Use rows to create horizontal groups of columns.




  • Content should be placed within columns, and only columns may be immediate children of rows.




  • Predefined grid classes like .row and .col-xs-4 are available for quickly making grid layouts. LESS mixins can also be used for more semantic layouts.




  • Columns create gutters (gaps between column content) via padding. That padding is offset in rows for the first and last column via negative margin on .rows.




  • Grid columns are created by specifying the number of twelve available columns you wish to span. For example, three equal columns would use three .col-xs-4.




Media Queries


Media query is actually fancy term for "conditional CSS rule". It basically applies several CSS based on certain conditions set forth. If those entire situations are met, the style is applied.


Bootstrap Media Queries allow you to hide, show and move content based on required browser’s dimensions. Subsequent media queries are used in LESS files to make the key breakpoints in the Bootstrap grid system.


/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */

/* Small devices (tablets, 768px and up) */
@media (min-width: @screen-sm-min) ...

/* Medium devices (desktops, 992px and up) */
@media (min-width: @screen-md-min) ...

/* Large devices (large desktops, 1200px and up) */
@media (min-width: @screen-lg-min) ...

Occasionally these are expanded to include a max-width to limit CSS to a narrower set of devices.


@media (max-width: @screen-xs-max) ... 
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) ...
@media (min-width: @screen-md-min) and (max-width: @screen-md-max) ...
@media (min-width: @screen-lg-min) ...

Media queries are based on two major parts:




  1. A device specification


  2. Then a size rule.


In the above case, the following rule is set:


Let’s think about this line:


@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) ... 

For all devices no matter what type with min-width: @screen-sm-min if the width of the screen gets smaller than @screen-sm-max, then do something.


Grid options


The following table sums up features of how Bootstrap grid system works across the multiple devices:









































































Extra small devices Phones (<768px)Small devices Tablets (≥768px)Medium devices Desktops (≥992px)Large devices Desktops (≥1200px)
Grid behaviorHorizontal at all timesCollapsed to start, horizontal above breakpointsCollapsed to start, horizontal above breakpointsCollapsed to start, horizontal above breakpoints
Max container widthNone (auto)750px970px1170px
Class prefix.col-xs-.col-sm-.col-md-.col-lg-
# of columns12121212
Max column widthAuto60px78px95px
Gutter width30px

(15px on each side of a column)
30px

(15px on each side of a column)
30px

(15px on each side of a column)
30px

(15px on each side of a column)
NestableYesYesYesYes
OffsetsYesYesYesYes
Column orderingYesYesYesYes

Basic Grid Structure


Following is basic structure of Bootstrap grid:


<div class="container">
<div class="row">
<div class="col-*-*"></div>
<div class="col-*-*"></div>
</div>
<div class="row">...</div>
</div>
<div class="container">....

Let us see some simple grid examples:



Responsive column resets


You are bound to run into issues with the four tiers of grids available at the positive breakpoints, the columns do not clear quite right side as one is taller than the other. To fix this issue, use this combination of class “.clearfix” and the responsive effectiveness classes as publicized in the following example.


<div class="container">
<div class="row" >
<div class="col-xs-6 col-sm-3"
style="background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div class="col-xs-6 col-sm-3"
style="background-color: #dedef8;box-shadow:
inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut.
</p>
</div>

<div class="clearfix visible-xs"></div>

<div class="col-xs-6 col-sm-3"
style="background-color: #dedef8;
box-shadow:inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
<div class="col-xs-6 col-sm-3"
style="background-color: #dedef8;box-shadow:
inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim
</p>
</div>
</div>
</div>

Resize your viewport or check it out on your phone for a desired result of this example.



Offset columns


Offsets are a helpful feature for additional specialized layouts. These can be used to push columns over for extra spacing, for example. The .col-xs=* classes do not support offsets, but they are simply simulated by using an empty cell.


To use these offsets on large displays, use the .col-md-offset-* classes. These classes increase the left margin of a column by * columns where * range from 1 to 11.


In this example we have <div class=”col-md-6″>..</div>, we will center this using class .col-md-offset-3.


<div class="container">

<h1>Hello, world!</h1>

<div class="row" >
<div class="col-xs-6 col-md-offset-3"
style="background-color: #dedef8;box-shadow:
inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing
elit.
</p>
</div>

</div>
</div>


Nesting columns


If you want to nest your content/data with the default grid, add a new .row and set of .col-md-* columns within an current .col-md-* column. Nested rows must include a set of columns that can add up to 12.


In the following model, the layout has two columns, with the second one being divided into four boxes over two rows.


<div class="container">

<h1>Hello, world!</h1>

<div class="row">

<div class="col-md-3" style="background-color: #dedef8;box-shadow:
inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<h4>First Column</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>

<div class="col-md-9" style="background-color: #dedef8;box-shadow:
inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<h4>Second Column- Split into 4 boxes</h4>
<div class="row">
<div class="col-md-6" style="background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Consectetur art party Tonx culpa semiotics. Pinterest
assumenda minim organic quis.
</p>
</div>
<div class="col-md-6" style="background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p> sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
</div>

<div class="row">
<div class="col-md-6" style="background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
</div>
<div class="col-md-6" style="background-color: #B18904;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim.</p>
</div>
</div>

</div>

</div>

</div>


Column ordering


An additional good feature of Bootstrap grid system is that you can write without difficulty the columns in an order, and explain them in another one. Anyone can easily modigy the order of built-in grid columns with .col-md-push-* and .col-md-pull-* modifier classes where * range from 1 to 11.


In this following example we are using two-column layout and left column is the narrowest and doing act as sidebar. We will exchange the order of these columns by .col-md-push-* and .col-md-pull-* classes.


<div class="container">

<h1>Hello, world!</h1>

<div class="row">
<p>Before Ordering</p>
<div class="col-md-4" style="background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I am on left
</div>
<div class="col-md-8" style="background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
I am on right
</div>
</div><br>
<div class="row">
<p>After Ordering</p>
<div class="col-md-4 col-md-push-8"
style="background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444,
inset -1px 1px 1px #444;">
I was on left
</div>
<div class="col-md-8 col-md-pull-4"
style="background-color: #dedef8;
box-shadow: inset 1px -1px 1px #444,
inset -1px 1px 1px #444;">
I was on right
</div>
</div>

</div>



Bootstrap Grid System

No comments:

Post a Comment