I am looking to define a variable for my media queries across my SASS/CSS files.
The general structure of my sass files for a given page are
- file.scss
- _variables.scss
- _colors.scss
- _web.scss
- _tablet.scss
- _mobile.scss
file.scss has nothing but @use statements (@use over @import because the SASS team is headed away from @import), _variables/_colors have my CSS variables for element properties and colors (respectively) and the remaining 3 files (I'll refer to them generically as _layout.scss) have the layouts and other features per screen size.
Since I have multiple file.scss files, and each corresponding _layout.scss needs a @media query with max-width set, I would ideally like a global variable I can use in an _global.scss file to include in all the _layout.scss files consistently rather than redefine the same max-widths manually in each of them.
I cannot use a CSS variable (I don't think) because those need to be defined inside a selector, and that doesn't import well into the @media query (I tried this including just defining the variable in :root to no avail).
A local sass variable (notably only the pre-processor can see this which is kind of a bummer, but I could live with that) will work as is shown in this CSS Tricks post, but this doesn't really provide a global solution where I could simply use the value.
That is all to say I want a global sass variable (i.e. defined in only one file, but used in may files) where I can then use @media(max-width: var(--my-variable)) or @media(max-width: $my-variable) in my local _layout.scss files for a consistent use of the same values.
I attempted to create a _global.scss file with my declarations and @use them in my _layout.scss files, but my sass compiler (Dart v 1.25.0) doesn't recognize this, and while I can do this using @import, the same reason as above (@import going away) makes me reticent to do that if there's an actual good solution out there which are going to be stable through that change.
An example of how this is structured in page.scss:
@use 'web';
@use 'tablet';
@use 'mobile';
//Note only one of the two below is present at once
@use '../App/global'; //doesn't work
@import '../App/global'; //does work
My styles folder structure
- styles
AppFolder
- _colors.scss - _global.scss - _variables.scss - App.scssGenericComponentFolder
- \_colors.scss - \_mobile.scss - \_tablet.scss - \_web.scss - file.scss
_global.scss looks like this (recall we're using SASS variables since CSS variables need to be defined inside a selector):
$small-laptop: 1400px;
$tablet: 1023px;
$mobile: 765px;
$small-mobile: 400px;
in _web.scss we use the variables like so
@media(max-width: $small-laptop) {
.my-selector {
height: 10%;
}
@importfor this.@importis not going to be deprecated anytime soon - the SASS team is saying October of 2021 and then ending support a year later. So, there is at least 2 years to convert to@useif your project is still being maintained. The good solution is that@useand@importwill work at the same time, so you can gradually shift to@use@useyou need to namespace the variable using the file name it comes from. You should be doing this@media(max-width: global.$small-laptop)...