9

I'm trying to create a Twitter Bootstrap application that displays a div that shows an application at different device sizes (desktop, tablet, mobile) in our SPA with functionality existing inside and outside that needs to communicate. When clicking on the specified size, the div should resize. Like this:

Desktop Example

Mobile Example

The bootstrap break points are based off media queries, which are at the window level. But I'm trying to make this work on a div. If we specify a container at the div level, grid's will not stack vertically like they would if you resize the window, they just shrink.

Harder problem then it sounds like at first. :( Any ideas on how to get around this?

The easiest answer is an iFrame, so that the iFrame's window size gets resized as the div changes size and the grid stacks correctly, but thats not ideal since functionality exists in our SPA from outside and inside the div.

I would enjoy hearing a better solution to handling this!

tmanion
  • 401
  • 2
  • 11
  • good question! looking forward to an answer! :) – achristoph Apr 16 '15 at 20:23
  • 1
    “Element Media Queries” is something that developers have been discussing for a while now – maybe you can take some inspiration from what others have already written on the matter, https://www.google.com/search?q=element+media+queries – CBroe Apr 17 '15 at 11:37

4 Answers4

1

Well, Bootstrap is just CSS and a bit of js written in LESS and SASS so you can modify the LESS/SASS version of Bootstrap file and replace all the media queries with a class name. The LESS preprocessor has a syntax which allows you to nest CSS declaration, for example, the following LESS declaration:

.mobile {
    .foo { ... }
    .bar { ... }
}

is equivalent to CSS:

.mobile .foo { ... }
.mobile .bar { ... }

Once you've replaced the media queries with CSS class, you'd need some JavaScript that adds the classes to the elements whose children needs to have whatever media query you want to fake out.

For additional reliability, so that the styles don't leak out to the surrounding page, you may want to use scoped CSS, but browser support for scoped CSS is rather appalling at this stage.

Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
1

Well bootstrap has different column sizes for divs. .col-xs- .col-sm- .col-md- .col-lg- xSmall and Small and Medium and Large. These are classes for your div.

0

Well you can use JQuery for that, or you can also use knockout library for dynamic binding, which enables to use custom bindings to achieve that. Here is what I will do is by using knockout bindings

<div id="devices"> It can have bindings which can have the values desktop, tablet & mobile<div>
<div id="leftDiv">Some Code...<div>
<div id="mainDiv">View<div>
  • I will read the value from div tag with id="devices" whatever it is {desktop, tablet, mobile} and can access it from the attributes defined in custom bindings
  • Secondly apply some if conditions to perform the required tasks. like

    $("#mainDiv").css( apply your css );

for each conditions. Apply what ever css you wish to resize the div with id="mainDiv" in css.

ashishraaj
  • 751
  • 7
  • 27
0

jQuery is the easy, fast and best solution to your problem. you can use

$("#idForSize").click(function(){
   $("#idForDiv").css({'height','20px','width','30px'});
});
Sainesh Mamgain
  • 754
  • 1
  • 10
  • 32
  • 1
    Changing the width of the container will only make the grid columns start stacking. The whole experience that is trying to be achieved is having the bootstrap columns shrink like they would on a mobile device or tablet while using a desktop, rather than having them stack. – tmanion May 01 '15 at 00:04