public
Last active — forked from /Flexbox mixins

This collection of Sass mixins to cover all 3 flexbox specifications that have been implemented. More information can be found here: https://gist.github.com/cimmanon/727c9d558b374d27c5b6

  • Download Gist
flexbox.scss
SCSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
@import "compass/css3/shared";
 
// NOTE:
// All mixins for the 2009 spec have been written assuming they'll be fed property values that
// correspond to the standard spec. Some mixins can be fed values from the 2009 spec, but don't
// rely on it. The `legacy-order` mixin will increment the value fed to it because the 2009
// `box-ordinal-group` property begins indexing at 1, while the modern `order` property begins
// indexing at 0.
 
// if `true`, the 2009 properties will be emitted as part of the normal mixin call
// (if this is false, you're still free to explicitly call the legacy mixins yourself)
$flex-legacy-enabled: false !default;
 
// if `true`, `$flex-legacy-enabled` is treated as false and an `@supports` block is added to
// the display-flex mixin to hide the standard value from versions of Firefox that support the
// unprefixed properties, but do not support wrapping
// (this includes suppressing the automatic emittion of 2009 properties)
$flex-wrap-required : false !default;
 
// September 2012 Candidate Recommendation (http://www.w3.org/TR/2012/CR-css3-flexbox-20120918/)
// NOTE: FF did not support wrapping until version ??. Because the `display: flex` property
// is wrapped in a `@supports (flex-wrap: wrap)` block (when $flex-wrap-required or the $wrap
// argument to the `display-flex` mixin is set to `true`), it will Just Work(TM) when support is
// finally added
// Chrome 21 (prefixed)
// Opera 12.1 (unprefixed)
// Firefox 20 (unprefixed)
$flex-webkit : true !default;
$flex-ms : false !default;
$flex-official: true !default;
 
// March 2012 Working Draft (http://www.w3.org/TR/2012/WD-css3-flexbox-20120322/)
// Chrome 17 (prefixed)
// Internet Explorer 10 (prefixed)
$flex-2012-webkit: true !default;
$flex-2012-ms : true !default;
 
// July 2009 Working Draft (http://www.w3.org/TR/2009/WD-css3-flexbox-20090723/)
// NOTE: no browser that implements this spec is known to support `box-lines: multiple`
// Chrome 4? (prefixed)
// Safari 3.1 (prefixed)
// Firefox <20 (prefixed)
$flex-2009-webkit: true !default;
$flex-2009-moz : true !default;
 
// ====================================================================================================
// | Common
// ====================================================================================================
 
// function for converting a value from the standard specification to one that is comparable from
// an older specification
@function flex-translate-value($value, $version: 2009) {
$value: unquote($value);
 
@if $value == flex {
@return if($version == 2009, box, flexbox);
} @else if $value == inline-flex {
@return if($version == 2009, inline-box, inline-flexbox);
} @else if $value == flex-start {
@return start;
} @else if $value == flex-end {
@return end;
} @else if $value == space-between {
@return justify;
} @else if $value == space-around { // 2009 doesn't have a value equivalent to `space-around`
@return if($version == 2009, justify, distribute);
}
@return $value;
}
 
@mixin flex-standard($property, $value) {
$value: unquote($value);
@include experimental($property, $value,
not -moz,
$flex-webkit,
not -o,
$flex-ms,
not -khtml,
$flex-official);
}
 
@mixin flex-2012($property, $value) {
$value: unquote($value);
@include experimental($property, $value,
not -moz,
$flex-2012-webkit,
not -o,
$flex-2012-ms,
not -khtml,
not -official);
}
 
@mixin flex-2009($property, $value) {
$value: unquote($value);
@include experimental($property, $value,
$flex-2009-moz,
$flex-2009-webkit,
not -o,
not -ms,
not -khtml,
not -official);
}
 
// mixin to use if standard and 2012 have the same property names
@mixin flex-modern($property, $value) {
$value: unquote($value);
@include experimental($property, $value,
not -moz,
$flex-2012-webkit or $flex-webkit,
not -o,
$flex-2012-ms or $flex-ms,
not -khtml,
$flex-official);
}
 
// ====================================================================================================
// | Display Property
// ====================================================================================================
 
// $type: flex | inline-flex
@mixin display-flex($type: flex, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-display-flex($type);
}
@include experimental-value(display, flex-translate-value($type, 2012),
not -moz,
$flex-2012-webkit,
not -o,
$flex-2012-ms,
not -khtml,
not -standard);
@include experimental-value(display, $type,
not -moz,
$flex-webkit,
not -o,
$flex-ms,
not -khtml,
$flex-official and not $wrap);
@if $flex-official and $wrap {
@supports (flex-wrap: wrap) {
display: $type;
}
}
}
 
@mixin legacy-display-flex($type: flex) {
@include experimental-value(display, flex-translate-value($type, 2009),
$flex-2009-moz,
$flex-2009-webkit,
not -o,
not -ms,
not -khtml,
not standard);
}
 
// ====================================================================================================
// | Flex Container Properties
// ====================================================================================================
 
