[SCSS] SCSS and CSS Variables

We'll define the color palette and create the basic screen styles for our project in addition to Sass helpers for our form design system.

We will set up Sass variables for the initial them and learn how to use the !default flag to allow our system to be easily overridden from project to project, making our form styles ultra transferable and reusable. You'll also learn how to leverage Sass color functions for ease of theming. We'll map the Sass variables to CSS variables so that as we build up our system we can use CSS variables for the most flexibility in our theming.

 

_theme.scss:

复制代码
$color-primary: #041db1 !default;
$color-error: #da1b22 !default;
$color-background: mix(#fff, $color-primary, 95%) !default;
$color-text: scale-color($color-background, $lightness: -65%) !default;

// create a colors map
$colors: (
  "primary": $color-primary,
  "error": $color-error,
  "background": $color-background,
  "text": $color-text,
  "white": #fff,
);

@function color($key) {
  @return map-get($colors, $key);
}

$border-radius: 0.25em !default;

// enable css custom properties
// instead of using scss variable
:root {
  @each $key, $color in $colors {
    --color-#{$key}: #{$color};
  }

  --border-radius: #{$border-radius};
}
复制代码

 

'!default': You can assign to variables if they aren’t already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won’t be re-assigned, but if it doesn’t have a value yet, it will be given one.

 

_screen.scss:

复制代码
body {
  // using color("background") as fallback
  background-color: var(--color-background, color("background"));
  color: var(--color-text, color("text"));
  font-family: "Roboto", sans-serif;
}

header,
main {
  width: 60ch;
  max-width: 100%;
  margin: 0 auto;
}
复制代码

 

style.scss:

@import "theme";
@import "screen";

 

posted @   Zhentiw  阅读(164)  评论(0编辑  收藏  举报
编辑推荐:
· 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工具
历史上的今天:
2020-03-02 [Github] Create a GitHub PR Template
2020-03-02 [Github] Create a GitHub Issue Template
2017-03-02 [SVG] Combine Multiple SVGs into an SVG Sprite
2017-03-02 [Ramda] Difference between R.converge and R.useWith
2017-03-02 [Ramda] Refactor to a Point Free Function with Ramda's useWith Function
2017-03-02 [Angular] Dynamic component's instance and sorting
2016-03-02 [React] Styling a React button component with Radium
点击右上角即可分享
微信分享提示