So, I want to apply the grid style of Bootstrap to the a number of div tag in my php code . Here is one way to achieve that: 1. Using this javascript snippet in the template: $(document).on("ready", function() { $.each(['xs', 'sm', 'md', 'lg'], function(idx, gridSize) { $('.col-' + gridSize + '-auto:first').parent().each(function() { //we count the number of childrens with class col-md-6 var numberOfCols = $(this).children('.col-' + gridSize + '-auto').length; if (numberOfCols > 0 && numberOfCols < 13) { minSpan = Math.floor(12 / numberOfCols); remainder = (12 % numberOfCols); $(this).children('.col-' + gridSize + '-auto').each(function(idx, col) { var width = minSpan; if (remainder > 0) { ...