beautifying code

This commit is contained in:
raphaelgoettter 2015-08-27 11:04:01 +02:00
parent 9f080f6071
commit 06e334f794
4 changed files with 115 additions and 312 deletions

View file

@ -4,35 +4,37 @@
// Tuto : http://www.alsacreations.com/tuto/lire/1659-une-grille-responsive-avec-flexbox-et-LESS.html // Tuto : http://www.alsacreations.com/tuto/lire/1659-une-grille-responsive-avec-flexbox-et-LESS.html
// Demo : http://codepen.io/raphaelgoetter/pen/zxBMLW // Demo : http://codepen.io/raphaelgoetter/pen/zxBMLW
// Usage in vanilla CSS: // Usage in vanilla CSS:
// - <div class="grid-4"> for an equal fourth columns grid container // - <div class="grid-4"> for an equal fourth columns grid container
// - <div class="grid-2-1"> for an uneven columns grid container // - <div class="grid-2-1"> for an uneven columns grid container
// Usage with preprocessors : if you're using LESS, you can config grids variables : // Usage with preprocessors : if you're using LESS, you can config grids variables :
// n = number of columns (default = 4) // n = number of columns (default = 4)
// example : .grid-perso { .grid(12); } // example : .grid-perso { .grid(12); }
// ... or uneven grids : // ... or uneven grids :
// left = left ratio column (default = 2) / right = right ratio column (default = 1) // left = left ratio column (default = 2) / right = right ratio column (default = 1)
// example : .grid-perso { .uneven-grid(2, 1); } // example : .grid-perso { .uneven-grid(2, 1); }
/* grid container */
/* grid container */
[class*="@{kna-namespace}grid-"] { [class*="@{kna-namespace}grid-"] {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
flex-wrap: wrap; flex-wrap: wrap;
margin-left: -@gutter; margin-left: -@gutter;
/* inline-block fallback for IE9 generation */
/* inline-block fallback for IE9 generation */
letter-spacing: -0.31em; letter-spacing: -0.31em;
text-rendering: optimizespeed; text-rendering: optimizespeed;
} }
/* grid childs */
/* grid childs */
[class*="@{kna-namespace}grid-"] > * { [class*="@{kna-namespace}grid-"] > * {
flex: 0 0 auto; flex: 0 0 auto;
width: ~'calc(100% * 1 / @{number} - @{gutter} - .01px)'; width: ~'calc(100% * 1 / @{number} - @{gutter} - .01px)';
margin-left: @gutter; margin-left: @gutter;
/* inline-block fallback for IE9 generation */
/* inline-block fallback for IE9 generation */
display: inline-block; display: inline-block;
vertical-align: top; vertical-align: top;
letter-spacing: normal; letter-spacing: normal;
@ -41,15 +43,11 @@
// LESS mixins for *equal* columns grid container // LESS mixins for *equal* columns grid container
// example : .grid-perso { .grid(12); } // example : .grid-perso { .grid(12); }
.grid(@number: @number, .grid(@number:@number, @gutter:@gutter) {
@gutter: @gutter) {
& > * { & > * {
width: ~'calc(100% * 1 / @{number} - @{gutter} - .01px)'; width: ~'calc(100% * 1 / @{number} - @{gutter} - .01px)';
} }
& > .@ { & > .@{kna-namespace}flex-item-double {
kna-namespace
}
flex-item-double {
width: ~'calc(100% * 2 / @{number} - @{gutter} - .01px)'; width: ~'calc(100% * 2 / @{number} - @{gutter} - .01px)';
} }
} }
@ -58,91 +56,80 @@
[class*="@{kna-namespace}grid-2"] { [class*="@{kna-namespace}grid-2"] {
.grid(2); .grid(2);
} }
[class*="@{kna-namespace}grid-3"] { [class*="@{kna-namespace}grid-3"] {
.grid(3); .grid(3);
} }
[class*="@{kna-namespace}grid-4"] { [class*="@{kna-namespace}grid-4"] {
.grid(4); .grid(4);
} }
[class*="@{kna-namespace}grid-5"] { [class*="@{kna-namespace}grid-5"] {
.grid(5); .grid(5);
} }
[class*="@{kna-namespace}grid-6"] { [class*="@{kna-namespace}grid-6"] {
.grid(6); .grid(6);
} }
[class*="@{kna-namespace}grid-7"] { [class*="@{kna-namespace}grid-7"] {
.grid(7); .grid(7);
} }
[class*="@{kna-namespace}grid-8"] { [class*="@{kna-namespace}grid-8"] {
.grid(8); .grid(8);
} }
[class*="@{kna-namespace}grid-10"] { [class*="@{kna-namespace}grid-10"] {
.grid(10); .grid(10);
} }
[class*="@{kna-namespace}grid-12"] { [class*="@{kna-namespace}grid-12"] {
.grid(12); .grid(12);
} }
/* Responsive grid */
/* Responsive grid */
// "small-2" = 2 columns when small screen // "small-2" = 2 columns when small screen
// example : .grid-4-small-2 will be 4 then 2 columns // example : .grid-4-small-2 will be 4 then 2 columns
@media (max-width: @small-screen) { @media (max-width: @small-screen) {
[class*="-small-4"] > * { [class*="-small-4"] > * {
width: ~'calc(100% * 1 / 4 - @{gutter} - .01px)'; width: ~'calc(100% * 1 / 4 - @{gutter} - .01px)';
} }
[class*="-small-4"] > .flex-item-double { [class*="-small-4"] > .flexitem-double {
width: ~'calc(100% * 1 / 2 - @{gutter} - .01px)'; width: ~'calc(100% * 1 / 2 - @{gutter} - .01px)';
} }
[class*="-small-3"] > * { [class*="-small-3"] > * {
width: ~'calc(100% * 1 / 3 - @{gutter} - .01px)'; width: ~'calc(100% * 1 / 3 - @{gutter} - .01px)';
} }
[class*="-small-3"] > .flex-item-double { [class*="-small-3"] > .flexitem-double {
width: ~'calc(100% * 2 / 3 - @{gutter} - .01px)'; width: ~'calc(100% * 2 / 3 - @{gutter} - .01px)';
} }
[class*="-small-2"] > * { [class*="-small-2"] > * {
width: ~'calc(100% * 1 / 2 - @{gutter} - .01px)'; width: ~'calc(100% * 1 / 2 - @{gutter} - .01px)';
} }
[class*="-small-2"] > .flex-item-double { [class*="-small-2"] > .flexitem-double {
width: ~'calc(100% - @{gutter} - .01px)'; width: ~'calc(100% - @{gutter} - .01px)';
} }
[class*="-small-1"] > * { [class*="-small-1"] > * {
width: ~'calc(100% - @{gutter} - .01px)'; width: ~'calc(100% - @{gutter} - .01px)';
} }
[class*="-small-1"] > .flex-item-double { [class*="-small-1"] > .flexitem-double {
width: ~'calc(100% - @{gutter} - .01px)'; width: ~'calc(100% - @{gutter} - .01px)';
} }
} }
// "tiny-1" = 1 column when tiny screen // "tiny-1" = 1 column when tiny screen
// example : .grid-4-small-2-tiny-1 will be 4 then 2 columns then 1 column // example : .grid-4-small-2-tiny-1 will be 4 then 2 columns then 1 column
@media (max-width: @tiny-screen) { @media (max-width: @tiny-screen) {
[class*="-tiny-2"] > * { [class*="-tiny-2"] > * {
width: ~'calc(100% * 1 / 2 - @{gutter} - .01px)'; width: ~'calc(100% * 1 / 2 - @{gutter} - .01px)';
} }
[class*="-tiny-2"] > .flex-item-double { [class*="-tiny-2"] > .flexitem-double {
width: ~'calc(100% - @{gutter} - .01px)'; width: ~'calc(100% - @{gutter} - .01px)';
} }
[class*="-tiny-1"] > * { [class*="-tiny-1"] > * {
width: ~'calc(100% - @{gutter} - .01px)'; width: ~'calc(100% - @{gutter} - .01px)';
} }
[class*="-tiny-1"] > .flex-item-double { [class*="-tiny-1"] > .flexitem-double {
width: ~'calc(100% - @{gutter} - .01px)'; width: ~'calc(100% - @{gutter} - .01px)';
} }
} }
// LESS mixins for *unequal* columns grid container // LESS mixins for *unequal* columns grid container
// example : .grid-perso { .uneven-grid(2, 1); } // example : .grid-perso { .uneven-grid(2, 1); }
.uneven-grid(@left: @left, .uneven-grid(@left:@left, @right:@right, @gutter:@gutter) {
@right: @right,
@gutter: @gutter) {
& > *:nth-child(odd) { & > *:nth-child(odd) {
@size: (@left / (@left + @right)) * 100%; @size: (@left / (@left + @right)) * 100%;
width: ~'calc(@{size} - @{gutter} - .01px)'; width: ~'calc(@{size} - @{gutter} - .01px)';
@ -159,82 +146,35 @@
} }
// Examples : will be compiled in CSS // Examples : will be compiled in CSS
.@ { .@{kna-namespace}grid-2-1 {
kna-namespace
}
grid-2-1 {
.uneven-grid(2,1); .uneven-grid(2,1);
} }
.@{kna-namespace}grid-1-2 {
.@ {
kna-namespace
}
grid-1-2 {
.uneven-grid(1,2); .uneven-grid(1,2);
} }
.@{kna-namespace}grid-3-1 {
.@ {
kna-namespace
}
grid-3-1 {
.uneven-grid(3,1); .uneven-grid(3,1);
} }
.@{kna-namespace}grid-1-3 {
.@ {
kna-namespace
}
grid-1-3 {
.uneven-grid(1,3); .uneven-grid(1,3);
} }
.@{kna-namespace}grid-3-2 {
.@ {
kna-namespace
}
grid-3-2 {
.uneven-grid(3,2); .uneven-grid(3,2);
} }
.@{kna-namespace}grid-2-3 {
.@ {
kna-namespace
}
grid-2-3 {
.uneven-grid(2,3); .uneven-grid(2,3);
} }
.@{kna-namespace}grid-4-1 {
.@ {
kna-namespace
}
grid-4-1 {
.uneven-grid(4,1); .uneven-grid(4,1);
} }
.@{kna-namespace}grid-1-4 {
.@ {
kna-namespace
}
grid-1-4 {
.uneven-grid(1,4); .uneven-grid(1,4);
} }
.@ { .@{kna-namespace}pull {
kna-namespace
}
pull {
margin-right: auto; margin-right: auto;
} }
.@ { .@{kna-namespace}push {
kna-namespace
}
push {
margin-left: auto; margin-left: auto;
} }

View file

@ -1,8 +1,8 @@
/* ----------------------------- */ /* ----------------------------- */
/* ==minor stylings */ /* ==minor stylings */
/* ----------------------------- */ /* ----------------------------- */
/* styling elements */
/* styling elements */
code, code,
kbd, kbd,
mark { mark {
@ -75,20 +75,12 @@ q {
} }
q, q,
.@ { .@{kna-namespace}q {
kna-namespace
}
q {
quotes: "“\00a0" "\00a0”"; quotes: "“\00a0" "\00a0”";
} }
q:lang(fr), q:lang(fr),
.@ { .@{kna-namespace}q:lang(fr) {
kna-namespace
}
q:lang(fr) {
quotes: "«\00a0" "\00a0»"; quotes: "«\00a0" "\00a0»";
} }
@ -102,14 +94,10 @@ hr {
color: #ccc; color: #ccc;
background-color: #ccc; background-color: #ccc;
} }
/* tables */ /* tables */
table, table,
.@ { .@{kna-namespace}table {
kna-namespace
}
table {
border: 1px solid #ccc; border: 1px solid #ccc;
} }

View file

@ -4,41 +4,37 @@
// Tuto : http://www.alsacreations.com/tuto/lire/1659-une-grille-responsive-avec-flexbox-et-LESS.html // Tuto : http://www.alsacreations.com/tuto/lire/1659-une-grille-responsive-avec-flexbox-et-LESS.html
// Demo : http://codepen.io/raphaelgoetter/pen/ZYjwEB // Demo : http://codepen.io/raphaelgoetter/pen/ZYjwEB
// Usage in vanilla CSS: // Usage in vanilla CSS:
// - <div class="grid-4"> for an equal fourth columns grid container // - <div class="grid-4"> for an equal fourth columns grid container
// - <div class="grid-2-1"> for an uneven columns grid container // - <div class="grid-2-1"> for an uneven columns grid container
// Usage with preprocessors : if you're using Sass, you can config grids variables : // Usage with preprocessors : if you're using Sass, you can config grids variables :
// n = number of columns (default = 4) / g = gutter value (default = 1em) // n = number of columns (default = 4) / g = gutter value (default = 1em)
// example : .grid-perso { @include grid(12, 10px); } // example : .grid-perso { @include grid(12, 10px); }
// ... or uneven grids : // ... or uneven grids :
// left = left ratio column (default = 2) / right = right ratio column (default = 1) // left = left ratio column (default = 2) / right = right ratio column (default = 1)
// example : .grid-perso { @include uneven-grid(2, 1, 10px); } // example : .grid-perso { @include uneven-grid(2, 1, 10px); }
/* grid container */
/* grid container */
[class*="#{$kna-namespace}grid-"] { [class*="#{$kna-namespace}grid-"] {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
flex-wrap: wrap; flex-wrap: wrap;
margin-left: -$gutter; margin-left: -$gutter;
/* inline-block fallback for IE9 generation */
/* inline-block fallback for IE9 generation */
letter-spacing: -0.31em; letter-spacing: -0.31em;
text-rendering: optimizespeed; text-rendering: optimizespeed;
} }
/* grid childs */
/* grid childs */
[class*="#{$kna-namespace}grid-"] > * { [class*="#{$kna-namespace}grid-"] > * {
flex: 0 0 auto; flex: 0 0 auto;
width: calc(100% * 1 / # { width: calc(100% * 1 / #{$number} - #{$gutter} - .01px);
$number
}
- # {
$gutter
}
- .01px);
margin-left: $gutter; margin-left: $gutter;
/* inline-block fallback for IE9 generation */
/* inline-block fallback for IE9 generation */
display: inline-block; display: inline-block;
vertical-align: top; vertical-align: top;
letter-spacing: normal; letter-spacing: normal;
@ -47,32 +43,17 @@
// Sass mixins for *equal* columns grid container // Sass mixins for *equal* columns grid container
// example : .grid-perso { @include grid(12); } // example : .grid-perso { @include grid(12); }
@mixin grid($number:$number, @mixin grid($number:$number,$gutter:$gutter) {
$gutter:$gutter) {
& > * { & > * {
width: calc(100% * 1 / # { width: calc(100% * 1 / #{$number} - #{$gutter} - .01px);
$number
} }
- # { & > .#{$kna-namespace}flex-item-double {
$gutter width: calc(100% * 2 / #{$number} - #{$gutter});
}
- .01px);
}
& > .# {
$kna-namespace
}
flex-item-double {
width: calc(100% * 2 / # {
$number
}
- # {
$gutter
}
);
} }
} }
// Examples : will be compiled in CSS // Examples : will be compiled in CSS
[class*="#{$kna-namespace}grid-2"] { [class*="#{$kna-namespace}grid-2"] {
@include grid(2); @include grid(2);
} }
@ -108,202 +89,110 @@ $gutter:$gutter) {
[class*="#{$kna-namespace}grid-12"] { [class*="#{$kna-namespace}grid-12"] {
@include grid(12); @include grid(12);
} }
/* Responsive grid */
/* Responsive grid */
// "small-2" = 2 columns when small screen // "small-2" = 2 columns when small screen
// example : .grid-4-small-2 will be 4 then 2 columns // example : .grid-4-small-2 will be 4 then 2 columns
@media (max-width: $small-screen) { @media (max-width: $small-screen) {
[class*="-small-4"] > * { [class*="-small-4"] > * {
width: calc(100% * 1 / 4 - # { width: calc(100% * 1 / 4 - #{$gutter} - .01px);
$gutter
} }
- .01px); [class*="-small-4"] > .flexitem-double {
} width: calc(100% * 1 / 2 - #{$gutter} - .01px);
[class*="-small-4"] > .flex-item-double {
width: calc(100% * 1 / 2 - # {
$gutter
}
- .01px);
} }
[class*="-small-3"] > * { [class*="-small-3"] > * {
width: calc(100% * 1 / 3 - # { width: calc(100% * 1 / 3 - #{$gutter} - .01px);
$gutter
} }
- .01px); [class*="-small-3"] > .flexitem-double {
} width: calc(100% * 2 / 3 - #{$gutter} - .01px);
[class*="-small-3"] > .flex-item-double {
width: calc(100% * 2 / 3 - # {
$gutter
}
- .01px);
} }
[class*="-small-2"] > * { [class*="-small-2"] > * {
width: calc(100% * 1 / 2 - # { width: calc(100% * 1 / 2 - #{$gutter} - .01px);
$gutter
} }
- .01px); [class*="-small-2"] > .flexitem-double {
} width: calc(100% - #{$gutter} - .01px);
[class*="-small-2"] > .flex-item-double {
width: calc(100% - # {
$gutter
}
- .01px);
} }
[class*="-small-1"] > * { [class*="-small-1"] > * {
width: calc(100% - # { width: calc(100% - #{$gutter} - .01px);
$gutter
} }
- .01px); [class*="-small-1"] > .flexitem-double {
} width: calc(100% - #{$gutter} - .01px);
[class*="-small-1"] > .flex-item-double {
width: calc(100% - # {
$gutter
}
- .01px);
} }
} }
// "tiny-1" = 1 column when tiny screen // "tiny-1" = 1 column when tiny screen
// example : .grid-4-small-2-tiny-1 will be 4 then 2 columns then 1 column // example : .grid-4-small-2-tiny-1 will be 4 then 2 columns then 1 column
@media (max-width: $tiny-screen) { @media (max-width: $tiny-screen) {
[class*="-tiny-2"] > * { [class*="-tiny-2"] > * {
width: calc(100% * 1 / 2 - # { width: calc(100% * 1 / 2 - #{$gutter} - .01px);
$gutter
} }
- .01px); [class*="-tiny-2"] > .flexitem-double {
} width: calc(100% - #{$gutter} - .01px);
[class*="-tiny-2"] > .flex-item-double {
width: calc(100% - # {
$gutter
}
- .01px);
} }
[class*="-tiny-1"] > * { [class*="-tiny-1"] > * {
width: calc(100% - # { width: calc(100% - #{$gutter} - .01px);
$gutter
} }
- .01px); [class*="-tiny-1"] > .flexitem-double {
} width: calc(100% - #{$gutter} - .01px);
[class*="-tiny-1"] > .flex-item-double {
width: calc(100% - # {
$gutter
}
- .01px);
} }
} }
// LESS mixins for *unequal* columns grid container // LESS mixins for *unequal* columns grid container
// example : .grid-perso { @include uneven-grid(2, 1); } // example : .grid-perso { @include uneven-grid(2, 1); }
@mixin uneven-grid($left:$left, @mixin uneven-grid($left:$left, $right:$right, $gutter:$gutter) {
$right:$right,
$gutter:$gutter) {
& > *:nth-child(odd) { & > *:nth-child(odd) {
$size: ($left / ($left + $right)) * 100%; $size: ($left / ($left + $right)) * 100%;
width: calc(# { width: calc(#{$size} - #{$gutter});
$size
}
- # {
$gutter
}
);
} }
& > *:nth-child(even) { & > *:nth-child(even) {
$size: ($right / ($left + $right)) * 100%; $size: ($right / ($left + $right)) * 100%;
width: calc(# { width: calc(#{$size} - #{$gutter});
$size
}
- # {
$gutter
}
);
} }
@media (max-width: $small-screen) { @media (max-width: $small-screen) {
& > *:nth-child(n) { & > *:nth-child(n) {
width: calc(100% - # { width: calc(100% - #{$gutter});
$gutter
}
);
} }
} }
} }
// Examples : will be compiled in CSS // Examples : will be compiled in CSS
.# {
$kna-namespace
}
grid-2-1 { .#{$kna-namespace}grid-2-1 {
@include uneven-grid(2,1); @include uneven-grid(2,1);
} }
.# { .#{$kna-namespace}grid-1-2 {
$kna-namespace
}
grid-1-2 {
@include uneven-grid(1,2); @include uneven-grid(1,2);
} }
.# { .#{$kna-namespace}grid-3-1 {
$kna-namespace
}
grid-3-1 {
@include uneven-grid(3,1); @include uneven-grid(3,1);
} }
.# { .#{$kna-namespace}grid-1-3 {
$kna-namespace
}
grid-1-3 {
@include uneven-grid(1,3); @include uneven-grid(1,3);
} }
.# { .#{$kna-namespace}grid-3-2 {
$kna-namespace
}
grid-3-2 {
@include uneven-grid(3,2); @include uneven-grid(3,2);
} }
.# { .#{$kna-namespace}grid-2-3 {
$kna-namespace
}
grid-2-3 {
@include uneven-grid(2,3); @include uneven-grid(2,3);
} }
.# { .#{$kna-namespace}grid-4-1 {
$kna-namespace
}
grid-4-1 {
@include uneven-grid(4,1); @include uneven-grid(4,1);
} }
.# { .#{$kna-namespace}grid-1-4 {
$kna-namespace
}
grid-1-4 {
@include uneven-grid(1,4); @include uneven-grid(1,4);
} }
.# { .#{$kna-namespace}pull {
$kna-namespace
}
pull {
margin-right: auto; margin-right: auto;
} }
.# { .#{$kna-namespace}push {
$kna-namespace
}
push {
margin-left: auto; margin-left: auto;
} }

View file

@ -1,11 +1,9 @@
/* ----------------------------- */ /* ----------------------------- */
/* ==minor stylings */ /* ==minor stylings */
/* ----------------------------- */ /* ----------------------------- */
/* styling elements */
code, /* styling elements */
kbd, code, kbd, mark {
mark {
border-radius: 2px; border-radius: 2px;
} }
@ -75,20 +73,12 @@ q {
} }
q, q,
.# { .#{$kna-namespace}q {
$kna-namespace
}
q {
quotes: "“\00a0" "\00a0”"; quotes: "“\00a0" "\00a0”";
} }
q:lang(fr), q:lang(fr),
.# { .#{$kna-namespace}q:lang(fr) {
$kna-namespace
}
q:lang(fr) {
quotes: "«\00a0" "\00a0»"; quotes: "«\00a0" "\00a0»";
} }
@ -102,14 +92,10 @@ hr {
color: #ccc; color: #ccc;
background-color: #ccc; background-color: #ccc;
} }
/* tables */ /* tables */
table, table,
.# { .#{$kna-namespace}table {
$kna-namespace
}
table {
border: 1px solid #ccc; border: 1px solid #ccc;
} }