[Sass] Variable -- Ex

VARIABLES

The color hex value #797979 has been popping up quite a bit throughout our stylesheet. Let's store the color in a variable and replace all instances of it with that variable.

复制代码
$base-color: #797979;

.surveyor {
  border: 1px solid $base-color;
  padding: 20px;
}
.surveyor h2 {
  color: $base-color;
  font-size: 18px;
}

.notice {
  background: yellow;
  border: 5px solid $base-color;
  padding: 10px;
}
复制代码

 

 

THE DEFAULT FLAG

A coworker has been asking to see our modals partial for another project. Before we hand it over, let's clean up the repeated padding declarations to use a variable. To keep it modular, that variable definition should ensure it doesn't override a variable of the same name set elsewhere.

复制代码
.modal {
  background: #fff;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
  padding: 10px;
}
.modal-title {
  border-bottom: 1px solid #ccc;
  font-size: 24px;
  padding: 10px;
}
.modal-action {
  background: purple;
  display: block;
  padding: 10px;
}
复制代码

Answer:

复制代码
$pand-all: 10px !default;

.modal {
  background: #fff;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
  padding: $pand-all;
}
.modal-title {
  border-bottom: 1px solid #ccc;
  font-size: 24px;
  padding: $pand-all;
}
.modal-action {
  background: purple;
  display: block;
  padding: $pand-all;
}
复制代码

 

LISTS

A last-minute change from the designer just came through: the padding value for the various modal components should be 20px 10px 15px instead of 10px. Knowing we can have lists stored in variables, alter $modal-padding to reflect this revision.

复制代码
$modal-padding: 10px !default;

.modal {
  background: #fff;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
  padding: $modal-padding;
}
.modal-title {
  border-bottom: 1px solid #ccc;
  font-size: 24px;
  padding: $modal-padding;
}
.modal-action {
  background: purple;
  display: block;
  padding: $modal-padding;
}
复制代码

Answer:

复制代码
$modal-padding: 20px 10px 15px !default;


.modal {
  background: #fff;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
  padding: $modal-padding;
}
.modal-title {
  border-bottom: 1px solid #ccc;
  font-size: 24px;
  padding: $modal-padding;
}
.modal-action {
  background: purple;
  display: block;
  padding: $modal-padding;
}
复制代码

 

VARIABLE NESTING

Our $font-base variable comes in handy when setting the font family in body, but we're getting an error when trying to use it elsewhere. Refactor the styles below so that $font-base is usable throughout.

复制代码
body {
  $font-base: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  background: #fff;
  font: normal 16px/1.5 $font-base;
  margin: 0;
  padding: 0;
}
.blueprint {
  font: bold 24px/1.3 Georgia, serif;
  text-align: center;
  span {
    font-family: $font-base;
  }
}
复制代码

Answer:

复制代码
$font-base: 'Helvetica Neue', Helvetica, Arial, sans-serif;
body {
  font: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  background: #fff;
  font: normal 16px/1.5 $font-base;
  margin: 0;
  padding: 0;
}
.blueprint {
  font: bold 24px/1.3 Georgia, serif;
  text-align: center;
  span {
    font-family: $font-base;
  }
}
复制代码

 

INTERPOLATION

We'd like to add a simple way to set which side borders appear on. Create a variable with the current value (left) and interpolate that value into each border property occurance.

复制代码
.girder {
  border-left: 4px solid #ccc;
  h2 {
    font-size: 24px;
  }
}
.notice {
  border-left: 8px solid #797979;
  a {
    color: #222;
  }
}
复制代码

Answer:

复制代码
$trend: left; 

.girder {
  border-#{$trend}: 4px solid #ccc;
  h2 {
    font-size: 24px;
  }
}
.notice {
  border-#{$trend}: 8px solid #797979;
  a {
    color: #222;
  }
}
复制代码

 

posted @   Zhentiw  阅读(314)  评论(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工具
点击右上角即可分享
微信分享提示