我已经在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专业人士可以帮助阐明这一点?
答案 0 :(得分:3)
使用#
选择ID,并使用.
对于你的元素
<div id="moreThanPlus100"> </div>
要么改为
<div class="moreThanPlus100"> </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;
}
}
原始回答:
#
用于ID,它们是唯一的,因此在这种情况下,如果您希望影响具有相同CSS属性的许多html元素,则类最好(它们在它之前用点声明) #39;名称.class
)。.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;
}
&#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"> </div>
<div class="common lessThanMinus100"> </div>
<div class="common noChange"> </div>
<div class="common lessThanPlus100"> </div>
<div class="common moreThanPlus100"> </div>
</div>
<div class="tableRow3"> </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;