• Stars
    star
    343
  • Rank 123,371 (Top 3 %)
  • Language SCSS
  • Created over 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Slice media queries with ease

an egg slicer

Breakpoint Slicer npm GitHub Workflow Status

Slice media queries with ease

Breakpoint Slicer is a set of Sass mixins that makes working with mediaqueries effortless and fun.

@include at(s)          // => @media (min-width: 400px) and (max-width: 599px)
@include from(s)        // => @media (min-width: 400px)
@include to(s)          // => @media                        (max-width: 599px)
@include between(xs, l) // => @media (min-width: 200px) and (max-width: 799px)

See below how to customize breakpoints!

Status

Breakpoint Slicer v.3 is a complete rewrite.

Breakpoint Slicer had been evolving, improving its ergonomics while maintaining compatibility with old versions and supporting now obsolete practices. As a result, it ended up being a bit messy.

It's time to shed the legacy and redesign Breakpoint Slicer with simplicity and leveraging new Sass features such as modules and maps.

See CHANGELOG.md for the list of breaking changes (there are many! ๐Ÿ™ˆ).

Concept

Most media query heleprs operate with breakpoints. Here they are on a scale:

 Breakpoint:   0       200px     400px     600px     800px     1000px    1200px    1400px       
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>

Breakpoint Slicer operates with slices instead of breakpoints. A slice is a range of viewport sizes between two consequetive breakpoints.

 Breakpoint:   0       200px     400px     600px     800px     1000px    1200px    1400px       
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
 Slice:            xxs        xs        s         m         l         xl       xxl       xxxl   

Slices make it much easier to reason about media queries. Take any viewport width, and you can say which slice it corresponds to.

Given the slices defined above, screen width of 1024px falls into the xl slice, and screen width of 1920px falls into the xxxl slice.

The goal of Breakpoint Slicer is to let you quickly apply media queries using slice names instead of px breakpoints.

Enter the mixins

Breakpoint Slicer offers four handy mixins that let you set breakpoint ranges easily: at, from, to and between:

Styles under at(m) are applied when browser window width is inside the m slice.

Styles under from(m) are applied when browser window width is inside the m slice or larger.

Styles under to(m) are applied when browser window width is inside the m slice or smaller.

Styles under between(s, l) are applied when browser window width is inside the s slice, l slice (inclusive) and any slices in between, if any.

 Breakpoint:   0       200px     400px     600px     800px     1000px    1200px    1400px       
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
 Slice:        ยท   xxs        xs   ยท    s    ยท    m    ยท    l    ยท    xl       xxl       xxxl   
               ยท                   ยท         ยท         ยท         ยท                              
               ยท                   ยท         ยท  at(m)  ยท         ยท                              
               ยท                   ยท         โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”คยท         ยท                              
               ยท                   ยท         ยท         ยท         ยท                              
               ยท                   ยท         ยท from(m) ยท         ยท                              
               ยท                   ยท         โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
               ยท                   ยท                   ยท         ยท                              
               ยท                   ยท            to(m)  ยท         ยท                              
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”คยท         ยท                              
                                   ยท                             ยท                              
                                   ยท         between(s, l)       ยท                              
                                   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”คยท                              
Mixin incovation Resulting media query
at(m) (min-width: 600px) and (max-width: 799px)
from(m) (min-width: 600px)
to(m) (max-width: 799px)
between(s, l) (min-width: 400px) and (max-width: 999px)

Note that each slice includes its left breakpoint but does not include its right breakpoint.

Edge cases

Note that the last slice does not have a right edge. When it is invoked, the media query will have no max-width value.

Some mixin invocations are synonomous:

 Breakpoint:   0       200px     400px     600px     800px     1000px    1200px    1400px       
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
 Slice:        ยท   xxs   ยท    xs        s         m    ยท    l         xl       xxl   ยท   xxxl   
               ยท         ยท                             ยท                             ยท          
               ยท at(xxs) ยท                             ยท                             ยท  at(xxxl)
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”คยท                             ยท                             โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
               ยท         ยท                             ยท                             ยท          
               ยท to(xxs) ยท                             ยท                             ยท from(xxxl)
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”คยท                             ยท                             โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
               ยท                                       ยท                                        
               ยท                               to(m)   ยท   from(l)                              
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”คโ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
               ยท                                       ยท                                        
               ยท           between(xxs, m)             ยท           between(l, xxxl)             
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”คโ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>

