Responsive Masonic layout without predefined width

I am creating a masonry layout in 2 columns using images of different sizes. Images can be of any size if they have the largest common divisor (as required by the Freemasonry plugin).

To make the layout responsive, I convert the width of the masonry elements to percentages (or I can use the minimum width and width of 100%).

Update: I noticed that many of those in charge make both columns 50% as a solution. It works, but it is not the goal. Images must retain the original image size. They can be reduced, but keep the same ratio.

$(function () { var container = $('#container'); // Convert .box width from pixels to percent $('.box').find('img').each(function () { var percent = ($(this).width()) / container.width() * 100 //convert to percent; $(this).closest('.box').css('max-width', percent + '%'); }); // Trigger masonry container.masonry({ itemSelector: '.box', columnWidth: 1 //widths dividable by 1 }); }); 

jsfiffle: http://jsfiddle.net/AMLqg/278/

It seems to work. Elements are fluid when the window is resized. However, if you load the script into a small window size (less than the width of column 2), the elements collapse. How can I save masonry elements responsive to window loading even when the window is smaller?

Update: Here you will find more information for a better understanding. I am trying to save 2 response columns regardless of window size. Columns cannot have equal widths because images have different widths. For this reason, I use columnWidth: 1 because all widths are divisible by 1.

See the pictures below for an example.

Problem: When you open the page in a small window, the elements are reset. When you resize the window to be larger, the elements remain folded until the window is wider than the width of both elements.

enter image description here

Purpose: I am trying to save elements in 2 responsive columns when loading, as in the image below. Currently, they remain responsive if you download a large window, and you reduce its size, but not vice versa, when the window is small when loading, and you make it larger.

enter image description here

+5
source share
9 answers

You can try overflow: hidden in the surrounding field.

0
source

Are you looking for something like this?

Fiddle

So, all we do is get rid of your percentage calculation (which I really don't understand) and set min-width in the .box class. Similar:

 .box { float: left; min-width: 100px; } 

I was able to reproduce your problem. Here's how it looks curious:

The stacking collapse problem

The problem is your CSS float: left rule, which collapses the field when Freemasonry performs its positioning calculations after adding the image. You can make a simple fix to save this if you really need to keep this awkward percentage calculation, like this:

 .container:after { content: ''; display: table; clear: both; } 

Hope this helps!

Edit - based on your comments:

Well, if you always want two columns there, this is an even simpler change:

Get rid of this javascript

 // Convert .box width from pixels to percent $('.box').find('img').each(function () { var percent = $(this).width() / container.width() * 100; $(this).closest('.box').css('max-width', percent + '%'); }); 

Add this CSS

 .box { max-width: 50%; } 

Pretty straightforward, I think.

Here's a violin, just for a giggle

0
source

Using imagesloaded.js and column width using css as follows:

jsFiddle

 <div id="container"> <div class="grid-sizer"></div> <div class="box"> <img src="http://images.huffingtonpost.com/2007-11-01-ice.jpg" /> </div> <div class="box"> <img src="http://www.wwalls.ru/mini/201211/57608.jpg" /> </div> <div class="box"> <img src="http://artistsandwriters.com/site/wp-content/uploads/2014/09/IMG_7303LR-390x150-1412284267.jpg" /> </div> </div> 

Script

 $(document).ready(function () { var container = $('#container'); $('.box').find('img').each(function () { var percent = $(this).width() / container.width() * 50; $(this).closest('.box').css('max-width', percent + '%'); }); // Trigger masonry container.imagesLoaded(function () { container.masonry({ itemSelector: '.box', columnWidth: '.grid-sizer' }); }); }); 

CSS

 #container { max-width:580px; } .box { float: left; margin-bottom: 5px; } .box img { width: 100%; } .grid-sizer { width: 50%; } 
0
source

EDIT

Check out http://jsfiddle.net/gk3t009j/2/

CSS

 #wrapper { background-color: red; margin: 0 auto; max-width:580px; } #container, { overflow: hidden; width: 100%; } .box { max-width: 290px!important; width: 50%; } .box img{width: 100%;} 

Js

 $( window ).load( function() { var wc=$( '#container').width(); wc=parseInt(wc); if( wc % 2) { var wb=$('.box').width(); wb--; $('.box').width(wb) } $( '#container').masonry( { itemSelector: '.box', columnWidth: function( containerWidth ) { return parseInt(containerWidth / 2); } }); }); 

HTML

 <div id="wrapper"> <div id="container"> <div class="box"> <img src="http://images.huffingtonpost.com/2007-11-01-ice.jpg" /> </div> <div class="box"> <img src="http://www.wwalls.ru/mini/201211/57608.jpg" /> </div> <div class="box"> <img src="http://artistsandwriters.com/site/wp-content/uploads/2014/09/IMG_7303LR-390x150-1412284267.jpg" /> </div> </div> </div> 
0
source

I removed the JS code and some HTML markup and updated the style:

 #container { width: 100%; } img { display: inline; vertical-align: top; float: left; min-width: 50%; width: 50%; } 

http://jsfiddle.net/org6nsr8/8/ I agree with Josh Burgess that Freemasonry is not needed for this, take a look and see if you went for it.

I would be happy to develop if something is unclear or you want to explain something.

0
source

You do not need JavaScript; just change css to .box to:

 .box { float: left; max-width: 50%; } 
0
source

I'm not sure if this is what you need. If I understand the problem correctly, perhaps you need to use max-width instead of width.

Here is an example script: http://jsfiddle.net/AMLqg/304/

My JS code is:

  $(function () { var container = $('#container'); var maxWidth = container.css("maxWidth"); maxWidth = parseInt(maxWidth.substring(0,maxWidth.indexOf("px"))); // Convert .box width from pixels to percent $('.box').find('img').each(function () { var percent = ($(this).width()) / maxWidth * 100; console.log(percent); $(this).closest('.box').css('max-width', percent + '%'); }); // Trigger masonry container.masonry({ itemSelector: '.box', columnWidth: 1 //widths dividable by 1 }); }); 
0
source

Having tried several libraries to make a mock layout, I prefer salvattor.js

Very easy to use. the size of the columns you can customize css.

 @media screen and (max-width: 480px){ #grid[data-columns]::before { content: '1 .column.size-1of1'; } } 
0
source

What I understand is, you want to leave Layout 2 Column with the images in the aspect ratio on all screen sizes,

Check out

http://jsfiddle.net/tasaeed/k40cgfye/

CSS

 #container { max-width: 580px; } .box { float: left; width:50%; } .box img { width: 100%; height:auto; } 

Script

 $(function () { var container = $('#container'); // Trigger masonry container.masonry({ itemSelector: '.box', }); }); 
0
source

Source: https://habr.com/ru/post/1206639/


All Articles