Little Miss Robot - Sass Breakpoints
This repository contains the logic and functionality to define and insert
media queries or breakpoints through the use of a configuration object within
SASS. It allows to make use of pre-defined mixins and functions that are only
breakpoint related.
Info
This repository does not generate any CSS. It simply makes functions and mixins
available to be used in combination with a configuration object to manage your
breakpoints. This only works when writing CSS in SASS and is specifically made
for that language.
Install
$ npm install --save-dev @littlemissrobot/sass-breakpoints
Usage
- Create a .scss file
- Import the library from the node_modules folder.
@import "~@littlemissrobot/sass-breakpoints/lib/main";
- Create a map-variable $lmr-sass-breakpoints and pass it a map where each
key is the name of the viewport, the value of that name is a map where the width
is defined. This is a map because this allows us to expand on the breakpoints
variable with other libraries that work well together. The unit can either be
pixels or rem. We prefer to work with rem. Therefore, in our setup 1rem is
equal to 10px to keep it readable.
$lmr-sass-breakpoints: (
viewport-0: (
width: 0,
),
viewport-3: (
width: 36rem
),
viewport-4: (
width: 48rem
),
viewport-7: (
width: 72rem
),
viewport-9: (
width: 99.2rem
),
viewport-12: (
width: 120rem
)
);
Mixins
respond-at($min-width)
Mixin that applies a media-query to a certain element. This media query can
either be placed inside a selector or can wrap around one or multiple selectors.
This mixin applies styling to the selector when the viewport is above the passed
width. The styling is applied until that breakpoint is reached.
Parameters:
$lmr-sass-breakpoints: (
viewport-7: (
width: 72rem
)
);
div {
@include respond-at(720px) {
margin: 3rem;
}
@include respond-at(72rem) {
margin: 3rem;
}
@include respond-at("viewport-7") {
margin: 3rem;
}
}
@include respond-at(720px) {
div {
margin: 3rem;
}
}
@include respond-at(72rem) {
div {
margin: 3rem;
}
}
@include respond-at("viewport-7") {
div {
margin: 3rem;
}
}
respond-to($max-width)
Mixin that applies a media-query to a certain element. This media query can
either be placed inside a selector or can wrap around one or multiple selectors.
This mixin applies styling to the selector when the viewport is below the passed
width. The styling is applied until that breakpoint is reached.
Parameters:
$lmr-sass-breakpoints: (
viewport-7: (
width: 72rem
)
);
div {
@include respond-to(720px) {
margin: 3rem;
}
@include respond-to(72rem) {
margin: 3rem;
}
@include respond-to("viewport-7") {
margin: 3rem;
}
}
@include respond-to(720px) {
div {
margin: 3rem;
}
}
@include respond-to(72rem) {
div {
margin: 3rem;
}
}
@include respond-to("viewport-7") {
div {
margin: 3rem;
}
}
respond-between($min-width, $max-width)
Mixin that applies a media-query to a certain element. This media query can
either be placed inside a selector or can wrap around one or multiple selectors.
This mixin applies styling to the selector when the viewport is between the
passed widths.
Parameters:
- min-width: the minimum width, when below this width, the styling is not
applied.
- max-width: the maximum width, when above this width, the styling is not
applied.
These parameters can both either be:
* A number in pixels.
* A number in rem. This value is converted automagically to pixels.
* A string, which is the name of the breakpoint defined in
$lmr-sass-breakpoints. This must have a width key. This key can either be
in pixels or in rem.
$lmr-sass-breakpoints: (
viewport-7: (
width: 72rem
),
viewport-9: (
width: 96rem
)
);
div {
@include respond-between(720px, 960px) {
margin: 3rem;
}
@include respond-between(72rem, 96rem) {
margin: 3rem;
}
@include respond-between("viewport-7", "viewport-9") {
margin: 3rem;
}
@include respond-between(720px, 96rem) {
margin: 3rem;
}
@include respond-between("viewport-7", 960px) {
margin: 3rem;
}
@include respond-between(72rem, "viewport-9") {
margin: 3rem;
}
}
@include respond-between(720px, 960px) {
div {
margin: 3rem;
}
}
@include respond-between(72rem, 96rem) {
div {
margin: 3rem;
}
}
@include respond-between("viewport-7", "viewport-9") {
div {
margin: 3rem;
}
}
@include respond-between(720px, 96rem) {
div {
margin: 3rem;
}
}
@include respond-between("viewport-7", 960px) {
div {
margin: 3rem;
}
}
@include respond-between(72rem, "viewport-9") {
div {
margin: 3rem;
}
}
Functions
get-width($name)
Function that returns the value of the width key defined within the a breakpoint
in the $lmr-sass-breakpoints map. These can be used to retrieve the value and
make a calculation or can be used with other media queries.
Parameters:
- $name: the name of the breakpoint in the $lmr-sass-breakpoints map.
$lmr-sass-breakpoints: (
viewport-7: (
width: 72rem
),
viewport-9: (
width: 960px
)
);
get-width("viewport-7");
get-width("viewport-9");
div {
@include respond-at(get-width("viewport-9")) {
margin: 3rem;
}
@include respond-to(get-width("viewport-7")) {
margin: 3rem;
}
@include respond-between(get-width("viewport-7"), get-width("viewport-9")) {
margin: 3rem;
}
}