Skip to content
/ mq-sass Public

Sass library to manage responsive breakpoints and generate media queries

License

Notifications You must be signed in to change notification settings

jonsuh/mq-sass

Repository files navigation

mq-sass

mq-sass is a Sass library to help you manage your responsive breakpoints and easily generate media queries.

Requirements

  • Sass 3.3+
  • SCSS syntax

Installation

  1. mq-sass is available on npm, yarn and Bower.
npm install mq-sass

yarn get mq-sass

bower install css-mq-sass

Also available as a Ruby gem to use within your Rails application—see below for more information.

Or to manually install it, download and unzip the source files, then copy the files from the stylesheets/mq-sass directory into your project.

  1. Import the _mq-sass.scss file into your project.
@import "mq-sass";

If you're using gulp, Grunt, Compass, or alike, include mq-sass:

// gulp-sass gulpfile.js
.pipe(sass({
  includePaths: ['node_modules/mq-sass/stylesheets']
}))

// grunt-sass Gruntfile.js
options: {
  includePaths: ['node_modules/mq-sass/stylesheets']
},
# Compass config.rb
add_import_path "node_modules/mq-sass/stylesheets"

Install for Ruby on Rails

  1. Add mq-sass to your Gemfile.
gem 'mq-sass'
  1. Run bundle install.
  2. Include mq-sass by using Sass’s native @import*
// application.scss
@import "mq-sass";

* More information on why Sass’s native @import + why you should ditch Sprockets directives altogether.

Documentation

Settings

$mq-breakpoints: (
  iphone  : 320px,
  iphone6 : 375px,
  iphone6p: 414px,
  small   : 480px,
  medium  : 640px,
  ipad    : 768px,
  large   : 1024px,
);

$mq-ems    : false;
$mq-em-base: 16px;

$mq-only: "only screen";

mq($breakpoint, $minmax, $widthheight) Mixin

@include mq($breakpoint, $minmax, $widthheight) { // $minmax and $widthheight are optional
  // Sass goes here
}

$breakpoint

  1. The first parameter, $breakpoint, accepts pre-defined values from the $mq-breakpoints(); map, which is set by default as above in Settings.

Example:

@include mq(small) {
  color: white;
}
/* Resulting CSS */
@media only screen and (min-width: 480px) {
  color: white;
}

You can also customize your own breakpoints.

$mq-breakpoints: (
  s : 600px,
  m : 800px,
  l : 1000px,
  xl: 1200px,
);

@include mq(xl) {
  color: blue;
}
  1. $breakpoint also accepts other pre-defined values:
  • portrait
  • landscape
  • retina

Example:

@include mq(portrait) {
  color: white;
}

@include mq(retina) {
  color: red;
}
/* Resulting CSS */
@media only screen and (orientation: portrait) {
  color: white;
}

@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-resolution: 2dppx) {
  color: red;
}
  1. $breakpoint accepts custom values in px.

Example:

@include mq(700px) {
  color: white;
}
/* Resulting CSS */
@media only screen and (min-width: 700px) {
  color: white;
}

Note: You can also use unitless pixel values: @include mq(700) {}

$minmax

By default, media queries are mobile first (min-width).

$minmax accepts values min or max, which will result in min-width: or max-width: respectively. If left blank, it falls back to the default, min.

Example:

@include mq(ipad) {
  color: white;
}

@include mq(600px, max) {
  color: cyan;
}
/* Resulting CSS */
@media only screen and (min-width: 768px) {
  color: white;
}

@media only screen and (max-width: 600px) {
  color: cyan;
}

$widthheight

By default, media queries that are generated are (min/max-width).

$widthheight accepts values width or height, which results in min/max-width: or min/max-height: respectively. If left blank, it falls back to the default, width.

Example:

@include mq(small, min, height) {
  color: cyan;
}

@include mq(600px, max, height) {
  color: pink;
}
/* Resulting CSS */
@media only screen and (min-height: 480px) {
  color: cyan;
}

@media only screen and (max-height: 600px) {
  color: pink;
}

ems

To have media queries in ems, set $mq-ems: true;. The default em base is 16px. You can change it by setting $mq-em-base to the pixel value of your choosing.

Examples:

$mq-ems: true;

@include mq(600px) {
  color: white;
}
/* Resulting CSS */
@media only screen and (min-width: 37.5em) {
  color: white;
}
$mq-ems    : true;
$mq-em-base: 20px;

@include mq(600px) {
  color: cyan;
}
/* Resulting CSS */
@media only screen and (min-width: 30em) {
  color: cyan;
}

media

By default the media is specified for only screen. For some reason if you'd like to change it or remove it completely, you can do so by changing the setting $mq-media:

$mq-media: "screen";
/* Resulting CSS */
@media screen and (min-width...) {}
$mq-media: ""; // or false
/* Resulting CSS */
@media (min-width...) {}

mq-get($breakpoint, $ems) function

Returns the value of the breakpoint in pixels (by default) or ems.

.example {
  max-width: mq-get(small);
}

$breakpoint

$breakpoint accepts only pre-defined keys from the $mq-breakpoints(); map.

$mq-breakpoints: (
  small : 480px,
  medium: 640px,
);

.example {
  max-width: mq-get(small);
}
.example2 {
  max-width: mq-get(medium);
}
/* Resulting CSS */
.example {
  max-width: 480px;
}
.example2 {
  max-width: 640px;
}

$ems

$ems is a boolean (false or true, false by default) and dictates whether or not the return is in pixels or ems.

.example {
  max-width: mq-get(small);
}
.example-ems {
  max-width: mq-get(small, true);
}
/* Resulting CSS */
.example {
  max-width: 480px;
}
.example2 {
  max-width: 30em;
}

Notes

1. Requires Node.js

2. Also available on Bower

License

The MIT License

Copyright © 2014–2016 Jonathan Suh (@jonsuh)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Sass library to manage responsive breakpoints and generate media queries

Resources

License

Stars

Watchers

Forks

Packages

No packages published