sass - How to handle global SCSS variables with `@use` when migrating from `@import`? - Stack Overflow
What is the most efficient way to use CSS/SCSS in your project?
reactjs - How to use SCSS with Tailwind CSS? - Stack Overflow
reactjs - How to use SCSS variables into my React components - Stack Overflow
Videos
It's possible to configure modules with @use ... with or a mixin can be created to set the variables.
// _library.scss
$black: #000 !default;
$border-radius: 0.25rem !default;
$box-shadow: 0 0.5rem 1rem rgba($black, 0.15) !default;
code {
border-radius: $border-radius;
box-shadow: $box-shadow;
}
// style.scss
@use 'library' with (
$black: #222,
$border-radius: 0.1rem
);
Replicate the way Material solved this issue using CSS variables rather than SCSS variables.
I always have to scroll through the css file to the class I want to update or create. Is there an extension or a tool that would help me be more organized in terms of style? For example by clicking on the className in the project it pops up the stylesheet on the side for me to update easily?
TailwindCSS v4
Starting from TailwindCSS v4, support for preprocessors has been discontinued, meaning that SASS can no longer be used with it from v4 onwards.
- Compatibility
- Deprecated: Sass, Less and Stylus preprocessors support
tailwindlabs/tailwindcss#15716 by RobinMalfait:
Migration tool doesn't recognize .scss files
TailwindCSS v4 and Sass (works, but independently of each other)
You can use TailwindCSS and Sass side by side separately. Create a tailwind.css file (important: not an .scss file) with content related only to TailwindCSS, for example:
@import "tailwindcss";Then reference this new file in your styles.scss like this in ./scss/main.scss:
@use "custom.scss"; $primary: #42b883; body { background: $primary; }You need to include
styles.scssandtailwind.cssas separate files in the project. They must not interact with each other or be nested into one another.main.ts
import "./main.scss"; import "./tailwind.css";
tailwindlabs/tailwindcssdiscussion #18364: Support Angular SCSS with TailwindCSS v4- How to use @apply in Tailwind v4?
Note: So officially it's not supported and cannot be used together directly, but they can be used side by side. Keep in mind that compatibility issues between TailwindCSS and these preprocessors will not be a priority in the future.
Related:
- Angular not detecting changes in
.scssfiles @import "tailwindcss";does not work when used in a file with an.scssextension
Extra: open source template
I put together an open-source template. It's difficult to implement directly in a Stack Overflow answer, but it can serve as a useful learning example for using TailwindCSS and Sass in the same project, including both global CSS, CSS modules, and style blocks.
- Template: Vite (with TypeScript) + Sass + TailwindCSS v4
It's pretty much same
- Install:
npm install sass --save-dev - Import:
import '../scss/yourStyle.scss';
Your style will be applied.
But I don't think you need SCSS when you use TailwindCSS though. TailwindCSS is very powerful
From React 17
To access your scss variables into the react component, you need to do something like that
- Install
node-sassas a dependency or dev dependency - No need to do any config change in webpack
- import as a module <-- main point
variables.module.scss
$color: skyblue;
$primaryColor: red;
:export {
color: $color;
primary-color: $primaryColor;
}
App.js
import variables from '<YOUR_PATH>/variables.module.scss';
const App = () => {
console.log(variables);
}
If you don't want to use styled-component
then you can follow this link.
https://til.hashrocket.com/posts/sxbrscjuqu-share-scss-variables-with-javascript