SL Home
Stein Lundbeck Tech blog
How to use function and mixin in Sass
When working with Sass both function and mixin is a handy feature that allows for advanced functionality.
This article just covers the basics, it's recommended to read the official documentation found under Links.
This article just covers the basics, it's recommended to read the official documentation found under Links.
Content |
||
1 | Media mixin | |
2 | How to use media mixin | |
Links |
Media mixin
This article explains how to create a reusable component that handles all device sizes and resizing.
First we create the _Media.scss file that will be referenced in all files that need media resizing.
First we create the _Media.scss file that will be referenced in all files that need media resizing.
Breakpoints
Name | Device | Size |
XS | Phone | 0px |
SM | Tablet | 576px |
MD | Laptop | 768px |
LG | Computer | 992px |
XL | Screen | 1200px |
XXL | BigScreen | 1920px |
The file consist of two map's containing the breakpoint sizes, orientation and a mixin that can be referenced from importing the file.
MD is the most common breakpoint and is the default size for the mixin.
MD is the most common breakpoint and is the default size for the mixin.
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 |
|
$mediaBreakpoints: ( XS: 0px, SM: 576px, MD: 768px, LG: 992px, XL: 1200px, XXL: 1920px );
$mediaDevices: ( Phone: XS, Tablet: SM, Laptop: MD, Computer: LG, Screen: XL, BigScreen: XXL );
$mediaOrientation: ( 0: Any, 1: Portrait, 2: Landscape );
@function mediaStr($size, $orientation, $mode, $minWidth, $maxWidth) {
$mode: to-upper-case(inspect($mode));
$size: to-upper-case(inspect($size));
$width: map-get($mediaBreakpoints, $size);
$query: null;
@if ($minWidth) {
$width: $minWidth;
}
@if ($mode == UP) {
$query: "(min-width: " + $width + ")";
}
@else if ($mode == DOWN) {
$query: "(max-width: " + $width + ")";
}
@else if ($mode == ONLY) {
$query: "(min-width: " + $width + ")";
$max: null;
@if ($maxWidth) {
$max: $maxWidth;
}
@else {
@if ($size == XS) {
$max: map-get($mediaBreakpoints, SM);
}
@else if ($size == SM) {
$max: map-get($mediaBreakpoints, MD);
}
@else if ($size == MD) {
$max: map-get($mediaBreakpoints, LG);
}
@else if ($size == LG) {
$max: map-get($mediaBreakpoints, XL);
}
}
@if ($max) {
$query: $query + " and (max-width: " + $max + ")";
}
}
@if not ($orientation == Any) {
$query: $query + " and (orientation: " + to-lower-case(inspect($orientation)) + ")";
}
@return $query;
}
@mixin media($size: MD, $orientation: Any, $mode: Up, $minWidth: null, $maxWidth: null) {
@media #{mediaStr($size, $orientation, $mode, $minWidth, $maxWidth)} {
@content;
}
}
@mixin device($device: Laptop, $orientation: Any, $mode: Up, $minWidth: null, $maxWidth: null) {
$size: map-get($mediaDevices, $device);
@media #{mediaStr($size, $orientation, $mode, $minWidth, $maxWidth)} {
@content;
}
}
How to use media mixin
When using the media mixin you import the _Media.scss file and use the media mixin in any Sass file.
First _Media.scss is imported as "Media" which exposes the media mixin.
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
@import "Media";
#Demo {
font-size: 10px;
font-weight: bold;
&:hover {
cursor: pointer;
}
}
@include media() {
#Demo {
font-size: 19px;
}
}
@include device() {
#Demo {
background-color: yellow;
}
}
Generated CSS
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
#Demo:hover {
cursor: pointer;
}
@media (min-width: 768px) {
#Demo {
font-size: 19px;
}
}
@media (min-width: 768px) {
#Demo {
background-color: yellow;
}
}