Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to conditionally load CSS and extend it using SASS/SCSS
    primarykey
    data
    text
    <p><strong>tl:dr version:</strong> is there a way to @extend a css class and not have the original class appear in my compiled css <em>without</em> changing all my css classes to %placeholder classes?</p> <p><strong>Short answer based on the below answers</strong>: it appears there is no way to do this unless you go through and convert the css to silent/placeholder classes e.g. convert .one{} to %one{} and even then that will cause problems with media queries.</p> <p>I have a css file (lets call it "style.css") which contains 200+ CSS classes to style various elements like forms and buttons etc. What I want is to include some of those classes in a project and other classes from that file in other random projects/websites. With each new project I also want to give the classes random semantic class names of my choosing.</p> <p>My preprocessor of choice when working with CSS is SCSS and I really need an answer that uses the power of SCSS. </p> <p>Here is a quick example of what I'm talking about - loading css into a SCSS file and then extending that css with my own class names:</p> <pre><code>//style.css .one { color: red; padding-top: 1px; } //style2.scss @import "style.css"; .two { @extend .one; } </code></pre> <p>The problem here is that my SCSS file will compile to CSS and look like this:</p> <pre><code>//style2.css .one { color: red; padding-top: 1px; } .two { color: red; padding-top: 1px; } </code></pre> <p>But what I want to do is only include the second class, which I gave a special name. </p> <p>I've tried a few ways of doing this but here's one example that does not work but is along the lines of what I was thinking I should be able to do:</p> <p>A.) First, I grab the style.css file and chuck copy/paste it into a style.scss file.</p> <p>B.) Second I wrap all the whole thing in a placeholder/silent class, like so:</p> <pre><code>//style.scss %placeholder { .one { color: red; padding-top: 1px; } } </code></pre> <p>C.) Then I import that SCSS file and try and extend a class of my choosing that is within the placeholder, like this:</p> <pre><code>//style2.scss @import "style"; .two { @extend .one; } </code></pre> <p>When I try and compile this I get <strong>a blank css file</strong> (and rightly so for trying to be too tricky). The other thing I know is that you can't extend nested selectors so "@extend %placeholder .one;" is also out of the question.</p> <p><strong>My question is this: does anyone know of a way to import and then extend a css class so that the compiled result does not include the imported css?</strong></p> <p>The only other solution I can think of is to just delete the imported css from the top of my file before I let it out into the wild. But this is honestly less than ideal solution.</p> <p>Thank you in advance to any answers :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload