Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

barryvdh commented Jun 4, 2012

Responsive file: https://gist.github.com/2867387 (import after this code)

@ashtewari

This comment has been minimized.

Copy link

ashtewari commented Jan 28, 2013

Is there an example of how to use this custom mixin in my own .less stylesheet? I am using Winless to compile and it is complaining that .row() is not defined. Here is my style.less file -

@import "less/variables.less";
@import "less/mixins.less";
@import "less/grid.less";

@import "custom-grid.less";
@import "custom-responsive.less";

.announcements {
color: brown;
.row();

.announcement
{
color:green;
}
}


custom-grid.less is the current gist.
custom-responsive.less is this gist - https://gist.github.com/2867387

@ashtewari

This comment has been minimized.

Copy link

ashtewari commented Jan 28, 2013

Changed to -
.announcements {
color: brown;
#custom > .grid.row;

.announcement
{
color:green;
#custom > .grid.span(4);
}
}

now it is compiling .. but ..
the result is not quite what I am expecting.

expecting - http://w8.site44.com/hero0.html
actual - http://w8.site44.com/hero.html

Any help will be greatly appreciated.

@barryvdh

This comment has been minimized.

Copy link
Owner Author

barryvdh commented Jan 29, 2013

It is a bit confusing, I know.

All your grid definitions have to be inside that #custom > .grid() mixin. This is because of the scope of the variables.
You run that mixin for every media width, so you only should define the grid itself, not extra options.

The mixins for .span() and .row() are always needed inside that mixin, because they use the variables @gridColumnWidth and @gridGutterWidth

So place your classes after //Define the semantic grid: instead of my example definitions.

After the mixin, run

#custom > .grid(@gridColumnWidth, @gridGutterWidth);

to create the regular grid. Include my responsive file AFTER that (not before). This will generate the other sizes of the grid inside a media query.

After all that, you can do the rest of the less markup. So for you example, it could be something like:

@import "less/variables.less";
@import "less/mixins.less";
@import "less/grid.less";

//Generate grid below  
#custom {          
    .grid(@gridColumnWidth, @gridGutterWidth){   //Generate our custom grid
        //Define styles for our custom grid 
        .span (@columns, @offset: 0) when(isNumber(@gridColumnWidth)){ 
            .makeColumn(@columns, @offset);
        } 
        .row()  when(isNumber(@gridColumnWidth)){
            .makeRow(); 
        }
        //Define spans/row for small screens (with responsive mediaqueries)
        .span (@columns, @offset: 0) when(@gridColumnWidth = auto){ 
            float: none;
            display: block;
            width: auto;
            margin: 0;   
        }   
        .row() when(@gridColumnWidth = auto){ 
            margin-left: 0;
        }

        //Define the semantic grid:
        .announcements {
            .row();
            .announcement {
                .span(4);
            }
        }
    } 
} 

#custom > .grid(@gridColumnWidth, @gridGutterWidth);
@import "custom-responsive.less";

//Rest of the normal LESS code
.announcements {
    color: brown;
    .announcement {
        color:green;
    }
}
@ashtewari

This comment has been minimized.

Copy link

ashtewari commented Jan 30, 2013

I see. It makes sense now. The grid is responsive now. The span(4) width is not the same as the original (non-semantic) version, but I will play around with this to see what might be missing.

Thanks for taking the time to explain this.

@adrianoldham

This comment has been minimized.

Copy link

adrianoldham commented Feb 21, 2013

Thanks for sharing. Unfortunately I have encountered some problems using the prescribed method.

I see the same span width calculation errors as mentioned here by @ashtewari. Ash has kindly created and shared his example files, you can see the error on his test site too (http://ash.site44.com/hero.html).

The other problem I have is, while this appears to sort-of work using less.js, I can't get this to compile at all using my preprocessor (CodeKit for Mac OS X), I see the following error:

No matching definition was found for .row()

Here's hoping that a real solution will eventually be integrated into the Boostrap core.

@barryvdh

This comment has been minimized.

Copy link
Owner Author

barryvdh commented Feb 24, 2013

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

This comment has been minimized.

Copy link
Owner Author

barryvdh commented Feb 25, 2013

@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

This comment has been minimized.

Copy link

lindseybradford commented May 1, 2013

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

This comment has been minimized.

Copy link

lindseybradford commented May 1, 2013

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
You can’t perform that action at this time.