Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap grid system responisve utility in resizable divs

i tried googleing found nothing so here i'am

Lets say u have this

<div class="resizable">
    <div class='row'>
      <div class='col-xs-12 col-md-4' style='border:1px solid red;'> HELLo </div>
    </div>
</div>

this work as expected and in > md screens the col will take 33%, yet my question is.

is there a way to make col-md/col-xs respondent to its container not to the viewport in case of resizing ?

i was thinking of a simple js solution that before it starts resizing div it can record current width of .resizable, and manipulate col-xs, col-md by removing or readding according to the new size after resizing is done..

but if there is something already existent or a better solution i would love if you share it .

thanks

Current solution :

As im using jquery-ui for resizing i wrote something simple:

$(document).on('resizestop','.resizable',function(){

        $(this).find('.row').each(function(){
        var w = $(this).innerWidth();
        var c=$(this).children('div');
        if(w <= 768){
          c.attr('class', c.attr('class').replace(' col-md-',' w-col-md-')); 
          c.attr('class', c.attr('class').replace(' col-sm-',' w-col-sm-')); 
          c.attr('class', c.attr('class').replace(' col-lg-',' w-col-lg-'));
        }else if(w < 960){
          c.attr('class', c.attr('class').replace(' w-col-sm-',' col-sm-'));
          c.attr('class', c.attr('class').replace(' col-md-',' w-col-md-')); 
          c.attr('class', c.attr('class').replace(' col-lg-',' w-col-lg-')); 
        }else if(w >= 960){
          c.attr('class', c.attr('class').replace(' w-col-sm-',' col-sm-'));
          c.attr('class', c.attr('class').replace(' w-col-lg-',' col-lg-'));
          c.attr('class', c.attr('class').replace(' w-col-md-',' col-md-')); 
        }
      });
  });

this simply remove all col-md,col-lg if the width of parent is less than XX px, and restore it if its larger.

let me know what you think about this approach :).

note that this dictate that col-xs be the first in class, and any other responsive class follows. thanks

like image 731
Zalaboza Avatar asked Feb 25 '14 15:02

Zalaboza


People also ask

Is the Bootstrap grid system responsive?

Bootstrap's grid system is responsive, and the columns will re-arrange depending on the screen size: On a big screen it might look better with the content organized in three columns, but on a small screen it would be better if the content items were stacked on top of each other.

How many tiers does Bootstrap 5 grid system includes for building complex responsive layouts?

Bootstrap's grid includes five tiers of predefined classes for building complex responsive layouts.

What is XS SM MD lg in Bootstrap?

The Bootstrap 3 grid system has four tiers of classes: xs (phones), sm (tablets), md (desktops), and lg (larger desktops). You can use nearly any combination of these classes to create more dynamic and flexible layouts.

Which class in Bootstrap is used to provide a responsive fixed width container?

container class is a responsive, fixed-width container, meaning its max-width changes at each breakpoint.


1 Answers

I'm not sure i understand your question well. You are probably looking for Media Queries for Elements.

I expect that your bootstrap layout is fluid (with .container-fluid). When your resizable had been defined as a percentage of the viewport (for instance wrapped in a col-md-9 which got 75% of the viewport inside a .container-fluid) you should be able to define a new set of breakpoint for your element (or .resizable).

For instance when .resizable has been wrapped in a col-md-6 its width will be 50% of the viewport, so its size will be > than 768px when the viewport > (768 x 2) now you can define a media query for that situation @media (min-width: 1536px) {} which is true when the width of the .resizable > 768px.

example

html

<div class="container-fluid">
<div class="col-md-9">
  <div class="resizable">
    <div class="row">
      <div class="col" style="border:1px solid red;"> HELLo </div>
      <div class="col" style="border:1px solid red;"> HELLo </div>
      <div class="col" style="border:1px solid red;"> HELLo </div>
    </div>
</div>
</div>
<div class="col-md-3">
  Right side
</div>
</div>

css

.resizable .row .col {
width:100%;
      } 
@media (min-width:1024px) {
/* (768 * 12) / 9 */
.resizable .row .col {
   width:50%; 
   float:left;  
            }             
}  
@media (min-width:1280px) {
/* (960 * 12) / 9 */
 .resizable .row .col { 
   width:33.33%; 
   float:left; 
  }               
}  

See also: http://www.bootply.com/YHigazGje5

like image 135
Bass Jobsen Avatar answered Oct 30 '22 15:10

Bass Jobsen