Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS/SCSS using @extend across individual files (responsive design)

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?

like image 293
phobia Avatar asked Mar 14 '12 10:03

phobia


People also ask

What does @extend do in SCSS?

It's written @extend <selector> , and it tells Sass that one selector should inherit the styles of another.

Which directive is used to convert multiple Sass into single or multiple CSS files?

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.

How many syntaxes that Sass support?

Sass consists of two syntaxes.

Can I use Sass in SCSS?

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.


2 Answers

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

like image 187
Rito Avatar answered Sep 26 '22 15:09

Rito


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.

like image 21
Chris Coyier Avatar answered Sep 26 '22 15:09

Chris Coyier