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

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
Copy link

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
Copy link

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
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