css - SCSS - 结合/缩短/简化代码与参数?

标签 css sass simplify scss-mixins

是否有可能以某种方式将此 SCSS 代码与参数或类似的东西结合起来?我只是不知道该怎么做。这段代码是关于 SCSS 中不同类型输入字段的样式。我不得不这样做,因为我不想删除不同类型的特定属性(例如 type=password -> 我希望我仍然屏蔽字段中的输入)

input[type=text]{
  width:100%;
  border:1px solid $nav-color;
  border-radius:4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing:border-box;
  transition:.3s;
  &:focus{
    border-color: $turquoise;
    box-shadow: 0 0 4px 0 $turquoise;
  }
}

.inputWithIcon{
  position:relative;
  input[type=text]{
    padding-left:40px;
    &:focus + i{
      color:$turquoise;
    }
  }
  i{
    position:absolute;
    left:0;
    padding: 10px 7px;
    color: $nav-color;
    transition:.3s;
  }
}

input[type=date]{
  width:100%;
  border:1px solid $nav-color;
  border-radius:4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing:border-box;
  transition:.3s;
  &:focus{
    border-color: $turquoise;
    box-shadow: 0 0 4px 0 $turquoise;
  }
}
.inputWithIcon{
  position:relative;
  input[type=date]{
    padding-left:40px;
    &:focus + i{
      color:$turquoise;
    }
  }
  i{
    position:absolute;
    left:0;
    padding: 10px 8px;
    color: $nav-color;
    transition:.3s;
  }
}

input[type=password]{
  width:100%;
  border:1px solid $nav-color;
  border-radius:4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing:border-box;
  transition:.3s;
  &:focus{
     border-color: $turquoise;
     box-shadow: 0 0 4px 0 $turquoise;
  }
} 

最佳答案

这看起来是使用占位符的好时机。占位符不直接输出代码,但可以通过其他选择器扩展以输出其内容。

$nav-color: #333;
$turquoise: #48d1cc;

// Input placeholder
%input {
  width: 100%;
  border: 1px solid $nav-color;
  border-radius: 4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing: border-box;
  transition: color .3s;

  &:focus {
    border-color: $turquoise;
    box-shadow: 0 0 4px 0 $turquoise;
  }
}

input[type=text],
input[type=date],
input[type=password] {
  @extend %input;
}


.inputWithIcon {
  position: relative;

  input {
    padding-left: 40px;

    &:focus + i {
      color: $turquoise;
    }
  }

  i {
    position: absolute;
    left: 0;
    padding: 10px 7px;
    color: $nav-color;
    transition: color .3s;
  }
}

关于css - SCSS - 结合/缩短/简化代码与参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50208140/

相关文章:

file - 在 Adob​​e Dreamweaver 中使用 SASS

java - 简化 boolean 表达式

html - 如何使垂直滚动条更薄并摆脱右下角的空白空间?

css - 生成不同大小的图像

css - 在没有 jquery 的情况下悬停后保持 div 打开

css - 表格 css 的 .asp 循环

node.js - Webpack 未捆绑输出 JS 文件

css - LESS 是否具有 "extend"功能?

c++ - 我可以使用什么 C++ 库来创建和简化贝塞尔曲线

powerbi - 删除 Power Bi 中未使用的度量