Responsive width Facebook Page Plugin
Facebook's new "Page Plugin" width ranges from 180px
to 500px
as per the documentation.
- If configured below
180px
it would enforce a minimum width of180px
- If configured above
500px
it would enforce a maximum width of500px
With Adaptive Width checked, ex:
Unlike like-box, this plugin enforces its limits by sticking to the boundary values if mis-configured.
For small screens / Responsive behaviors
When rendering on smaller screens, enforce desiered
width
on the plugin container and plugin would try to fit in.The plugin renders at a smaller width (to fit in smaller screens) automatically, if the container is of slimmer than the configured
width
.You can scale down the container on mobile and the plugin will fit in as long as it gets the minimum of
180px
to fit in.
Without Adaptive Width
- The plugin will render at the width specified, irrespective of the container width
This doesn't work too well when you have the plugin placed in a thin column, like a sidebar for example. On medium sized screens these typically run smaller than 280px in width.
.fb-page,
.fb-page span,
.fb-page span iframe[style] {
width: 100% !important;
}
This is the code I use to stop the plugin breaking outside of a wrapping container. Unlike the old like box which would tile, this one just overflows, hiding the overflowed content.
this works for me (the width is forced by javascript and FB plugin loaded via javascript)
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.5&appId=443271375714375";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
jQuery(document).ready(function($) {
$(window).bind("load resize", function(){
setTimeout(function() {
var container_width = $('#container').width();
$('#container').html('<div class="fb-page" ' +
'data-href="http://www.facebook.com/IniciativaAutoMat"' +
' data-width="' + container_width + '" data-tabs="timeline" data-small-header="true" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true"><div class="fb-xfbml-parse-ignore"><blockquote cite="http://www.facebook.com/IniciativaAutoMat"><a href="http://www.facebook.com/IniciativaAutoMat">Auto*Mat</a></blockquote></div></div>');
FB.XFBML.parse( );
}, 100);
});
});
</script>
<div id="container" style="width:100%;">
<div class="fb-page" data-href="http://www.facebook.com/IniciativaAutoMat" data-tabs="timeline" data-small-header="true" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true"><div class="fb-xfbml-parse-ignore"><blockquote cite="http://www.facebook.com/IniciativaAutoMat"><a href="http://www.facebook.com/IniciativaAutoMat">Auto*Mat</a></blockquote></div></div>
</div>
To make the new Facebook Page Plugin responsive on initial page load, you'll want to remove the data-width
attribute and instead add
data-adapt-container-width="true"
This will make the Facebook Page Plugin responsive, but only on the initial page render, with a minimum width of 180px.
I'm still trying to figure out how to make it truly dynamically responsive, in spite of Facebook's caveat (I'll post an update if I ever find the answer).
No Dynamic Resizing
The Page plugin works with responsive, fluid and static layouts. You can use media queries or other methods to set the
width
of the parent element, yet:The plugin will determine its
width
on page load. It will not react changes to the box model after page load. If you want to adjust the plugin'swidth
on window resize, you manually need to rerender the plugin.
Source: https://developers.facebook.com/docs/plugins/page-plugin
You could make it dynamically responsive by reinitializing the widget on browser resize, as Io Ctaptceb suggested, but by doing that you run the risk of eating up memory very quickly.
Yugal Jindle had a good answer, but I wanted to clarify that I have yet to find a way to make the plugin truly dynamically responsive.
I'm putting this here for those that had the same problem as me and couldn't find their answer here between the comments or on any other stackoverflow page.
I added the Facebook Page Plugin with settings that would adjust it to the container width.
data-adapt-container-width="true"
However, one or more elements within the iframe or Javascript SDK element were given the width of 340px making the Page Plugin not adapt to the container width. While it should have a minimum of 180px and a maximum of 500px.
The code provided by Facebook was missing something however.
<div class="fb-page" data-href="https://www.facebook.com/Paras-Design-393209377418188" data-tabs="timeline" data-small-header="false" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="false"></div>
By manually adding data-width="500"
the Page Plugin responded as expected and adapted to the container width to a maximum width of 500px.
I hope this helps anyone coming across the same problem.