SCSS Utilites
Mw-grid SCSS API provides follow functions, which you can use to get some grid parameters. These functions get data from $devices obj.
Mixins
Mw-grid SCSS API provides follow mixins for write @media() expressions
-
media-up()- output @media expression (min-width) -
media-down()- output @media expression (max-width) -
media-only()- output @media expression (min-width) and (max-width)
media-up()
output @media expression up from breakpoint (min-width)
SCSS source
/* 1. media-up($bp_name) - output @media expression up from breakpoint (min-width) */
//
// @param {String} $bp_name - breakpoint name
// @return {CSS expression} - @media expression
@mixin media-up($bp_name) {
@media (min-width: get_grid_bp($bp_name)+px) {
@content;
}
}
.my-test-block {
@include media-up(ds_) {
width: 50%;
background-color: #999;
};
}
CSS compiled
@media (min-width: 1024px) {
.my-test-block {
width: 50%;
background-color: #999;
}
}
media-down()
output @media expression down from breakpoint (max-width)
SCSS source
/* 2. media-down($bp_name) - output @media expression down from breakpoint (max-width) */
//
// @param {String} $bp_name - breakpoint name
// @return {CSS expression} - @media expression
@mixin media-down($bp_name) {
@media (max-width: get_grid_bp($bp_name)-1px) {
@content;
}
}
.my-test-block {
@include media-down(tb_) {
width: 100%;
background-color: dakrblue;
};
}
CSS compiled
@media (max-width: 767px) {
.my-test-block {
width: 100%;
background-color: dakrblue;
}
}
media-only()
output @media expression up from breakpoint to the next breakpoint (min-width) and (max-width)
SCSS source
/* 3. media-only($bp_name) - output @media expression up from breakpoint to the next breakpoint (min-width) and (max-width) */
//
// @param {String} $bp_name - breakpoint name
// @return {CSS expression} - @media expression
@mixin media-only($bp_name) {
$bp_index: index(map-keys($devices), $bp_name); //+
@if ( $bp_index >=length($devices-names)) {
@media (min-width: get_grid_bp($bp_name)+px) {
@content;
}
}
@else {
$next_bp_name: nth($devices-names, $bp_index+1);
@media (min-width: get_grid_bp($bp_name)+px) and (max-width: get_grid_bp($next_bp_name)-1px) {
@content;
}
}
}
.my-test-block {
@include media-only(tb_) {
background-color: lightgreen;
};
}
CSS compiled
@media (min-width: 768px) and (max-width: 1023px) {
.my-test-block {
background-color: lightgreen;
}
}
Functions
Mw-grid SCSS API provides follow functions, which you can use to get some grid parameters. These functions get data from $devices obj.
get_grid_bp() - get device breakpoint value
get_grid_c_m() - get device container_max value
get_grid_f_b() - get device font_base value
get_grid_ms_c() - get device ms_coeff (modular scale) coefficient value
get_grid_property() - gets the specified (custom) property value
get_grid_bp()
SCSS source
// Get grid breakpoint value from $devices
// @param {String} $bp_name - breakpoint name
// @return {Number} - breakpoint value (unitless)
@function get_grid_bp($bp_name) {
$bp_obj: map-get($devices, $bp_name);
$bp_value: map-get($bp_obj, breakpoint);
@return $bp_value;
}
.my-block {
@media (min-width: get_grid_bp('ds_')+px ) {
margin-top: 20px; // some code..
}
}
CSS compiled
@media (min-width: 1024px) {
.my-block {
margin-top: 20px;
}
}
get_grid_c_m()
SCSS source
// Get grid device container_max value
// @param $bp_name {String} $number - breakpoint name
// @return {Number} - container_max value
@function get_grid_c_m($bp_name) {
$bp_obj: map-get($devices, $bp_name);
$bp_value: map-get($bp_obj, container_max);
@return $bp_value;
}
.my-block {
max-width: 1/2 * get_grid_c_m('hd_')+px; // 1/2 * 600
}
CSS compiled
.my-block {
max-width: 600px;
}
get_grid_f_b()
SCSS source
// Get grid device font_base value
// @param $bp_name {String} $number - breakpoint name
// @return {Number} - font_base value
@function get_grid_f_b($bp_name) {
$bp_obj: map-get($devices, $bp_name);
$bp_value: map-get($bp_obj, font_base);
@return $bp_value;
}
.my-block {
max-width: 1/2 * get_grid_c_m('hd_')+px; // 1/2 * 600
}
CSS compiled
.my-block {
font-size: 15px;
}
get_grid_ms_c()
SCSS source
// Get grid device ms_coeff value
// @param $bp_name {String} $number - breakpoint name
// @return {Number} - ms_coeff value (unitless)
@function get_grid_ms_c($bp_name) {
$bp_obj: map-get($devices, $bp_name);
$bp_value: map-get($bp_obj, ms_coeff);
@return $bp_value;
}
h6 {
font-size: get_grid_f_b('mv_') * get_grid_ms_c('mv_'); // 15 * 1.10
}
CSS compiled
h6 {
font-size: 16.5px;
}
get_grid_property()
gets the specified (custom) property value from the grid parameters. You can add your custom parameters to grid and use it in your project files
SCSS source
// Get the specified (custom) property value
// @param $bp_name {String} - breakpoint name
// @param $propery {String} - breakpoint name
// @return {Number} - property value
@function get_grid_property($bp_name, $propery) {
$bp_obj: map-get($devices, $bp_name);
$propery_value: map-get($bp_obj, $propery);
@return $propery_value;
}
body {
line-height: get_grid_property('mv_', 'line_height');
@media (min-width: get_grid_bp('ds_')+px ) {
line-height: get_grid_property('ds_', 'line_height')
}
}
SCSS source
/* _mygrid.scss */
$devices: (
mv_: (
breakpoint: 320,
container_max: 300,
font_base: 15px,
ms_coeff: 1.10,
line_height: 1.5 // add your custom property
),
tb_: (
breakpoint: 768,
container_max: 750,
font_base: 15px,
ms_coeff: 1.17,
),
ds_: (
breakpoint: 1024,
container_max: 960,
font_base: 16px,
ms_coeff: 1.17,
line_height: 1.7 // add your custom property
)
);
// Import module
@import 'node_modules/mw-grid/src/_mwgrid.scss';
CSS compiled
body {
line-height: 1.5;
}
@media (min-width: 1024px) {
body {
line-height: 1.7;
}
}