โ€ฆand some become meaningless and will produce an error, preventing you from accidentally covering all viewports with a useless media query:

 Breakpoint:   0       200px     400px     600px     800px     1000px    1200px    1400px       
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
 Slice:        ยท   xxs        xs        s         m         l         xl       xxl       xxxl   
               ยท                                                                                
               ยท  from(xxs)                                                                     
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>
               ยท                                                                                
               ยท                                                                      to(xxxl)  
               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€>

Installation

npm

npm install -D breakpoint-slicer

Yarn

yarn add -D breakpoint-slicer

Usage with default slices

With Sass modules and @use, recommended

@use "node_modules/breakpoint-slicer" as bs;

.foo {
  @include bp.at(s) {
    color: red;
  }
}

With @import

Use this only if your Sass version does not support Sass modules.

Import breakpoint-slicer early in your stylesheet:

@import 'breakpoint-slicer';

This will expose mixins into the global scope:

.foo {
  @include at(s) {
    color: red;
  }
}

Usage with custom slices

Breakpoint Slicer lets you define your own set of slices.

Basics

Slices are defined as a map like this:

$slices: (
  small: 0px,
  medium: 400px,
  large: 800px,
);

Keys are slice names and values are left breakpoints of each slice.

โš  One slice must have a left breakpoint of 0px.

It is recommended that you list slices in the incremental order.

You can pass a slices map to any of the mixins, ad-hoc style:

.foo {
  @include bp.at(small, $slices: $slices) {
    color: red;
  }
}

This approach is only useful if you have to deal with more than one set of slices per Sass module/partial.

With Sass modules and @use, recommended

This is how you can override default slices at the top of your Sass module:

// config.scss

$slices: (
  small: 0px,
  medium: 400px,
  large: 800px,
);
// my-module.scss

@use "config";
@use "node_modules/breakpoint-slicer" as bs with ($slices: config.$slices);

.foo {
  @include bp.at(small) {
    color: red;
  }
}

With @import

โš  Use this only if your Sass version does not support Sass modules.

Define $slices globally next to your breakpoint-slicer invocation:

@import 'breakpoint-slicer';

$slices: (
  small: 0px,
  medium: 400px,
  large: 800px,
);

This will make mixins use your slices definition globally, unless overridded with an argumetn:

