I'm using a "mobile first" approach on my site, and I'm utilizing SCSS.
I have 3 SCSS stylesheets:
base.scss
(served to all)medium.scss
(>= 768px window)large.scss
(>= 1024px window)They are served like this:
<link rel="stylesheet" href="/css/base.css" type="text/css" />
<link rel="stylesheet" href="/css/medium.css" type="text/css" media="only screen and (min-width: 767px)" />
<link rel="stylesheet" href="/css/large.css" type="text/css" media="only screen and (min-width: 1023px)" />
In other words, large.scss
has no knowledge of the css in base.scss
, yet I need to extend a class that resides in base.css
, when in large.scss
.
How can I do this?
I tried separating the elements I need to extend, such as the buttons, into a separate stylesheet, and then use @import 'buttons.scss';
in medium.scss
, but then all the css in buttons.scss
will be rendered in that sheet as well.
Are there any way to make expose classes in base.scss to the SASS "rendering engine" when compiling the css so that they can be used in medium.scss?
It's written @extend <selector> , and it tells Sass that one selector should inherit the styles of another.
Sass Importing Files Just like CSS, Sass also supports the @import directive. The @import directive allows you to include the content of one file in another.
Sass consists of two syntaxes.
scss is the source file and style. css is the destination file where Sass generates the CSS code. Now installation and configuration are complete! You can use Sass in your projects.
I had similar issue recently. My solution: mixins.
_mixins.scss
@mixin someSpacing($base: 5px;){
padding: $base;
margin: $base;
}
Remember to use underscore as prefix, sass wont compile those files. So now you can simply import _mixins.scss to your files. Additionally you can extend your mixin with parameters to give you more flexibility.
e.g. base.scss
@import '_mixins.scss';
.classA {
@include someSpacing(3px);
}
large.scss
@import '_mixins.scss';
.anotherClass {
@include someSpacing(10px);
}
more details in sass reference: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#mixins
I needed this as well once. I asked Chris Eppstein today and he said:
extend is per output file
So yeah. @include is your only hope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With