读bootstrap的栅格系统
bootstrap中栅格布局源码。主要是为了多熟悉less,然后就是更加痛彻的明白bootstrap的原理。
(一)布局容器
less\grid.less
.container { .container-fixed(); @media (min-width: @screen-sm-min) { width: @container-sm; } @media (min-width: @screen-md-min) { width: @container-md; } @media (min-width: @screen-lg-min) { width: @container-lg; } } // Fluid container // // Utilizes the mixin meant for fixed width containers, but without any defined // width for fluid, full width layouts. .container-fluid { .container-fixed(); }
mixin\grid.less
.container-fixed(@gutter: @grid-gutter-width) { margin-right: auto; margin-left: auto; padding-left: floor((@gutter / 2)); padding-right: ceil((@gutter / 2)); &:extend(.clearfix all); }
栅格布局分:
固定宽布局(.container):容器有固定width值。
流式布局(.container-fluid):容器不设置width值。
(二)行
less\grid.less
.row { .make-row(); }
mixin\grid.less
.make-row(@gutter: @grid-gutter-width) { margin-left: ceil((@gutter / -2)); margin-right: floor((@gutter / -2)); &:extend(.clearfix all); }
为列(col)设置一个row并设置负margin撑满container。
(三)列
less\grid.less
.make-grid-columns();
mixin\grid-framework.less
.make-grid-columns() { // Common styles for all sizes of grid columns, widths 1-12 .col(@index) { // initial @item: ~".col-xs-@{index}, .col-sm-@{index}, .col-md-@{index}, .col-lg-@{index}"; .col((@index + 1), @item); } .col(@index, @list) when (@index =< @grid-columns) { // general; "=<" isn't a typo @item: ~".col-xs-@{index}, .col-sm-@{index}, .col-md-@{index}, .col-lg-@{index}"; .col((@index + 1), ~"@{list}, @{item}"); } .col(@index, @list) when (@index > @grid-columns) { // terminal @{list} { position: relative; // Prevent columns from collapsing when empty min-height: 1px; // Inner gutter via padding padding-left: ceil((@grid-gutter-width / 2)); padding-right: floor((@grid-gutter-width / 2)); } } .col(1); // kickstart it }
.make-grid-columns生成的所有栅格列的类名后,添加每个类名的公共样式。
less\grid.less
.make-grid(xs);
mixin\grid-framework.less
.float-grid-columns(@class) { .col(@index) { // initial @item: ~".col-@{class}-@{index}"; .col((@index + 1), @item); } .col(@index, @list) when (@index =< @grid-columns) { // general @item: ~".col-@{class}-@{index}"; .col((@index + 1), ~"@{list}, @{item}"); } .col(@index, @list) when (@index > @grid-columns) { // terminal @{list} { float: left; } } .col(1); // kickstart it }
.calc-grid-column(@index, @class, @type) when (@type = width) and (@index > 0) { .col-@{class}-@{index} { width: percentage((@index / @grid-columns)); } } .calc-grid-column(@index, @class, @type) when (@type = push) and (@index > 0) { .col-@{class}-push-@{index} { left: percentage((@index / @grid-columns)); } } .calc-grid-column(@index, @class, @type) when (@type = push) and (@index = 0) { .col-@{class}-push-0 { left: auto; } } .calc-grid-column(@index, @class, @type) when (@type = pull) and (@index > 0) { .col-@{class}-pull-@{index} { right: percentage((@index / @grid-columns)); } } .calc-grid-column(@index, @class, @type) when (@type = pull) and (@index = 0) { .col-@{class}-pull-0 { right: auto; } } .calc-grid-column(@index, @class, @type) when (@type = offset) { .col-@{class}-offset-@{index} { margin-left: percentage((@index / @grid-columns)); } } // Basic looping in LESS .loop-grid-columns(@index, @class, @type) when (@index >= 0) { .calc-grid-column(@index, @class, @type); // next iteration .loop-grid-columns((@index - 1), @class, @type); }
.make-grid(@class) { .float-grid-columns(@class); .loop-grid-columns(@grid-columns, @class, width); .loop-grid-columns(@grid-columns, @class, pull); .loop-grid-columns(@grid-columns, @class, push); .loop-grid-columns(@grid-columns, @class, offset); }
栅格列偏移(offset)计算Mixin,列排序(pull or push)计算Mixin,列宽(width)计算Mixin,浮动。
bootstrap3以xs屏优先,直接初始化.make-grid(xs)。
@media (min-width: @screen-sm-min) { .make-grid(sm); } // Medium grid // // Columns, offsets, pushes, and pulls for the desktop device range. @media (min-width: @screen-md-min) { .make-grid(md); } // Large grid // // Columns, offsets, pushes, and pulls for the large desktop device range. @media (min-width: @screen-lg-min) { .make-grid(lg); }
其他屏幕写入@media中。
不明白float-grid-columns直接添加了变量(@class)以小屏(xs)优先写在了@media外边,而make-grid-columns并没有这样。
(4)Mixin 列
// Generate the extra small columns .make-xs-column(@columns; @gutter: @grid-gutter-width) { position: relative; float: left; width: percentage((@columns / @grid-columns)); min-height: 1px; padding-left: (@gutter / 2); padding-right: (@gutter / 2); } .make-xs-column-offset(@columns) { margin-left: percentage((@columns / @grid-columns)); } .make-xs-column-push(@columns) { left: percentage((@columns / @grid-columns)); } .make-xs-column-pull(@columns) { right: percentage((@columns / @grid-columns)); } // Generate the small columns .make-sm-column(@columns; @gutter: @grid-gutter-width) { position: relative; min-height: 1px; padding-left: (@gutter / 2); padding-right: (@gutter / 2); @media (min-width: @screen-sm-min) { float: left; width: percentage((@columns / @grid-columns)); } } .make-sm-column-offset(@columns) { @media (min-width: @screen-sm-min) { margin-left: percentage((@columns / @grid-columns)); } } .make-sm-column-push(@columns) { @media (min-width: @screen-sm-min) { left: percentage((@columns / @grid-columns)); } } .make-sm-column-pull(@columns) { @media (min-width: @screen-sm-min) { right: percentage((@columns / @grid-columns)); } } // Generate the medium columns .make-md-column(@columns; @gutter: @grid-gutter-width) { position: relative; min-height: 1px; padding-left: (@gutter / 2); padding-right: (@gutter / 2); @media (min-width: @screen-md-min) { float: left; width: percentage((@columns / @grid-columns)); } } .make-md-column-offset(@columns) { @media (min-width: @screen-md-min) { margin-left: percentage((@columns / @grid-columns)); } } .make-md-column-push(@columns) { @media (min-width: @screen-md-min) { left: percentage((@columns / @grid-columns)); } } .make-md-column-pull(@columns) { @media (min-width: @screen-md-min) { right: percentage((@columns / @grid-columns)); } } // Generate the large columns .make-lg-column(@columns; @gutter: @grid-gutter-width) { position: relative; min-height: 1px; padding-left: (@gutter / 2); padding-right: (@gutter / 2); @media (min-width: @screen-lg-min) { float: left; width: percentage((@columns / @grid-columns)); } } .make-lg-column-offset(@columns) { @media (min-width: @screen-lg-min) { margin-left: percentage((@columns / @grid-columns)); } } .make-lg-column-push(@columns) { @media (min-width: @screen-lg-min) { left: percentage((@columns / @grid-columns)); } } .make-lg-column-pull(@columns) { @media (min-width: @screen-lg-min) { right: percentage((@columns / @grid-columns)); } }
不使用预定义栅格类名的方法,而是直接使用Mixin划分.row。需要使用less编写。