Skip to content

Instantly share code, notes, and snippets.

@barryvdh
Last active March 7, 2023 23:38
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save barryvdh/2834636 to your computer and use it in GitHub Desktop.
Save barryvdh/2834636 to your computer and use it in GitHub Desktop.
custom grid
/* ===== Primary Styles =====================================================
Author: Fruitcake Studio (Barry vd. Heuvel)
========================================================================== */
//Generate a custom (semantic) grid
.customGrid(@gridColumnWidth, @gridGutterWidth){
#header, #main {
.row();
}
.block {
.span(3);
}
#content{
.span(9);
}
footer {
#copyright {
.span(12);
}
}
.thumbnails > li {
.span(3);
}
/* ===== Custom grid mixins =================================================
Do not change this mixin below this line!
========================================================================== */
.row() when( isnumber(@gridColumnWidth) ) {
margin-left: @gridGutterWidth * -1;
.clearfix();
}
.row() when(@gridColumnWidth = auto){
margin-left: 0;
}
.span(@columns: 1, @offset: 0) when( isnumber(@gridColumnWidth) ) {
float: left;
margin-left: (@gridColumnWidth * @offset) + (@gridGutterWidth * (@offset - 1)) + (@gridGutterWidth * 2);
width: (@gridColumnWidth * @columns) + (@gridGutterWidth * (@columns - 1));
min-height: 1px; // prevent collapsing columns
}
.span (@columns, @offset: 0) when(@gridColumnWidth = auto){
float: none;
display: block;
width: 100%;
margin-left: 0;
.box-sizing(border-box);
}
}
// Custom grid (default size)
.customGrid(@gridColumnWidth, @gridGutterWidth);
@barryvdh
Copy link
Author

I only tested this on lessPHP, an PHP port of Less.JS, which tries to do behave the same as Less.js

In my testcase, the result of .span4 , is the same as a .block with .span(4).

I have edited my gists to reflect changes in Bootstrap 2.3, but that doesn't change the width calculations.

@barryvdh
Copy link
Author

@ashtewari and @adrianoldham, I changed some of the code and tested it with the latest Bootstrap version (2.3) and in both lessphp and less.js. It seems to be working fine now.

less.js seemed to deal differently with the scope variables, and also didn't like isNumber (should be isnumber).

Also tried to make it a bit simpler (just a mixin .customGrid instead #custom .grid()). Does this work better for you?

@lindseybradford
Copy link

Thanks for sharing this code!

I've imported in the order prescribed, and things work fine till I run into an issue in the custom-responsive.less

These two lines throw an error, and return as undefined variables.

Is there a piece I'm missing where you define them elsewhere in your code? I can see the rest is working, as I'm able to get the media queries for this view:

@media (max-width: 767px) {
.customGrid(auto, 0);
}

Much appreciate your help!
LB

@lindseybradford
Copy link

Left out the two troublesome lines… sorry about that:

.customGrid(@gridColumnWidth768, @gridGutterWidth768);

and

.customGrid(@gridColumnWidth1200, @gridGutterWidth1200);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment