2016-03-07 78 views
0

我一直在試圖解決我的網格,我不知道爲什麼我的列不堆疊在一起。當我提出一個div容器和裏面一排,然後12列列正在整個寬度,則跑馬圈地下面enter image description here用sass解決我的Grid問題?

$screen-width: 1147px; 
$number-of-columns: 12; 
$gutter: 30px; 
$column-width: $screen-width/$number-of-columns; 
$padding: $gutter/2; 
$total-width: ($column-width * $number-of-columns) + ($gutter * ($number-of-columns - 1)); 
$gutter-width:($gutter/$total-width) * 100%; 


@for $i from 1 through $number-of-columns { 
    .column-#{$i} { 
    width: ($i /$number-of-columns) * 100%; 
    background:#ccc; 
    float: left; 
    margin-left: $gutter; 
    } 
} 



@mixin clearfix() { 
    &:before, 
    &:after { 
    content: " "; // 1 
    display: table; // 2 
    } 
    &:after { 
    clear: both; 
    } 
} 
// Set Base Container 
.container { 
    max-width:$total-width; 
    margin:0px auto; 
    padding: 0 $padding 0 $padding; 
    background: blue; 
    @include clearfix; 
} 
.row { 
    width: 100%; 
    margin: 0; 
    background: green; 
    @include clearfix; 
} 

回答

1

你被列數除以可用空間,但隨後您在每列的左側添加了一個邊距因此,如果您有10列,則每列是總寬度+排水溝的10%。這增加了超過100%,並將你的三列推到下一行。

大多數電網系統解決這兩種方式中的一個...

A)使用CSS計算()由按列然後扣除溝槽的數目除以空間來計算每個列的寬度。例如。 calc(10% - 30px); B)在每一列上使用填充來創建排水溝並在兩側平均添加它。例如。填充:0 15px;這會給你一個列的均勻分佈,不需要計算,但缺點是你需要在你的容器的每一邊需要-15px的邊距來容納它,並且你需要在每一列中添加一個額外的HTML標籤。

+0

不應盒大小:邊界盒;考慮寬度的排水溝? –

+0

不,這意味着盒子的寬度是使用內容,填充和邊框寬度計算的,但不是邊距。 – jonhobbs

+0

@jonhbbs我使用了yr建議並添加了一個函數來計算每列的寬度,我非常感謝你。 –

0

所以我設法添加到一個函數,但現在我留下了似乎正在發生的邊界,我不知道爲什麼會這樣,我試過了,比如在行上放一個負邊距。

$screen-width: 2147px; 
$number-of-columns: 12; 
$gutter: 30px; 
$column-width: $screen-width/$number-of-columns; 
$padding: $gutter/2; 
$total-width: ($column-width * $number-of-columns) + ($gutter * ($number-of-columns - 1)); 
$gutter-width:($gutter/$total-width) * 100%; 



@function grid-width($cols, $has-gutter:false) { 
@if $has-gutter { 
    @return calc(((100%/#{$number-of-columns}) * #{$cols}) - #{$gutter}); 
} 
@else { 
    @return calc((100%/#{$number-of-columns}) * #{$cols}); 
} 
} 

@for $i from 1 through $number-of-columns { 
    .column-#{$i} { 
    width: grid-width(#{$i}, true); 
    background:#ccc; 
    display: inline-block; 
    margin-left: $gutter; 
    float: left; 
    } 
} 



@mixin clearfix() { 
    &:before, 
    &:after { 
    content: " "; // 1 
    display: table; // 2 
    } 
    &:after { 
    clear: both; 
    } 
} 
// Set Base Container 
.container { 
    max-width:$total-width; 
    margin: 0px auto; 
    height:100px; 
    background: blue; 
    @include clearfix; 
} 
.row { 
    width: 100%; 
    height:50px; 
    background: green; 
    @include clearfix; 
} 

enter image description here

+1

Alwan,你應該編輯你的原始問題,而不是把這個新問題作爲答案,但在你的.column你應該有餘裕: $排水溝/ 2;和margin-right:$ gutter/2;平均分配差距。然後,在.row中,您需要添加保證金 - 右側:0 - $ gutter/2;左邊空白也是一樣。 – jonhobbs

+0

opps我的壞!但我繼續並做到了這一點。它工作得很好,只剩下餘量:$ gutter/2;和margin-right:$ gutter/2;在.column上,但是一旦我將它添加到.row它打破了,我想這是不需要的。 –