jQuery/css: Fluid responsive isotope grid with square divs

Deadly 提交于 2019-12-10 19:05:45

问题


I'm trying to figure out if its at all possible to have square divs with a fluid isotope layout. I have a 4 column grid, each div being 24% width of the container, but I want these divs to be perfect squares, I can't work out how to achieve this.
Here's a working jsfiddle demo: http://jsfiddle.net/RJZu6/8/
jQuery:

var $container = $("#main-grid");

var size = function () {
    $container.isotope({
        masonry: {
            columnWidth: $container.width() / 4
        }
    });
}

$container.imagesLoaded(function () {
    $container.isotope({
        itemSelector: ".grid-block",
        animationEngine: "jquery",
        sortBy: "random",
        resizable: false,
        masonry: {
            columnWidth: $container.width() / 4
        }
    });

    size();
});

$(window).smartresize(size);

css:

#main-grid { 
    position: absolute;
    width: 100%;
}

.grid-block { 
    width: 24%; height: 50px; 
    margin-left: 0.25%; margin-right: 0.25%; 
    margin-bottom: 5px; 
    background: no-repeat 50% 50%;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

The fluidity and functionality works fine, just the sizes of the divs I'm having trouble with. Is it at all possible to have perfect square responsive divs?


回答1:


Depending on what you have inside as content, you can achieve fluid squares pretty easily using padding-bottom;

for example

width: 50%;
height: 0;
padding-bottom: 50%;

So in this updated fiddle all that I changed was the CSS of grid-lock

.grid-block { 
    width: 24%;  
    margin-left: 0.25%; margin-right: 0.25%; 
    margin-bottom: 5px; 
    background: no-repeat 50% 50%;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    padding-bottom:24%;
}

Here's an example of square elements in a page

Here's more information about the technique.



来源:https://stackoverflow.com/questions/16532520/jquery-css-fluid-responsive-isotope-grid-with-square-divs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!