Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/234bfc604d22457f052f to your computer and use it in GitHub Desktop.
Save mindplay-dk/234bfc604d22457f052f to your computer and use it in GitHub Desktop.
Semantic grid mix-ins for LESS

Example LESS file:

.layout {
    .row();    
    background: #ddd;
}

.nav {
    .col(4);
    background: red;
}

.content {
    .col(6);
    background: green;
}

.ads {
    .col(2);
    background: blue;
}

Resulting CSS:

.layout {
  display: block;
  width: 940px;
  overflow: hidden;
  background: #ddd;
}
.layout > :first-child {
  margin-left: 0;
}
.nav {
  display: inline-block;
  float: left;
  width: 300px;
  margin-left: 20px;
  background: red;
}
.content {
  display: inline-block;
  float: left;
  width: 460px;
  margin-left: 20px;
  background: green;
}
.ads {
  display: inline-block;
  float: left;
  width: 140px;
  margin-left: 20px;
  background: blue;
}

Example in fiddle here

@grid-column-size: 60;
@grid-gutter-size: 20;
@grid-num-columns: 12;
@grid-unit: 1px;
.width (@n) {
& when (@n = 1) {
width: @grid-column-size * @n * @grid-unit;
}
& when (@n > 1) {
width: (@grid-column-size * @n + @grid-gutter-size * (@n - 1)) * @grid-unit;
}
}
.row (@columns: @grid-num-columns) {
display: block;
.width(@columns);
overflow: hidden;
> :first-child { margin-left: 0; }
}
.col (@columns: 1) {
display: inline-block;
float: left;
.width(@columns);
margin-left: @grid-gutter-size * @grid-unit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment