熟悉Bootstrap的同学应该了解其中的格栅系统,用来排版非常方便。他将页面分为12等分,并且适用不同的尺寸屏幕。超小xs(小于768px),小屏sm(大于等于768px),中屏md(大于等于992px),大屏lg(大于等于1200px)。为此需要生成一堆的.clo-*-*类,总计4*12=48个,再加上pull,push,offset。这种只能用less来写了,参考Bootstrap的less源码,自己重新写了一个,仅供练手
Less版本
@sm-width: 768px; @md-width: 992px; @lg-width: 1200px; @grid-columns: 12; @grid-gutter: 30px; @container-sm: (720px + @grid-gutter); @container-md: (940px + @grid-gutter); @container-lg: (1140px + @grid-gutter); .grid-padding(@gutter){ padding-left: floor((@gutter)/2); padding-right: floor((@gutter)/2); } .make-container() { .grid-padding(@grid-gutter); margin-left: auto; margin-right: auto; @media (min-width: @sm-width) { width: @container-sm; } @media (min-width: @md-width) { width: @container-md; } @media (min-width: @lg-width) { width: @container-lg; } } .container{ .make-container; } .container-fluid{ .grid-padding(@grid-gutter); margin-left: auto; margin-right: auto; } .row { margin-left: -15px; margin-right: -15px; } .make-grid() { .col(@index; @list) when(@index @grid-columns) { @{item} { .grid-padding(@grid-gutter); position: left; min-height: 1px; } } .col(1; '.col-xs-0'); } .make-grid(); .make-column(@class) { .col(@index; @list) when (@index @grid-columns) { @{list} { float: left; } } .col(1; '.col-@{class}-0'); } .make-column(xs); @media (min-width: @sm-width) { .make-column(sm); } @media (min-width: @md-width) { .make-column(md); } @media (min-width: @lg-width) { .make-column(lg); }
sass版本
$screen-map: (xs:768px, sm: 768px, md: 992px, lg: 1200px); $gutter-width: 30px; $container-map: (sm: 720px + $gutter-width, md: 940px + $gutter-width, lg: 1140px + $gutter-width); $grid-columns: 12; $colclass-map: (pull: right, push: left, offset: margin-left); @mixin grid-padding($gutter) { padding-left: $gutter/2; padding-right: $gutter/2; } @mixin margin-auto($horizone:null) { @if $horizone { margin: $horizone/2 auto; } @else { margin-left: auto; margin-right: auto; } } @mixin make-container() { @include grid-padding($gutter-width); @include margin-auto(); @each $screen, $width in $container-map { $min-width: map-get($screen-map, $screen); @media (min-width: $min-width) { width: $width;