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 {
width: (2/@number * 100) + 0%;
}
& > .flexitem-first {
order: -1;
}
@media (min-width: (@small-screen + 1)) and (max-width: @medium-screen) {
& > * {
width: 33.3333%;
}
& > .flexitem-double {
width: 66.6666%;
}
}
@media (min-width: (@tiny-screen + 1)) and (max-width: @small-screen) {
& > * {
width: 50%;
}
& > .flexitem-double {
width: 100%;
}
}
@media (max-width: @tiny-screen) {
& > * {
width: 100%;
}
& > .flexitem-double {
width: 100%;
}
}
} }
/* global styles for direct child ex. .grid3 */ /* Examples : will be compiled in CSS */
.grid > * {
display: block; .grid-2 {
padding: 0; .grid(2);
margin-left: -@gutter; /* gutter value */
text-align: left;
} }
/* global styles for each "cell" */ .grid-3 {
.grid > * > * { .grid(3);
display: inline-block;
padding-left: @gutter; /* gutter value */
margin-left: 0;
vertical-align: top;
} }
/* whitespace fixing for modern browsers including IE9+ */ .grid-4 {
:root .grid { .grid(4);
font-size: 0;
text-justify: distribute-all-lines; /* fallback for IE9+ */
} }
:root .grid > * > * { .grid-5 {
/* fallback for Opera Mini */ .grid(5);
font-size: @base-font-size;
font-size: unit((@base-font-size / 10), rem);
} }
/* Opera hack */ .grid-6 {
.opera:-o-prefocus, .grid(6);
.grid > * {
word-spacing: -0.43em;
} }
.grid2 > * { .grid-7 {
width: 50%; .grid(7);
} }
.grid3 > * { .grid-8 {
width: 33.333%; .grid(8);
} }
.grid4 > * { .grid-10 {
width: 25%; .grid(10);
} }
.grid5 > * { .grid-12 {
width: 20%; .grid(12);
} }
.grid6 > * { .grid-16 {
width: 16.667%; .grid(16);
} }
.grid8 > * { // LESS mixins for *unequal* columns grid container
width: 12.5%; // 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%;
}
}
} }
.grid10 > * {
width: 10%; /* Examples : will be compiled in CSS */
.grid-2-1 {
.uneven-grid(2,1);
} }
.grid12 > * { .grid-1-2 {
width: 8.333%; .uneven-grid(1,2);
} }
/* unequal grids (1-2, 2-1, 1-3 and 3-1) for 2 blocks */ .grid-3-1 {
.grid2-1 > *:first-child, .uneven-grid(3,1);
.grid1-2 > * + * {
width: 66.666%;
} }
.grid1-2 > *:first-child, .grid-1-3 {
.grid2-1 > * + * { .uneven-grid(1,3);
width: 33.333%;
} }
.grid1-3 > *:first-child, .grid-3-2 {
.grid3-1 > * + * { .uneven-grid(3,2);
width: 25%;
} }
.grid3-1 > *:first-child, .grid-2-3 {
.grid1-3 > * + * { .uneven-grid(2,3);
width: 75%;
} }
/* ---------------------------------- */ .grid-4-1 {
/* ==autogrids */ .uneven-grid(4,1);
/* .. to automatically justify blocs */
/* ---------------------------------- */
/* Demo : http://codepen.io/raphaelgoetter/pen/Kqehf */
/* container of autogrids */
[class*="autogrid"] {
text-align: justify;
} }
[class*="autogrid"]:after { .grid-1-4 {
content: ""; .uneven-grid(1,4);
display: inline-block; }
width: 100%;
}
[class*="autogrid"] > * {
display: inline-block;
vertical-align: top;
text-align: left;
}
/* whitespace fixing for modern browsers including IE9+ */
:root [class*="autogrid"] {
font-size: 0;
/* fallback for IE9+ */
text-justify: distribute-all-lines;
}
:root [class*="autogrid"] > * {
/* fallback for Opera Mini */
font-size: @base-font-size;
font-size: unit((@base-font-size / 10), rem);
}
/* Opera hack */
[class*="autogrid"]:-o-prefocus {
word-spacing: -0.43em;
}
.autogrid2 > * {
width: 49%;
}
.autogrid3 > * {
width: 32%;
}
.autogrid4 > * {
width: 23.6%;
}
.autogrid5 > * {
width: 19%;
}
.autogrid6 > * {
width: 15%;
}
.autogrid8 > * {
width: 10.8%;
}
.autogrid10 > * {
width: 9%;
}
.autogrid12 > * {
width: 6.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;
& > * {
/* grid child can be any element */
flex: 0 0 auto;
width: (1/$number * 100) + 0%;
display: block; /* IE fix */
padding: 1em;
border-left: $gutter solid transparent;
background-clip: padding-box !important; /* no background on border */
}
& > .flexitem-double {
width: (2/$number * 100) + 0%;
}
& > .flexitem-first {
order: -1;
}
@media (min-width: ($small-screen + 1)) and (max-width: $medium-screen) {
& > * {
width: 33.3333%;
}
& > .flexitem-double {
width: 66.6666%;
}
}
@media (min-width: ($tiny-screen + 1)) and (max-width: $small-screen) {
& > * {
width: 50%;
}
& > .flexitem-double {
width: 100%;
}
}
@media (max-width: $tiny-screen) {
& > * {
width: 100%;
}
& > .flexitem-double {
width: 100%;
}
}
} }
/* global styles for each "cell" */ /* Examples : will be compiled in CSS */
.grid > * > * {
display: inline-block; .grid-2 {
/* gutter value */ @include grid(2);
padding-left: $gutter;
margin-left: 0;
vertical-align: top;
} }
/* whitespace fixing for modern browsers including IE9+ */ .grid-3 {
:root .grid { @include grid(3);
font-size: 0;
/* fallback for IE9+ */
text-justify: distribute-all-lines;
} }
:root .grid > * > * { .grid-4 {
/* fallback for Opera Mini */ @include grid(4);
font-size: $base-font-size;
font-size: ($base-font-size / 10px) + rem;
} }
/* Opera hack */ .grid-5 {
.opera:-o-prefocus, @include grid(5);
.grid > * {
word-spacing: -0.43em;
} }
.grid2 > * { .grid-6 {
width: 50%; @include grid(6);
} }
.grid3 > * { .grid-7 {
width: 33.333%; @include grid(7);
} }
.grid4 > * { .grid-8 {
width: 25%; @include grid(8);
} }
.grid5 > * { .grid-10 {
width: 20%; @include grid(10);
} }
.grid6 > * { .grid-12 {
width: 16.667%; @include grid(12);
} }
.grid8 > * { .grid-16 {
width: 12.5%; @include grid(16);
} }
.grid10 > * { // LESS mixins for *unequal* columns grid container
width: 10%; // 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%;
}
}
} }
.grid12 > * {
width: 8.333%; /* Examples : will be compiled in CSS */
.grid-2-1 {
@include uneven-grid(2,1);
} }
/* unequal grids (1-2, 2-1, 1-3 and 3-1) for 2 blocks */ .grid-1-2 {
.grid2-1 > *:first-child, @include uneven-grid(1,2);
.grid1-2 > * + * {
width: 66.666%;
} }
.grid1-2 > *:first-child, .grid-3-1 {
.grid2-1 > * + * { @include uneven-grid(3,1);
width: 33.333%;
} }
.grid1-3 > *:first-child, .grid-1-3 {
.grid3-1 > * + * { @include uneven-grid(1,3);
width: 25%;
} }
.grid3-1 > *:first-child, .grid-3-2 {
.grid1-3 > * + * { @include uneven-grid(3,2);
width: 75%;
} }
/* ---------------------------------- */ .grid-2-3 {
/* ==autogrids */ @include uneven-grid(2,3);
/* .. to automatically justify blocs */
/* ---------------------------------- */
/* Demo : http://codepen.io/raphaelgoetter/pen/Kqehf */
/* container of autogrids */
[class*="autogrid"] {
text-align: justify;
} }
[class*="autogrid"]:after { .grid-4-1 {
content: ""; @include uneven-grid(4,1);
display: inline-block;
width: 100%;
} }
[class*="autogrid"] > * { .grid-1-4 {
display: inline-block; @include uneven-grid(1,4);
vertical-align: top; }
text-align: left;
}
/* whitespace fixing for modern browsers including IE9+ */
:root [class*="autogrid"] {
font-size: 0;
/* fallback for IE9+ */
text-justify: distribute-all-lines;
}
:root [class*="autogrid"] > * {
/* fallback for Opera Mini */
font-size: $base-font-size;
font-size: ($base-font-size / 10px) + rem;
}
/* Opera hack */
[class*="autogrid"]:-o-prefocus {
word-spacing: -0.43em;
}
.autogrid2 > * {
width: 49%;
}
.autogrid3 > * {
width: 32%;
}
.autogrid4 > * {
width: 23.6%;
}
.autogrid5 > * {
width: 19%;
}
.autogrid6 > * {
width: 15%;
}
.autogrid8 > * {
width: 10.8%;
}
.autogrid10 > * {
width: 9%;
}
.autogrid12 > * {
width: 6.4%;
}