Sunday, 30 August 2015

How to use CSS flex box in SASS

Add flex mixins to meet the browser compatibilities

@mixin flexbox() {
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flex;
  display: flex;
}

@mixin flex($values) {
  -webkit-flex:  $values;
  -moz-flex:  $values;
  -ms-flex:  $values;
  flex:  $values;
}

@mixin flexflow($values){
  -webkit-flex-flow: $values; 
  -moz-flex-flow: $values;    
  -ms-flex-flow: $values;    
  flex-flow: $values;
}

@mixin order($val) {
  -webkit-order: $val;
  -moz-order: $val;    
  -ms-flex-order: $val;    
  order: $val;
}

@mixin alignitems($val){
    -webkit-align-items: $val;
    align-items: $val;
}
Note: these cover most of the frequently used flex css styles, but not all of them

A sample for displaying the images and making the images aligned centered which might not be able to achieve via div or table

.flex-box{
    @include flexbox();
    @include flexflow(row wrap);/* the items will fall down to the next line when the current line is full */
    @include alignitems(centre);/* the items will be aligned centred */
}

.flex-item{
    @include flex(1 15%); /*flex-grow flex-basis*/
}

No comments:

Post a Comment