```scss
.foo {
  @include at(medium) {
    color: red;
  }
}

.bar {
  @include at(tablet-landscape, $slices: $other-slices) {
    color: blue;
  }
}

Prepending media queries with custom media types or features

Use the $media variable in the same manner as you use $slices, as explained above.

๐Ÿ’ก Here only the ad-hoc variant is demonstrated, but @use and global invocations are also possible.

Providing one media/feature group

If you need something simple like screen, you can do $media: screen.

If you need to use spaces, then wrap it in parens like this: $media: (screen and (orientation: portrait)).

.foo {
  @include at(s, $media: (screen and (orientation: portrait))) {
    color: red;
  }
}

produces:

@media screen and (orientation: portrait)) and (min-width: 400px) and (max-width: 599px) {
  .foo {
    color: red;
  }
}

Providing multiple media/feature groups

You can pass media types/features split with commas, e. g $media: (screen, print):

.foo {
  @include at(s, $media: (screen, print)) {
    color: red;
  }
}

The generated media query will be duplicated for each type/feature in the list:

@media screen and (min-width: 400px) and (max-width: 599px), print and (min-width: 400px) and (max-width: 599px) {
  .foo {
    color: red;
  }
}

Credit

Built by Andrey Mikhaylov (@lolmaus) and contributors.

License

MIT.

More Repositories

1

ember-drag-sort

A sortable list component with support for multiple and nested lists
JavaScript
89
star
2

ember-element-query

Element queries for Ember
TypeScript
39
star
3

jquery.dragbetter

A no-bullshit solution for `dragenter` and `dragleave` events that behave like `mouseenter` and `mouseleave`, not `mousein` and `mouseout`.
JavaScript
36
star
4

ember-cli-stained-by-children

Ember CLI addon: a mixin that makes the `isDirty` property of a record respect the dirtiness of its belongsTo/hasMany children
JavaScript
25
star
5

ember-cli-custom-assertions-collection

An ever growing library of assertions for QUnit in Ember
JavaScript
25
star
6

jquery.timer-tools

Handy decorators for delaying, debouncing and throttling callbacks.
CoffeeScript
14
star
7

ember-svg-pie

A very lightweight, pure SVG pie chart
JavaScript
12
star
8

ember-dialogs

Customizable implementation of `alert`, `confirm` and `prompt` modal dialogs with straightforward API
JavaScript
11
star
9

ember-app-state-slider

Immutable state management in Ember (proof of concept)
JavaScript
9
star
10

ember-stamplay

First-class support for the Stamplay.com BaaS (WIP)
JavaScript
7
star
11

jquery.closestchild

An opposite of `closest()`: find the nearest child that matches a selector, ignore deeper matching children
JavaScript
6
star
12

derbyjs-talk-ru

A placeholder for having Derby-related discussions in Github Issues (Russian). ะœะตัั‚ะพ ะดะปั ะฒะตะดะตะฝะธั ะดะธัะบัƒััะธะน ะฒะพะบั€ัƒะณ Derby ะฒ Github Issues ะฝะฐ ั€ัƒััะบะพะผ.
6
star
13

yuidork

View YUIDoc docs for any project and branch/tag/commit fresh from GitHub
JavaScript
6
star
14

sassbin

An HTML/SASS playground with support for Compass extensions
JavaScript
5
star
15

ember-bdd

A Cucumber-style BDD testing solution for Ember
TypeScript
3
star
16

lolma.us

My personal website, built with Ember
JavaScript
3
star
17

url-decode-filenames

Ruby script to decode filenames. Intended to be used for decrypting DokuWiki's cyrillic filenames.
Ruby
2
star
18

singularity-quick-spanner

A collection of handy shortcut mixins for the Singularity grid system
CSS
2
star
19

sharing-variables-between-sass-and-js

A proof of concept for having a single list of breakpoints shared between SASS and JS
Ruby
2
star
20

assertron

Run assertions against your objects. Define contracts with a handy DSL. Test your code inline.
JavaScript
2
star
21

covisual

No-bullshit coronavirus epicdemic severity chart
CSS
2
star
22

ember-zen

JavaScript
2
star
23

test01

PHP
1
star
24

test3

1
star
25

drupal-platform

My humble Drupal 7 build that i use to host websites
1
star
26

branch

Run blocks asynchronously with little effort, a Ruby equivalent for JS setTimeout
Ruby
1
star
27

food_finder2

Ruby
1
star
28

intellij-idea-bug-demonstration-WEB-10845

http://youtrack.jetbrains.com/issue/WEB-10845
Ruby
1
star
29

ember-drag-sort-old

JavaScript
1
star
30

test2

1
star
31

test123

KILL Me
1
star
32

food_finder

A tutorial project
Ruby
1
star
33

test

1
star
34

depot_agilewebdevrails3

A tutorial app being created while following the
Ruby
1
star
35

intercom-test-assignment

A test assignment I did for Intercom.io
JavaScript
1
star
36

taskaparilla-stamplay-client

Client for the Taskaparilla app backed by Stamplay.com
JavaScript
1
star
37

selectel-s3ql-backup

A bash script to quickly backup stuff to Selectel.ru cloud storage via S3QL
Shell
1
star
38

districomp

A distibuted computing solution written for the bare purpose of studying Ruby.
Ruby
1
star
39

codepage1251

A Windows XP .reg file that changes character table of cp1252 to cp1251. Useful for cyrillic users.
1
star
40

dir_size_renamer

This console utility lets you rename subdirectories within a specified directory, so that subdirectories' names contain their sizes.
Ruby
1
star
41

lsizerenamer

Renames first-level subofolders of a given folder to contain their sizes
Ruby
1
star