[SCSS] Organize SCSS into Multiple Files with Partials
Tired of dealing with monolithic CSS files? Are requests for multiple CSS files hurting your performance? In this lesson we learn how to separate our styles with SCSS partials, and how SCSS imports compile to one file so there's only one request.
Things to know:
- If importing partial file, won't generate a new css file
- If importing normal scss file, it will generate a new css file
So when we should use partial import or not, you can think that whether this file should be used when app first loading? If it yes, then use partial import, if not, then normal import. The reason behind that is because browser will loading main css (the main.scss file that import other scss files ) file first. After this main.css file loaded, then browser will start loading other css file.
As you can see other.css which are import as normal import mode. "Content downloaded" happens after main.css file are downloaded.
- Only partial file's variable can be shared accross the rest of partial file. But it also require you import that partial file which has variables defines into main.scss file before the rest partial files.
/** About using variable There are tow cases when using variable 1. none partial file (provider) + none partial file Variable only available for its own file scope. If you want to use one variable inside file A from file B You have to import file A into file B. 2. partial file (provider) + none partial file If you have an variable defined in partial file, and you want to use it inside none partial file, you also need to import partial file into none partial file. 3. partial file (provider) + partial file If you have an variable defined in partial file A, and you want to use it inside another partial file B, you have to import file A into main.scss file before you import file B into main.scss. Then you can use variable inside file A inside file B. */ @import "color"; @import "partial"; @import "other"; .color { color: $primary-color; // From _color }
_color.scss
$primary-color: lighten(red, 15%); .red { color: $primary-color; }
_partial.scss
.l-border { border-left: 5px solid $primary-color; }
other.scss
@import "color"; .bg { background-color: $primary-color; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-04-08 [Git] Automatically running tests before commits with ghooks