mq()
is a Sass mixin that helps you compose media queries in an elegant way.
- compiles keywords and
px
/em
values toem
-based queries (a good thing) - For version 6 and up we removed fallbacks for older browsers (see Mobile-first Responsive Web Design and IE8 on the Guardian's developer blog).
Here is a very basic example:
@use 'mq' as * with (
$breakpoints: (
mobile: 320px,
tablet: 740px,
desktop: 980px,
wide: 1300px
)
);
.foo {
@include mq($from: mobile, $until: tablet) {
background: red;
}
@include mq($from: tablet) {
background: green;
}
}
Compiles to:
@media (min-width: 20em) and (max-width: 46.24em) {
.foo {
background: red;
}
}
@media (min-width: 46.25em) {
.foo {
background: green;
}
}
Sass MQ was crafted in-house at the Guardian. Today, many more companies and developers are using it in their projects: see who uses Sass MQ.
How to use it
Immediately play with it on SassMeister: @use 'mq';
.
OR:
-
Install:
- with Bower:
bower install sass-mq --save
- with npm:
npm install sass-mq --save
supports eyeglass - with yarn:
yarn add sass-mq
supports eyeglass
OR Download _mq.scss into your Sass project.
- with Bower:
-
Import the partial in your Sass files and override default settings with your own preferences:
// Name your breakpoints in a way that creates a ubiquitous language
// across team members. It will improve communication between
// stakeholders, designers, developers, and testers.
$breakpoints: (
mobile: 320px,
tablet: 740px,
desktop: 980px,
wide: 1300px,
// Tweakpoints
desktopAd: 810px,
mobileLandscape: 480px,
);
// If you want to display the currently active breakpoint in the top
// right corner of your site during development, add the breakpoints
// to this list, ordered by width. For examples: (mobile, tablet, desktop).
$breakpoints-shown: (mobile, mobileLandscape, tablet, desktop, wide);
@use 'path/to/mq' with (
$breakpoints: $breakpoints,
$show-breakpoints: $breakpoints-shown,
);
@use
Vs @import
Notes about When using the @use
directive, you have to change your mindset when working with vars, functions or mixins and how they are now seen by Sass.
Previously, with the @import
statement any var, function, or mixin were exposed in the global scope. That means that you could define a var like $mq-media-type: all
in your main sass file and use it anywhere as long as the main file had been loaded previously.
This was possible because vars, functions, and mixins were set in the global scope.
One drawback of this behaviour was that we needed to ensure not to pollute the global scope with common names or names that may be already taken by any other library.
To solve this matter, we mostly used a prefix in vars, functions, or mixins in order to avoid collapsing names.
Now with the new @use
directive, no var, function, or mixin is placed in global scope, and they are all scoped within the file.
That means that we explicitly need to include the partial file in each file that may use its vars, functions or mixins (similar to ES6 import modules).
So, previously we could have a typical setup like this:
// main.scss
@import 'mq';
@import 'typography';
@import 'layout';
@include mq($from:tablet) {
...
}
...
// typography.scss
@include mq($from:tablet) {
...
}
Now, you will need to explicitly import the _mq.scss
file in each file that needs to use any var, function or mixin from it:
// main.scss
@use 'mq';
@use 'typography';
@use 'layout';
@include mq.mq($from:tablet) {
...
}
...
// typography.scss
@use 'mq';
@include mq.mq($from:tablet) {
...
}
Other important things about @use
:
-
The file is only imported once, no matter how many times you @use it in a project.
-
Variables, mixins, and functions (what Sass calls “members”) that start with an underscore (_) or hyphen (-) are considered private, and not imported.
-
Members from the used file are only made available locally, but not passed along to future imports.
-
Similarly,
@extends
will only apply up the chain; extending selectors in imported files, but not extending files that import this one. -
All imported members are namespaced by default.
Please see introducing-sass-modules for more info about sass modules.
- Play around with
mq()
(see below)
Responsive mode
mq()
takes up to three optional parameters:
$from
: inclusivemin-width
boundary$until
: exclusivemax-width
boundary$and
: additional custom directives
Note that $until
as a keyword is a hard limit i.e. it's breakpoint - 1.
@use 'mq';
.responsive {
// Apply styling to mobile and upwards
@include mq.mq($from: mobile) {
color: red;
}
// Apply styling up to devices smaller than tablets (exclude tablets)
@include mq.mq($until: tablet) {
color: blue;
}
// Same thing, in landscape orientation
@include mq.mq($until: tablet, $and: '(orientation: landscape)') {
color: hotpink;
}
// Apply styling to tablets up to desktop (exclude desktop)
@include mq.mq(tablet, desktop) {
color: green;
}
}
Verbose and shorthand notations
Sometimes you’ll want to be extra verbose (for example, if you’re developing a library based on top of sass-mq), however for readability in a codebase, the shorthand notation is recommended.
All of these examples output the exact same thing and are here for reference, so you can use the notation that best matches your needs:
@use 'mq';
// Verbose
@include mq.mq(
$from: false,
$until: desktop,
$and: false,
$media-type: $media-type // defaults to 'all'
) {
.foo {
}
}
// Omitting argument names
@include mq.mq(false, desktop, false, $media-type) {
.foo {
}
}
// Omitting tailing arguments
@include mq(false, desktop) {
.foo {
}
}
// Recommended
@include mq($until: desktop) {
.foo {
}
}
See the detailed API documentation
Adding custom breakpoints
@include add-breakpoint(tvscreen, 1920px);
.hide-on-tv {
@include mq(tvscreen) {
display: none;
}
}
Seeing the currently active breakpoint
While developing, it can be nice to always know which breakpoint is active. To achieve this, set the $show-breakpoints
variable to be a list of the breakpoints you want to debug, ordered by width. The name of the active breakpoint and its pixel and em values will then be shown in the top right corner of the viewport.
// Adapt the list to include breakpoint names from your project
$show-breakpoints: (phone, phablet, tablet);
Changing media type
If you want to specify a media type, for example to output styles for screens only, set $media-type
:
SCSS
@use 'mq' with ($media-type: screen);
.screen-only-element {
@include mq.mq(mobile) {
width: 300px;
}
}
CSS output
@media screen and (max-width: 19.99em) {
.screen-only-element {
width: 300px;
}
}
Implementing sass-mq in your project
Please see the examples
folder which contains a variety of examples on how to implement "sass-mq"
@import
Backward compatibility with Just in case you need to have backward compatibility and want to use@import
instead of @use
, you can do so by importing _mq.import.scss
instead of _mq.scss
.
Please see legacy.scss
on examples
folder.
Running tests
npm test
Generating the documentation
Sass MQ is documented using SassDoc.
Generate the documentation locally:
sassdoc .
Generate & deploy the documentation to http://sass-mq.github.io/sass-mq/:
npm run sassdoc
Inspired By…
- https://github.com/alphagov/govuk_frontend_toolkit/blob/master/stylesheets/_conditionals.scss
- https://github.com/bits-sass/helpers-responsive/blob/master/_responsive.scss
- https://gist.github.com/magsout/5978325
On Mobile-first CSS With Legacy Browser Support
- http://jakearchibald.github.io/sass-ie/
- http://nicolasgallagher.com/mobile-first-css-sass-and-ie/
- http://cognition.happycog.com/article/fall-back-to-the-cascade
- http://www.theguardian.com/info/developer-blog/2013/oct/14/mobile-first-responsive-ie8
Who uses Sass MQ?
Sass MQ was developed in-house at the Guardian.
These companies and projects use Sass MQ:
- The Guardian
- BBC (Homepage, Sport, News, Programmes)
- The Financial Times
- Rightmove
- Stockholm International Fairs and Congress Centre
- Beyond
- EQ Design
- Baseguide
- Base Creative
- Locomotive
- Le Figaro (TV Mag)
- LunaWeb
- inuitcss
- Hotelbeds Group
- Beneš & Michl
- Manchester International Festival
- Shopify Polaris
- Taylor / Thomas
- GOV.UK Design System
- You? Open an issue
Looking for a more advanced sass-mq, with support for height and other niceties?
Give @mcaskill's fork of sass-mq a try.
Dart sass 1.34/version bump
New bump Version.
TODO
I add about 22 jest test to check for functionality, but I left in this MR old test to decide if we should keep them or remove them along with their dependencies to
eyeglass
. Since they are now all cover with jest test andsass-true
package and this it will be a good idea to remove themAdd diamond support
Following up on our conversation via email I have added diamond support. I didn't know if you wanted the package to be named
sass-mq
like npm and bower ormq
as you refer to it in the readme.To publish the package when you are ready, run the following commands: Install diamond:
npm i -g diamondpkg
Register for an account:
diamond login
Publish the package (run in package dir)
diamond publish
Ability to show active breakpoint in corner of viewport
A little bonus feature that I've found quite useful for debugging and cross-device testing: Flip a switch to show the active breakpoint in the top right corner of the viewport.
Add the ability to override width calculation - Closes #64
This PR is an answer to #64 and #59. Thanks to @fvch, @davidhund, @shaunbent and @smbeiragh for their input.
I want to give authors the ability to use whatever unit they want, but I also really want to keep ems as the very opinionated default.
Instead of giving an option between ems/rems/px, I'm allowing developers to tweak sass-mq at its core by overriding the width calculation.
Install the new beta:
npm i sass-mq@^3.3.0 --save
or
bower i sass-mq@^3.3.0 --save
Let me know what you think!
Todo
mq-width
andmq-width-override
Keep all media queries in px
Convert media queries to rem
sass-mq v4: Sort added breakpoints
Fixes #101 - Thanks to @solleer for raising this issue!
This pull request is the new sass-mq v4
This PR contains a small breaking change that should only affect a few users.
⭐️ Upgrading to v4.0.0 is recommended ⭐️
How to test v4.0.0 beta in your project:
You can also download the latest _mq.scss into your project.
Before
New breakpoints added via
mq-add-breakpoint
are simply appended to$mq-breakpoints
:After
New breakpoints added via
mq-add-breakpoint
keeps$mq-breakpoints
ordered from the smallest to the largest breakpoint:Thanks to @snugug for providing a quick-sort function!
Stop converting media queries to em
Hi,
sass-mq currently converts all breakpoints to
em
values, using a ratio of1em = 16px
(that ratio can be changed but it would definitely create unexpected behavior for users, so it's a good thing the variable for that is not advertised). The rationale for converting toem
was browser limitations (January 2014 post) with zooming. More specifically, it was a WebKit bug, affecting Chrome and Safari, which has since been fixed (around 2014?).No zoom issue with
px
media queries in my tests on:So it seems safe to say it's a non-issue now. What's more, using
em
media queries as substitutes for pixels can pose a risk if end users have changed the browser's base font size to something that is not 16px (to be confirmed; not sure if it does impactem
-based media queries in all browsers).I suggest that sass-mq stop converting pixels to ems and allow users to set breakpoints in ems in
$mq-breakpoints
(if that is not already the case). Formax-width
queries, a pixel breakpoint should be reduced by one, e.g.(max-width: 799px)
if the breakpoint is800px
.Warning using / for division is deprecated
Updating sass to version 1.34.0 I get this warning from sass:
Redesign and update library
TL;RD : redesign and update library
Motivation
Deprecation of
@import
and global vars and addition of new functionalitieswith the addition of new functionalities on dart sass compiler , this library starts to get deprecation warnings and will probably stop working by October 2022.
Read more in this post
Abandon of sass engines other than dart-sass
Also since now there were 3 different sass compilers but it look that now there will be only one maintained (dart sass), so this makes unnecessary to maintain test and compatibility with the other two sass engines:
Current Releases for sass engines are: Dart Sass 1.34.0: Dart Sass is the primary implementation of Sass, which means it gets new features before any other implementation. Also currently is the only one maintain
LibSass 3.6.5: LibSass is Deprecated
Ruby Sass ⚰ Ruby Sass was the original implementation of Sass, but it reached its end of life as of 26 March 2019. It's no longer supported, and Ruby Sass users should migrate to another implementation.
Drop compatibility of old browsers
This library has some backward compatibilities with very, very old browsers (IE8) that didn't support media queries and it looks reasonable that we could get read of this compatibility
Redesign
Whit all these things said, it looks that a redesign / rethinking of this library may need it to be done
Checkpoints
Bellow I will list some checkpoint that I detect that can be done in this library, probably there are some more and maybe not all these points are the best approach, I just put it here to discuss them.
$mq-base-font-size
since it is deprecated$mq-responsive
(Drop compatibility of old browsers)$mq-breakpoints
maybe we can use build in modulesass.map
map-has-key(..)
in favor ofmap.has-key(...)
@use
@import
instead of@use
(this can be easily done by using@forward
directive)Bibliography
the module system is launched libsass is deprecated sass built in modules
Subpixel issue in FF and IE11
After extensive testing, I believe this value should go down one more decimal for it to work perfectly with FF and IE11. It should be
0.001em
instead of0.01em
.https://github.com/sass-mq/sass-mq/blob/47cd6b1156d35a702edd3fbed0bcd285dcd95d29/_mq.scss#L195
Add an option for overriding default media type in queries
Membership have had a few issues with media queries applying to
all
. I'd like to add this new option,$mq-media-type
(which defaults toall
) to allow us to override this.Error: media query expression must begin with '('
I'm getting this error with the latest version of node:
I'm not even sure what dependency might be at fault here, so I'm sorry if I'm way off. Any ideas about why this might be occurring?
Bump minimatch from 3.0.4 to 3.1.2
Bumps minimatch from 3.0.4 to 3.1.2.
Commits
699c459
3.1.22f2b5ff
fix: trim pattern25d7c0d
3.1.155dda29
fix: treat nocase:true as always having magic5e1fb8d
3.1.0f8145c5
Add 'allowWindowsEscape' option570e8b1
add publishConfig for v3 publishes5b7cd33
3.0.620b4b56
[fix] revert all breaking syntax changes2ff0388
document, expose, and test 'partial:true' optionDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting
@dependabot rebase
.Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR:
@dependabot rebase
will rebase this PR@dependabot recreate
will recreate this PR, overwriting any edits that have been made to it@dependabot merge
will merge this PR after your CI passes on it@dependabot squash and merge
will squash and merge this PR after your CI passes on it@dependabot cancel merge
will cancel a previously requested merge and block automerging@dependabot reopen
will reopen this PR if it is closed@dependabot close
will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually@dependabot ignore this major version
will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)@dependabot ignore this minor version
will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)@dependabot ignore this dependency
will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)@dependabot use these labels
will set the current labels as the default for future PRs for this repo and language@dependabot use these reviewers
will set the current reviewers as the default for future PRs for this repo and language@dependabot use these assignees
will set the current assignees as the default for future PRs for this repo and language@dependabot use this milestone
will set the current milestone as the default for future PRs for this repo and languageYou can disable automated security fix PRs for this repo from the Security Alerts page.
Basic container query support
Perhaps this is out of scope of this library, but it would be lovely to have some kind of container query support in sass-mq. Use it exclusively and have for years, so would be awesome to not have to use
@container
syntax directly just like I don't have to with@media
.There's loads more syntax to be explored should there be an interest in supporting them fully, especially around things like: Do we apply
container-type
automatically somehow? Do we support style queries?Anyway, this is a really basic implementation that allows rudimentary usage:
or
Using / for division outside of calc() is deprecated and will be removed in Dart Sass 2.0.0
Hi,
Using sass-mq with Dart Sass gives the following deprecation warning:
Deprecation Warning: Using / for division outside of calc() is deprecated and will be removed in Dart Sass 2.0.0.
Since locally editing "_mq.scss" in my node_modules isn't a good long-term solution (Also: it's a shared project so...) Is there any way, outside of editing "_mq.scss", to remove/avoid this warning?
I would like to keep using sass-mq but also use the latest sass and other tools. Ok, it's just a warning but it would be nicer/cleaner/better to have no (deprecation) warnings or errors at all.
Some extra info: node: 16 sass-mq: ^5.0.0 sass: ^1.52.3
Thanks in advance!