Refonte intégrale des grilles en Flexbox

... et en plus ça marche sur IE10 !
This commit is contained in:
raphaelgoettter 2015-03-05 11:29:06 +01:00
parent de1716855e
commit c2cb4e7cb9
4 changed files with 269 additions and 265 deletions

View file

@ -45,5 +45,8 @@
@extra-large-screen : 1600px; // screens between 1281px and 1600px @extra-large-screen : 1600px; // screens between 1281px and 1600px
@ultra-large-screen : 1920px; // ultra large screens @ultra-large-screen : 1920px; // ultra large screens
// misc (choose unit you prefer) // grids variables (choose unit you prefer)
@gutter : 20px; // gutter value for grid layouts. Unit can be: %, px, em, rem @gutter: 1em; // gutter value for grid layouts. Unit can be: %, px, em, rem
@number: 4; // number of equal columns
@left: 2; // left side of uneven columns
@right: 1; // right side of uneven columns

View file

@ -1,172 +1,172 @@
/* ---------------------------------- */ /* ---------------------------------- */
/* ==classic grids */ /* ==Grids */
/* .. use it when gutter size matters */
/* ---------------------------------- */ /* ---------------------------------- */
/* grids inspired from SUIT https://github.com/suitcss/suit */ // WARNING : KNACSS grids are flexbox based and only supported by IE10+
// Tuto : http://www.alsacreations.com/tuto/lire/1659-une-grille-responsive-avec-flexbox-et-LESS.html
// Demo : http://codepen.io/raphaelgoetter/pen/zxBMLW
/* overall container of grids */ // Usage in vanilla CSS:
.grid { // - <div class="grid-4"> for an equal fourth columns grid container
overflow: hidden; // - <div class="grid-2-1"> for an uneven columns grid container
// Usage with preprocessors : if you're using LESS, you can config grids variables :
// n = number of columns (default = 4) / g = gutter value (default = 1em)
// example : .grid-container { .grid(12, 10px); }
// ... or uneven grids :
// left = left ratio column (default = 2) / right = right ratio column (default = 1) / gutter (default = 1em)
// example : .grid-container { .uneven-grid(2, 1, 10px); }
// LESS mixins for *equal* columns grid container
// example : .grid-container { .grid(12, 10px); }
.grid(@number:@number, @gutter:@gutter) {
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin-left: -@gutter;
& > * {
/* grid child can be any element */
flex: 0 0 auto;
width: (1/@number * 100) + 0%;
display: block; /* IE fix */
border-left: @gutter solid transparent;
background-clip: padding-box !important; /* no background on border */
} }
& > .flexitem-double {
/* global styles for direct child ex. .grid3 */ width: (2/@number * 100) + 0%;
.grid > * {
display: block;
padding: 0;
margin-left: -@gutter; /* gutter value */
text-align: left;
} }
& > .flexitem-first {
/* global styles for each "cell" */ order: -1;
.grid > * > * {
display: inline-block;
padding-left: @gutter; /* gutter value */
margin-left: 0;
vertical-align: top;
} }
@media (min-width: (@small-screen + 1)) and (max-width: @medium-screen) {
/* whitespace fixing for modern browsers including IE9+ */ & > * {
:root .grid { width: 33.3333%;
font-size: 0;
text-justify: distribute-all-lines; /* fallback for IE9+ */
} }
& > .flexitem-double {
:root .grid > * > * { width: 66.6666%;
/* fallback for Opera Mini */
font-size: @base-font-size;
font-size: unit((@base-font-size / 10), rem);
} }
/* Opera hack */
.opera:-o-prefocus,
.grid > * {
word-spacing: -0.43em;
} }
@media (min-width: (@tiny-screen + 1)) and (max-width: @small-screen) {
.grid2 > * { & > * {
width: 50%; width: 50%;
} }
& > .flexitem-double {
.grid3 > * {
width: 33.333%;
}
.grid4 > * {
width: 25%;
}
.grid5 > * {
width: 20%;
}
.grid6 > * {
width: 16.667%;
}
.grid8 > * {
width: 12.5%;
}
.grid10 > * {
width: 10%;
}
.grid12 > * {
width: 8.333%;
}
/* unequal grids (1-2, 2-1, 1-3 and 3-1) for 2 blocks */
.grid2-1 > *:first-child,
.grid1-2 > * + * {
width: 66.666%;
}
.grid1-2 > *:first-child,
.grid2-1 > * + * {
width: 33.333%;
}
.grid1-3 > *:first-child,
.grid3-1 > * + * {
width: 25%;
}
.grid3-1 > *:first-child,
.grid1-3 > * + * {
width: 75%;
}
/* ---------------------------------- */
/* ==autogrids */
/* .. to automatically justify blocs */
/* ---------------------------------- */
/* Demo : http://codepen.io/raphaelgoetter/pen/Kqehf */
/* container of autogrids */
[class*="autogrid"] {
text-align: justify;
}
[class*="autogrid"]:after {
content: "";
display: inline-block;
width: 100%; width: 100%;
} }
}
[class*="autogrid"] > * { @media (max-width: @tiny-screen) {
display: inline-block; & > * {
vertical-align: top; width: 100%;
text-align: left; }
& > .flexitem-double {
width: 100%;
}
}
} }
/* whitespace fixing for modern browsers including IE9+ */ /* Examples : will be compiled in CSS */
:root [class*="autogrid"] {
font-size: 0; .grid-2 {
/* fallback for IE9+ */ .grid(2);
text-justify: distribute-all-lines;
} }
:root [class*="autogrid"] > * { .grid-3 {
/* fallback for Opera Mini */ .grid(3);
font-size: @base-font-size;
font-size: unit((@base-font-size / 10), rem);
} }
/* Opera hack */ .grid-4 {
[class*="autogrid"]:-o-prefocus { .grid(4);
word-spacing: -0.43em;
} }
.autogrid2 > * { .grid-5 {
width: 49%; .grid(5);
} }
.autogrid3 > * { .grid-6 {
width: 32%; .grid(6);
} }
.autogrid4 > * { .grid-7 {
width: 23.6%; .grid(7);
} }
.autogrid5 > * { .grid-8 {
width: 19%; .grid(8);
} }
.autogrid6 > * { .grid-10 {
width: 15%; .grid(10);
} }
.autogrid8 > * { .grid-12 {
width: 10.8%; .grid(12);
} }
.autogrid10 > * { .grid-16 {
width: 9%; .grid(16);
} }
.autogrid12 > * { // LESS mixins for *unequal* columns grid container
width: 6.4%; // example : .grid-container { .uneven-grid(2, 1, 10px); }
.uneven-grid(@left:@left, @right:@right, @gutter:@gutter) {
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin-left: -@gutter;
& > * {
display: block; /* IE fix */
border-left: @gutter solid transparent;
background-clip: padding-box !important; /* no background on border */
}
& > *:nth-child(odd) {
width: (@left / (@left + @right)) * 100%;
}
& > *:nth-child(even) {
width: (@right / (@left + @right)) * 100%;
}
@media (max-width: @tiny-screen) {
& > *:nth-child(n) {
width: 100%;
}
}
}
/* Examples : will be compiled in CSS */
.grid-2-1 {
.uneven-grid(2,1);
}
.grid-1-2 {
.uneven-grid(1,2);
}
.grid-3-1 {
.uneven-grid(3,1);
}
.grid-1-3 {
.uneven-grid(1,3);
}
.grid-3-2 {
.uneven-grid(3,2);
}
.grid-2-3 {
.uneven-grid(2,3);
}
.grid-4-1 {
.uneven-grid(4,1);
}
.grid-1-4 {
.uneven-grid(1,4);
} }

