Skip to content

Commit 400c600

Browse files
committed
Adds media mixins
1 parent 336732c commit 400c600

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

docs/scss-mixins.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,46 @@ To use, import the main mixins file into your SCSS, e.g.:
3434
- **`$url`** Path to the font file, including the filename (without
3535
file extension).
3636

37+
### Media Mixins
38+
These mixins help to create conditional SCSS, responsive to the viewport size.
39+
40+
We consider five viewport sizes: extra-small (XS), small (SM), medium (MD),
41+
large (LG), and extra-large (XL). It is assumed that mobile devices have SM
42+
screen size; tablets have MD screens, and desktops have LG or XL screen size.
43+
The actual break-points between these sizes are defined by four variables with
44+
the following default values:
45+
- **`$screen-xs`** — 320px;
46+
- **`$screen-sm`** — 768px;
47+
- **`$screen-md`** — 1024px;
48+
- **`$screen-lg`** — 1280px.
49+
50+
Each of these variables set the maximal pixel size of the corresponding
51+
viewport; i.e. XS viewport may have any width under `$screen-xs` (inclusive);
52+
MD viewport may have a width from `$screen-sm` (exclusive) up to `$screen-md`
53+
(inclusive); XL viewport may have any width above `$screen-lg` (exclusive).
54+
55+
Based on these variables, we provide two sets of media mixins:
56+
- **`@mixin xs`**, **`@mixin sm`**, **`@mixin md`**, **`@mixin lg`**,
57+
**`@mixin xl`** — allow to apply styling to a single specified
58+
size of the viewport;
59+
- **`@mixin xs-to-sm`**, **`@mixin xs-to-md`**, **`@mixin xs-to-lg`**,
60+
**`@mixin sm-to-md`**, **`@mixin sm-to-lg`**, **`@mixin sm-to-xl`**,
61+
**`@mixin md-to-lg`**, **`@mixin md-to-xl`**, **`@mixin lg-to-xl`** —
62+
allow to apply styling for a range of viewport sizes, from the first mentioned
63+
in the mixin name to the last one, both inclusive.
64+
65+
The both kinds of these mixins you can use the same way:
66+
```scss
67+
// style.scss
68+
69+
.someClass {
70+
// General style
71+
background: green;
72+
73+
// The element will become red on the smallest screens (XS to SM), which means
74+
// any screen smaller than MD.
75+
@include xs-to-sm {
76+
background: red;
77+
}
78+
}
79+
```
File renamed without changes.

src/styles/_mixins/media.scss

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Mixins for different layout sizes: xs, sm, md, lg.
3+
* Breaking points are defined in _variables.scss
4+
* The range mixins A-to-B all means "for the sizes from A to B, both
5+
* inclusive", in particular it means that mixin A-to-lg is equivalent to
6+
* all sizes from A (inclusive) and larger.
7+
*
8+
* NOTE: For convenience, these mixins are sorted not alphabetically, but,
9+
* first, by increase of the first size; second, by increase of the second size.
10+
*/
11+
12+
/* Break points. */
13+
14+
$screen-xs: 320px !default;
15+
$screen-sm: 768px !default;
16+
$screen-md: 1024px !default;
17+
$screen-lg: 1280px !default;
18+
19+
/* XS */
20+
21+
@mixin xs {
22+
@media (max-width: #{$screen-xs}) {
23+
@content;
24+
}
25+
}
26+
27+
@mixin xs-to-sm {
28+
@media (max-width: #{$screen-sm}) {
29+
@content;
30+
}
31+
}
32+
33+
@mixin xs-to-md {
34+
@media (max-width: #{$screen-md}) {
35+
@content;
36+
}
37+
}
38+
39+
@mixin xs-to-lg {
40+
@media (max-width: #{$screen-lg}) {
41+
@content;
42+
}
43+
}
44+
45+
/* XS */
46+
47+
@mixin sm {
48+
@media (min-width: #{$screen-xs + 1px}) and (max-width: #{$screen-sm}) {
49+
@content;
50+
}
51+
}
52+
53+
@mixin sm-to-md {
54+
@media (min-width: #{$screen-xs + 1px}) and (max-width: #{$screen-md}) {
55+
@content;
56+
}
57+
}
58+
59+
@mixin sm-to-xl {
60+
@media (min-width: #{$screen-xs + 1px}) {
61+
@content;
62+
}
63+
}
64+
65+
/* MD */
66+
67+
@mixin md {
68+
@media (min-width: #{$screen-sm + 1px}) and (max-width: #{$screen-md}) {
69+
@content;
70+
}
71+
}
72+
73+
@mixin md-to-lg {
74+
@media (min-width: #{$screen-sm + 1px}) and (max-width: #{$screen-lg}) {
75+
@content;
76+
}
77+
}
78+
79+
@mixin md-to-xl {
80+
@media (min-width: #{$screen-sm + 1px}) {
81+
@content;
82+
}
83+
}
84+
85+
/* LG */
86+
87+
@mixin lg {
88+
@media (min-width: #{$screen-md + 1px}) and (max-width: #{$screen-lg}) {
89+
@content;
90+
}
91+
}
92+
93+
@mixin lg-to-xl {
94+
@media (min-width: #{$screen-md + 1px}) {
95+
@content;
96+
}
97+
}
98+
99+
/* XL */
100+
101+
@mixin xl {
102+
@media (min-width: #{$screen-lg + 1px}) {
103+
@content;
104+
}
105+
}

src/styles/mixins.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/* Collection of standard mixins, being used all around. */
22
@import "_mixins/fonts";
3+
@import "_mixins/media";

0 commit comments

Comments
 (0)