Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive design on site that is viewed via an iframe

I am working on an app that will be deployed to Facebook therefore viewed from an iframe inside of Facebook's chrome.

I have some basic media queries that linearise the content at a set viewport size.

When the site is viewed in the browser locally the media queries work fine but when tested inside Facebook's chrome then they do not work.

I assume that the resizing of the viewport is not detected by the child of the iframe therefore the media queries will have no effect. Is there a way to get this working?

like image 610
RyanP13 Avatar asked Mar 13 '12 09:03

RyanP13


People also ask

Can an iframe be responsive?

A responsive iframe will render well across a variety of devices and screen sizes. In order to make your embedded iframe responsive, you need to wrap the iframe in a div and apply inline css. Follow these simple steps: Get the iframe embed code and paste in into your HTML page.

Can you put any website in an iframe?

HTTPS. Websites that begin with https can in most instances be embedded as iFrames. The exception to this is where the original website does not allow itself to be embedded.


2 Answers

You could add a little jQuery to detect a window resize and then swap out a separate stylesheet.

$(window).resize(function(){

    var currentWidth = $(document).width();
    var allStyleSheets = $("link");
    allStyleSheets.each(function(){ 
        var $this = $(this);

        //assume sheet1.css is for pages with widths equal and over 700 and sheet 2 is for widths under 700px
        if(currentWidth >= 700 && $this.attr("href").indexOf("sheet2.css") > 0){
            $this.remove();
            $(head).append('<link href="sheet1.css" type="text/css" rel="stylesheet" />');
        }
        if(currentWidth < 700 && $this.attr("href").indexOf("sheet1.css") > 0){
            $this.remove();
            $(head).append('<link href="sheet2.css" type="text/css" rel="stylesheet" />');
        }
    });    

});
like image 198
Fisch Avatar answered Sep 22 '22 23:09

Fisch


As my adapted code was using media queries in a common CSS file, I prefer to toggle a fbIframe class on the body element, and add specific CSS rules in context of this class. The jQuery code thus becomes simpler:

function adaptToWindowSize(){
    $("body").toggleClass("fbIframe", $(window).width() < 820);
};
$(window).resize(adaptToWindowSize);
$(adaptToWindowSize);
like image 38
Adrien Joly Avatar answered Sep 20 '22 23:09

Adrien Joly