View file

@ -45,5 +45,8 @@ $large-screen : 1280px; // screens between 1025px and 1280px
$extra-large-screen : 1600px; // screens between 1281px and 1600px $extra-large-screen : 1600px; // screens between 1281px and 1600px
$ultra-large-screen : 1920px; // ultra large screens $ultra-large-screen : 1920px; // ultra large screens
// misc (choose unit you prefer) // grids variables (choose unit you prefer)
$gutter : 20px; // gutter value for grid layouts. Unit can be: %, px, em, rem $gutter: 1em; // gutter value for grid layouts. Unit can be: %, px, em, rem
$number: 4; // number of equal columns
$left: 2; // left side of uneven columns
$right: 1; // right side of uneven columns

View file

@ -1,175 +1,173 @@
/* ---------------------------------- */ /* ---------------------------------- */
/* ==classic grids */ /* ==Grids */
/* .. use it when gutter size matters */
/* ---------------------------------- */ /* ---------------------------------- */
/* grids inspired from SUIT https://github.com/suitcss/suit */ // WARNING : KNACSS grids are flexbox based and only supported by IE10+
// Tuto : http://www.alsacreations.com/tuto/lire/1659-une-grille-responsive-avec-flexbox-et-LESS.html
// Demo : http://codepen.io/raphaelgoetter/pen/ZYjwEB
/* overall container of grids */ // Usage in vanilla CSS:
.grid { // - <div class="grid-4"> for an equal fourth columns grid container
overflow: hidden; // - <div class="grid-2-1"> for an uneven columns grid container
}
/* global styles for direct child ex. .grid3 */ // Usage with preprocessors : if you're using Sass, you can config grids variables :
.grid > * { // n = number of columns (default = 4) / g = gutter value (default = 1em)
display: block; // example : .grid-container { @include grid(12, 10px); }
padding: 0; // ... or uneven grids :
/* gutter value */ // left = left ratio column (default = 2) / right = right ratio column (default = 1) / gutter (default = 1em)
// example : .grid-container { @include uneven-grid(2, 1, 10px); }
// Sass mixins for *equal* columns grid container
// example : .grid-container { @include grid(12, 10px); }
@mixin grid($number:$number,$gutter:$gutter) {
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin-left: -$gutter; margin-left: -$gutter;
text-align: left;
}
/* global styles for each "cell" */ & > * {
.grid > * > * { /* grid child can be any element */
display: inline-block; flex: 0 0 auto;
/* gutter value */ width: (1/$number * 100) + 0%;
padding-left: $gutter; display: block; /* IE fix */
margin-left: 0; padding: 1em;
vertical-align: top; border-left: $gutter solid transparent;
background-clip: padding-box !important; /* no background on border */
} }
& > .flexitem-double {
/* whitespace fixing for modern browsers including IE9+ */ width: (2/$number * 100) + 0%;
:root .grid {
font-size: 0;
/* fallback for IE9+ */
text-justify: distribute-all-lines;
} }
& > .flexitem-first {
:root .grid > * > * { order: -1;
/* fallback for Opera Mini */
font-size: $base-font-size;
font-size: ($base-font-size / 10px) + rem;
} }
@media (min-width: ($small-screen + 1)) and (max-width: $medium-screen) {
/* Opera hack */ & > * {
.opera:-o-prefocus, width: 33.3333%;
.grid > * {
word-spacing: -0.43em;
} }
& > .flexitem-double {
.grid2 > * { width: 66.6666%;
}
}
@media (min-width: ($tiny-screen + 1)) and (max-width: $small-screen) {
& > * {
width: 50%; width: 50%;
} }
& > .flexitem-double {
.grid3 > * {
width: 33.333%;
}
.grid4 > * {
width: 25%;
}
.grid5 > * {
width: 20%;
}
.grid6 > * {
width: 16.667%;
}
.grid8 > * {
width: 12.5%;
}
.grid10 > * {
width: 10%;
}
.grid12 > * {
width: 8.333%;
}
/* unequal grids (1-2, 2-1, 1-3 and 3-1) for 2 blocks */
.grid2-1 > *:first-child,
.grid1-2 > * + * {
width: 66.666%;
}
.grid1-2 > *:first-child,
.grid2-1 > * + * {
width: 33.333%;
}
.grid1-3 > *:first-child,
.grid3-1 > * + * {
width: 25%;
}
.grid3-1 > *:first-child,
.grid1-3 > * + * {
width: 75%;
}
/* ---------------------------------- */
/* ==autogrids */
/* .. to automatically justify blocs */
/* ---------------------------------- */
/* Demo : http://codepen.io/raphaelgoetter/pen/Kqehf */
/* container of autogrids */
[class*="autogrid"] {
text-align: justify;
}
[class*="autogrid"]:after {
content: "";
display: inline-block;
width: 100%; width: 100%;
} }
}
[class*="autogrid"] > * { @media (max-width: $tiny-screen) {
display: inline-block; & > * {
vertical-align: top; width: 100%;
text-align: left; }
& > .flexitem-double {
width: 100%;
}
}
} }
/* whitespace fixing for modern browsers including IE9+ */ /* Examples : will be compiled in CSS */
:root [class*="autogrid"] {
font-size: 0; .grid-2 {
/* fallback for IE9+ */ @include grid(2);
text-justify: distribute-all-lines;
} }
:root [class*="autogrid"] > * { .grid-3 {
/* fallback for Opera Mini */ @include grid(3);
font-size: $base-font-size;
font-size: ($base-font-size / 10px) + rem;
} }
/* Opera hack */ .grid-4 {
[class*="autogrid"]:-o-prefocus { @include grid(4);
word-spacing: -0.43em;
} }
.autogrid2 > * { .grid-5 {
width: 49%; @include grid(5);
} }
.autogrid3 > * { .grid-6 {
width: 32%; @include grid(6);
} }
.autogrid4 > * { .grid-7 {
width: 23.6%; @include grid(7);
} }
.autogrid5 > * { .grid-8 {
width: 19%; @include grid(8);
} }
.autogrid6 > * { .grid-10 {
width: 15%; @include grid(10);
} }
.autogrid8 > * { .grid-12 {
width: 10.8%; @include grid(12);
} }
.autogrid10 > * { .grid-16 {
width: 9%; @include grid(16);
} }
.autogrid12 > * { // LESS mixins for *unequal* columns grid container
width: 6.4%; // example : .grid-container { @include uneven-grid(2, 1, 10px); }
@mixin uneven-grid($left:$left, $right:$right, $gutter:$gutter) {
display: flex;
flex-direction: row;
flex-wrap: wrap;
margin-left: -$gutter;
& > * {
display: block; /* IE fix */
border-left: $gutter solid transparent;
background-clip: padding-box !important; /* no background on border */
}
& > *:nth-child(odd) {
width: ($left / ($left + $right)) * 100%;
}
& > *:nth-child(even) {
width: ($right / ($left + $right)) * 100%;
}
@media (max-width: $tiny-screen) {
& > *:nth-child(n) {
width: 100%;
}
}
}
/* Examples : will be compiled in CSS */
.grid-2-1 {
@include uneven-grid(2,1);
}
.grid-1-2 {
@include uneven-grid(1,2);
}
.grid-3-1 {
@include uneven-grid(3,1);
}
.grid-1-3 {
@include uneven-grid(1,3);
}
.grid-3-2 {
@include uneven-grid(3,2);
}
.grid-2-3 {
@include uneven-grid(2,3);
}
.grid-4-1 {
@include uneven-grid(4,1);
}
.grid-1-4 {
@include uneven-grid(1,4);
} }