// $value: <'flex-direction'> || <'flex-wrap'>
@mixin flex-flow($value: row nowrap, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-flex-flow($value);
}
@include flex-modern(flex-flow, $value);
}
 
@mixin legacy-flex-flow($value: row nowrap) {
@if length($value) > 1 { // 2009 version doesn't have a shorthand
@include legacy-flex-direction(nth($value, 1));
@include legacy-flex-wrap(nth($value, 2));
} @else {
$value: unquote($value);
@if $value == row or $value == row-reverse or $value == column or $value == column-reverse {
@include legacy-flex-direction($value);
} @else {
@include legacy-flex-wrap($value);
}
}
}
 
// ----------------------------------------------------------------------
 
// $value: row | row-reverse | column | column-reverse
@mixin flex-direction($value: row, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-flex-direction($value);
}
@include flex-modern(flex-direction, $value);
}
 
@mixin legacy-flex-direction($value: row) {
$value: unquote($value);
@include flex-2009(box-orient, if($value == row or $value == row-reverse, horizontal, vertical));
@include flex-2009(box-direction, if($value == row-reverse or $value == column-reverse, reverse, normal));
}
 
// ----------------------------------------------------------------------
 
// $value: nowrap | wrap | wrap-reverse
@mixin flex-wrap($value: nowrap, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-flex-wrap($value);
}
@include flex-modern(flex-wrap, $value);
}
 
@mixin legacy-flex-wrap($value: nowrap) {
// NOTE: 2009 has no equivalent of wrap-reverse
@include flex-2009(box-lines, if($value == nowrap, single, multiple));
}
 
// ----------------------------------------------------------------------
 
// Distributing extra space along the "main axis"
// $value: flex-start | flex-end | center | space-between | space-around
@mixin justify-content($value: flex-start, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-justify-content($value);
}
@include flex-2012(flex-pack, flex-translate-value($value, 2012));
@include flex-standard(justify-content, $value);
}
 
@mixin legacy-justify-content($value: flex-start) {
@include flex-2009(box-pack, flex-translate-value($value, 2009));
}
 
// ----------------------------------------------------------------------
 
// Distributing extra space along the "cross axis"
// $value: flex-start | flex-end | center | space-between | space-around | stretch
@mixin align-content($value: flex-start, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-align-content($value);
}
@include flex-2012(flex-line-pack, flex-translate-value($value, 2012));
@include flex-standard(align-content, $value);
}
 
@mixin legacy-align-content($value: flex-start) {
@include flex-2009(box-align, flex-translate-value($value, 2009));
}
 
// ----------------------------------------------------------------------
 
// Align items along the "cross axis"
// $value: flex-start | flex-end | center | baseline | stretch
@mixin align-items($value: stretch) { // the flex container
// There is no 2009 version of this property
@include flex-2012(flex-align, flex-translate-value($value, 2012));
@include flex-standard(align-items, $value);
}
 
// ====================================================================================================
// | Flex Item Properties
// ====================================================================================================
 
// $value: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
@mixin flex($value: 0 1 auto, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
$value: unquote($value);
@if $legacy and unitless(nth($value, 1)) {
// 2009 version does not have a shorthand, see `legacy-flex-grow`
@include legacy-flex-grow(nth($value, 1));
}
@include flex-modern(flex, $value);
}
 
// ----------------------------------------------------------------------
 
// $value: Integer
@mixin flex-grow($value: 0, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-flex-grow($value);
}
// There is no 2012 version of this property
@include flex-standard(flex-grow, $value);
}
 
@mixin legacy-flex-grow($value: 0) {
@include flex-2009(box-flex, $value);
}
 
// ----------------------------------------------------------------------
 
// $value: Integer
@mixin flex-shrink($value: 1) {
// There is no 2009 version of this property
// There is no 2012 version of this property
@include flex-standard(flex-shrink, $value);
}
 
// ----------------------------------------------------------------------
 
// $value: united number (eg: 100px)
@mixin flex-basis($value: auto) {
// There is no 2009 version of this property
// There is no 2012 version of this property
@include flex-standard(flex-basis, $value);
}
 
// ----------------------------------------------------------------------
 
// Align items along the "cross axis" -- overrides `align-items` value on individual items
// $value: auto | flex-start | flex-end | center | baseline | stretch
@mixin align-self($value: auto) { // children of flex containers
// There is no 2009 version of this property
@include flex-2012(flex-item-align, flex-translate-value($value, 2012));
@include flex-standard(align-self, $value);
}
 
// ----------------------------------------------------------------------
 
// $value: Integer
@mixin order($value: 0, $wrap: $flex-wrap-required, $legacy: $flex-legacy-enabled) {
@if $legacy and not $wrap {
@include legacy-order($value);
}
@include flex-2012(flex-order, $value);
@include flex-standard(order, $value);
}
 
@mixin legacy-order($value: 0) {
// the 2009 spec starts the ordering at 1 instead of 0 like the modern specs
@include flex-2009(box-ordinal-group, $value + 1);
}

Can you add an ".scss" extension to the file name? It will turn on syntax highlighting, and make this considerably easier to read.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.