我试图通过添加颜色属性来拆分代码 作为一般规则,我会使用选择器扩展
.ribbon.mycolor {
css.tag1: value_tage1;
...
}
但它不起作用,因为部分代码是跨度,我不知道在这种情况下如何模拟它。
.box {
width: 200px;
height: 300px;
position: absolute;
border: 1px solid #BBB;
background: #EEE;
}
.ribbon {
position: absolute;
left: -5px;
top: -5px;
z-index: 1;
overflow: hidden;
width: 75px;
height: 75px;
text-align: right;
background-color: transparent;
border-color: transparent;
}
.ribbon span {
font-size: 10px;
font-weight: bold;
color: #FFF;
text-transform: uppercase;
text-align: center;
line-height: 20px;
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
width: 100px;
display: block;
background: #79A70A;
background: linear-gradient(#F70505 0%, #8F0808 100%);
box-shadow: 0 3px 10px -5px rgba(0, 0, 0, 1);
position: absolute;
top: 19px;
left: -21px;
}
.ribbon span::before {
content: "";
position: absolute;
left: 0px;
top: 100%;
z-index: -1;
border-left: 3px solid #8F0808;
border-right: 3px solid transparent;
border-bottom: 3px solid transparent;
border-top: 3px solid #8F0808;
}
.ribbon span::after {
content: "";
position: absolute;
right: 0px;
top: 100%;
z-index: -1;
border-left: 3px solid transparent;
border-right: 3px solid #8F0808;
border-bottom: 3px solid transparent;
border-top: 3px solid #8F0808;
}
// Edited with partial solution
.red span {
background: linear-gradient(#F70505 0%, #8F0808 100%);
}
.red span::before {
border-left-color: #8F0808;
border-top-color: #8F0808;
}
.red span::after {
border-right-color: #8F0808;
border-top-color: #8F0808;
}
.blue span {
background: linear-gradient(#2989d8 0%, #1e5799 100%);
}
.blue span::before {
border-left-color: #1e5799;
border-top-color: #1e5799;
}
.blue span::after {
border-right-color: #1e5799;
border-top-color: #1e5799;
}
.orange span {
background: linear-gradient(#F79E05 0%, #8F5408 100%);
}
.orange span::before {
border-left-color: #8F5408;
border-top-color: #8F5408;
}
.orange span::after {
border-right-color: #8F5408;
border-top-color: #8F5408;
}

<div class="box">
<div class="ribbon orange"><span>PROFESIONAL</span></div>
</div>
&#13;
编辑片段添加代码,工作,但我认为不是最佳方式。
.red span {background: linear-gradient(#F70505 0%, #8F0808 100%);}
.red span::before {border-left-color: #8F0808; border-top-color: #8F0808;}
.red span::after {border-right-color: #8F0808; border-top-color: #8F0808;}
.blue span {background: linear-gradient(#2989d8 0%, #1e5799 100%);}
.blue span::before {border-left-color: #1e5799; border-top-color: #1e5799;}
.blue span::after {border-right-color: #1e5799; border-top-color: #1e5799;}
.orange span {background: linear-gradient(#F79E05 0%, #8F5408 100%);}
.orange span::before {border-left-color: #8F5408; border-top-color: #8F5408;}
.orange span::after {border-right-color: #8F5408; border-top-color: #8F5408;}
答案 0 :(得分:1)
我猜您正在寻找变量,您可以使用新类轻松更改功能区的颜色,而无需更改CSS:
:root {
--color-1: #8F0808;
--color-2: #F70505;
}
.box {
width: 180px;
height: 200px;
position: relative;
border: 1px solid #BBB;
background: #EEE;
display:inline-block;
}
.ribbon {
position: absolute;
left: -5px;
top: -5px;
z-index: 1;
overflow: hidden;
width: 75px;
height: 75px;
text-align: right;
background-color: transparent;
border-color: transparent;
}
.ribbon span {
font-size: 10px;
font-weight: bold;
color: #FFF;
text-transform: uppercase;
text-align: center;
line-height: 20px;
transform: rotate(-45deg);
width: 100px;
display: block;
background: linear-gradient(var(--color-2) 0%, var(--color-1) 100%);
box-shadow: 0 3px 10px -5px rgba(0, 0, 0, 1);
position: absolute;
top: 19px;
left: -21px;
}
.ribbon span::before {
content: "";
position: absolute;
left: 0px;
top: 100%;
z-index: -1;
border-left: 3px solid var(--color-1);
border-right: 3px solid transparent;
border-bottom: 3px solid transparent;
border-top: 3px solid var(--color-1);
}
.ribbon span::after {
content: "";
position: absolute;
right: 0px;
top: 100%;
z-index: -1;
border-left: 3px solid transparent;
border-right: 3px solid var(--color-1);
border-bottom: 3px solid transparent;
border-top: 3px solid var(--color-1);
}
.orange {
--color-1: #8F5408;
--color-2: #F79E05;
}
.blue {
--color-1: #1e5799;
--color-2: #2989d8;
}
<div class="box">
<div class="ribbon"><span>PROFESIONAL</span></div>
</div>
<div class="box">
<div class="ribbon orange"><span>PROFESIONAL</span></div>
</div>
<div class="box">
<div class="ribbon blue"><span>PROFESIONAL</span></div>
</div>