在scss中实现这个设计的优雅方式是什么?

时间:2017-03-16 18:54:28

标签: html css sass less

我已经在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专业人士可以帮助阐明这一点?

2 个答案:

答案 0 :(得分:3)

使用#选择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;
}

答案 1 :(得分:0)

根据评论进行修改:

<强> Codepen Scss fiddle link

Scss Structure Snippet:

#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;
    }
}

enter image description here

原始回答:

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

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;