2017-07-08 53 views
0

我有一個網格設置在塊中心對齊的地方,因此我已經使用display: inline block並將text-align: center設置爲容器。但現在客戶想要塊上的變量高度,因爲它們是內聯塊,所以現在在網格中留下了很大的空白。或者我可以使用float: left,但這不會工作,因爲塊需要居中。我在這裏有一個codepen設置:https://codepen.io/anon/pen/WOaeve中心對齊網格沒有間隙和浮動

我通常使用同位素插件的網格,但沒有佈局模式,將允許中心對齊的塊,所以我需要一個解決方案,將允許網格中的所有間隙被填充並且塊被居中。這裏是我的CSS標記也:

.feed-grid { 
    position: relative; 
    display: block; 
    width: 100%; 
    height: auto; 
    text-align: center; 
    font-size: 0; 
} 

.feed-grid .grid-block { 
    display: inline-block; 
    vertical-align: top; 
    margin-left: 6px; 
    margin-right: 6px; 
    margin-bottom: 12px; 
    width: auto; 
    height: 255px; 
} 

.feed-grid .grid-block.large { 
    height: 522px; 
} 

.feed-grid .grid-block img { 
    position: relative; 
    display: block; 
    width: auto; 
    height: 255px; 
} 

.feed-grid .grid-block.large img { 
    height: 522px; 
} 

任何解決方案,將不勝感激!

+0

圖像意味着「迴流」改變它們在網格上的位置以彌補差距? – Afrowave

+0

@微波是的,這是想法,缺口需要填充......例如,當你使用同位素或磚石插件。雖然 – user1374796

回答

0

它可以解決你的問題。我在.child元素中使用了float: left;

.container {background: green; overflow: hidden; text-align: center; padding: 15px;} 
 
.center-element {float: none; background: yellow; overflow: hidden; width: auto; box-sizing: border-box; display: inline-block; padding: 5px; vertical-align: middle;} 
 
.child {float: left; padding: 12px 24px; background-color: red;} 
 
.child:not(:first-child) {margin-left: 5px;}
<div class="container"> 
 
\t <div class="center-element"> 
 
\t \t <div class="child"></div> 
 
\t \t <div class="child"></div> 
 
\t \t <div class="child"></div> 
 
\t \t <div class="child"></div> 
 
\t \t <div class="child"></div> 
 
\t \t <div class="child"></div> 
 
\t </div> 
 
</div>

0

什麼在你有比它的「行」更小的圖像的情況下,垂直中間alignement?

這裏是一個CodePen

$(document).ready(function(){ 
    var rowHeight = 0; 
    var rowHeights = []; 
    var rowCount = 0; 
    var offset = $(".grid-block").first().offset().top; 

    // Loop throught all images to get their heights. 
    $(".grid-block img").each(function(){ 

    // If on the same row 
    if($(this).offset().top == offset){ 
     // Find the biggest height 
     if($(this).height() > rowHeight){ 
     rowHeight = $(this).height(); 
     rowHeights[rowCount] = rowHeight; 
     } 

     // If the row is different. 
    }else{ 
     // Get the new row offset. 
     offset = $(this).offset().top; 
     rowCount++; 

     rowHeight = $(this).height(); 
     rowHeights.push(rowHeight); 
    } 

    // Set a custom attribute to apply the right margins. 
    $(this).attr("data-row",rowCount); 
    }); 

    //Here you have the array of row heights in console. 
    console.log(JSON.stringify(rowHeights)); 


    // Loop again to apply some margins. 
    $(".grid-block img").each(function(){ 
    var thisRowHeight = rowHeights[parseInt($(this).data("row"))]; 

    // Apply the margin if this image is smaller than the row height. 
    if($(this).height() < thisRowHeight){ 
     var margin = (thisRowHeight - $(this).height())/2; 
     $(this).css({"margin":margin+"px 0"}); 
    } 
    }); 
}); 

據「辦法」行高保證金適用於小型圖像......在中間使它們。

+0

它接近,但它們都不允許像我的演示那樣集中塊,但是現在我已經得到了雙倍高度圖像,我需要小圖像來填充這個空間並且不留空隙 - 就像砌體或者同位素一樣。對此有何建議? – user1374796

+0

如果縱橫比不是問題,你可以將它們拉伸...... [** CodePen-v2 **](https://codepen.io/Bes7weB/full/JJmKWz/) –