【问题标题】:what would be an elegant way to implement this design in scss?在 scss 中实现这种设计的优雅方式是什么?
【发布时间】:2017-03-18 04:08:46
【问题描述】:

我在 html 中创建了一个显示小部件,我正在尝试找出一种优雅的方式来设置它的样式。这是一个jsfiddle:

https://jsfiddle.net/fbwu9aoc/3/

我尝试用有意义的类名封装一些样式,例如:

.moreThanMinus100
{
    background-color:darkgray;
    display:table-cell;
    width:50px;
    line-height:10px;
    border:2px solid lightgrey;
}

但没有应用 css 定义。这个 jsfiddle 也代表了 css 的部分提取。我确信有一种更优雅的方式来实现这个小部件的 css,尤其是使用 scss。是否有任何 scss 专业人士可以帮助阐明这一点?

【问题讨论】:

    标签: html css sass less


    【解决方案1】:

    ID 用# 选择,类名用. 选择

    为你的元素

    <div id="moreThanPlus100">&nbsp;</div>
    

    改成

    <div class="moreThanPlus100">&nbsp;</div>
    

    或者把你的css改成

    #moreThanPlus100
    {
        background-color:darkgreen;
        display:table-cell;
        width:50px;
        line-height:10px;
        border:2px solid lightgrey;
    }
    

    【讨论】:

      【解决方案2】:

      基于 cmets 编辑:

      Codepen Scss fiddle link

      Scss 结构片段:

      #VarianceColorKey {
          width: 370px;
          display: table;
          .common {
              display: table-cell;
              line-height: 10px;
              border-color: lightgrey;
              border: 2px solid lightgrey;
          }
          .tableRow {
              display: table-row;
              .moreThanMinus100 {
                  background-color: #57595b;
                  width: 55px;
              }
              .lessThanMinus100 {
                  background-color: #939599;
                  width: 110px;
                  border-left-width: 0px;
                  border-right-width: 0px;
              }
              .noChange {
                  background-color: white;
                  width: 55px;
              }
              .lessThanPlus100 {
                  background-color: #a5c036;
                  width: 110px;
                  border-left-width: 0px;
                  border-right-width: 0px;
              }
              .moreThanPlus100 {
                  background-color: #586a30;
                  width: 55px;
              }
              .tableCell {
                  display: table-cell;
              }
              .textRight {
                  text-align: right;
              }
              .textLeft {
                  text-align: left;
              }
              .textCenter {
                  text-align: center;
              }
          }
          .tr1 {
              line-height: 30px;
              font-size: 16px;
              font-weight: bold;
          }
          .tr2 {
              line-height: 10px;
          }
          .tr3 {
              font-size: 11px;
          }
          .tr4 {
              line-height: 12px;
              font-size: 11px;
          }
          .textRight {
              text-align: right;
          }
          .textLeft {
              text-align: left;
          }
          .textCenter {
              text-align: center;
          }
      }
      

      原答案:

      • 这与 Sass 无关,至少由共享承担 代码。
      • 请注意,# 用于 ID,它们是唯一的,因此在这种情况下,如果您希望影响许多具有相同 CSS 属性的 html 元素,最好使用类(它们在名称前用点声明 @ 987654328@)。
      • 下面有一个建议,将大多数样式放在样式表中,并避免将它们嵌入到 html 中,从而使后者更具可读性/可编辑性。
      • 通过使用类,您可以使用 同一类(如.common.tableCell)避免重复 其他类中的相同属性。

      .ColorKeySpectrumWidget {
        width: 370px;
        display: table;
        border: 2px;
      }
      
      .common {
        display: table-cell;
        line-height: 10px;
        border: 2px solid lightgrey;
      }
      
      .tableRow {
        display: table-row;
        font-weight: bold;
        line-height: 30px;
      }
      
      .tableRow2 {
        line-height: 10px;
        display: table-row;
      }
      
      .tableRow3 {
        display: table-row;
        font-size: 2px;
      }
      
      .tableRow4 {
        display: table-row;
        font-size: 11px;
        line-height: 12px;
      }
      
      .tableCell {
        display: table-cell;
      }
      
      .textRight {
        text-align: right;
      }
      
      .textLeft {
        text-align: left;
      }
      
      .textCenter {
        text-align: center;
      }
      
      .moreThanMinus100 {
        background-color: darkgray;
        width: 50px;
      }
      
      .lessThanMinus100 {
        background-color: lightgray;
        width: 100px;
      }
      
      .noChange {
        background-color: white;
        width: 50px;
      }
      
      .lessThanPlus100 {
        background-color: limegreen;
        width: 100px;
      }
      
      .moreThanPlus100 {
        background-color: darkgreen;
        width: 50px;
      }
      <div class="ColorKeySpectrumWidget">
        <div class="tableRow">
          <div class="tableCell"></div>
          <div class="tableCell textLeft">Decrease</div>
          <div class="tableCell"></div>
          <div class="tableCell textRight">Increase</div>
          <div class="tableCell"></div>
        </div>
        <div class="tableRow2">
          <!-- color key -->
          <div class="common moreThanMinus100">&nbsp;</div>
          <div class="common lessThanMinus100">&nbsp;</div>
          <div class="common noChange">&nbsp;</div>
          <div class="common lessThanPlus100">&nbsp;</div>
          <div class="common moreThanPlus100">&nbsp;</div>
        </div>
        <div class="tableRow3">&nbsp;</div>
        <div class="tableRow4 textCenter">
          <div class="tableCell">More than</div>
          <div class="tableCell"></div>
          <div class="tableCell textCenter">No</div>
          <div class="tableCell"></div>
          <div class="tableCell">More than</div>
        </div>
        <div class="tableRow4">
          <div class="tableCell textRight">-100%</div>
          <div class="tableCell"></div>
          <div class="tableCell textCenter">change</div>
          <div class="tableCell"></div>
          <div class="tableCell">+100%</div>
        </div>
      </div>

      【讨论】:

      • 感谢西登。您提供的代码是一个很好的更新。有没有办法嵌套这个css?例如,假设另一个 css 文件加载到具有 .tableRow 的 css 类的页面上。我想避免用于我的 ColorKeySpectrumWidget 的 tableRow css 与其他 css 文件中定义的 tableRow css 之间的冲突。
      • 您可以使用 Sass 嵌套:sass-lang.com - 但在您提到的特定情况下,将影响的 CSS 要么是最后加载的,要么是具有更多特异性的(例如 .class1 vs body div .class1),后者将覆盖第一个,因为它有更深的选择器。
      • 您可以将多个选择器放在一个样式中以防止冲突。例如,你可以用 '.ColorKeySpectrumWidget .moreThanPlus100' 代替 '.moreThanPlus100'
      • 根据我的面向对象编程背景,这对我来说将是一个更理想的 sass 结构:jsfiddle.net/r4gsj52q。您能否根据我想到的更理想的结构发布对 snyden 示例的更新以反映正确的 sass 实现?如果您有更深入的了解或发现更多机会,请随时进一步优化。
      • @syden 感谢您的帮助。我刚刚尝试了您发布的最新版本。您的原始实现有效: .tableRow1{ display: table-row;行高:30px;字体大小:16px; font-weight: bold } 但新的实现似乎不起作用: .tableRow{ display: table-row; .tr1 { 行高:30px;字体大小:16px; font-weight: bold;}} 仅应用 'display: table-row'。例如,font-weight:bold 不会被应用。我在 tr 中将 css 引用为 class="tableRow tr1"
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2011-01-29
      • 1970-01-01
      • 2018-11-28
      相关资源
      最近更新 更多