【问题标题】:Split CSS style by color for Corner ribbon code按颜色拆分角功能区代码的 CSS 样式
【发布时间】:2018-02-05 18:30:20
【问题描述】:

我正在尝试通过添加颜色属性来拆分代码 作为一般规则,我会使用选择器扩展

.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>

编辑 sn-p 添加代码,工作,但我认为不是最好的方法。

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

【问题讨论】:

  • 你不知道如何模拟跨度?不知道这意味着什么。一种可能的方法是告诉 span,他的哥哥 div 更有才华。
  • 我不太确定你在这里想要达到什么目的。您能否在 Photoshop 中制作图片或绘画以显示想要的结果?
  • 不确定你想要达到什么目的......你能改写模拟句子吗?你想得到像.ribbon.mycolor span 这样的东西吗?目前尚不清楚您要从当前代码中拆分出什么
  • 我已经编辑了自己的问题,但我认为这不是最好的方法,因为所有文档的 span 标签都会受到影响。

标签: css css-shapes linear-gradients


【解决方案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>

【讨论】:

  • 多年来没有 webkit 浏览器需要供应商前缀。
  • @Rob 当然,OP 代码的简单复制粘贴 :) 将会更新
  • 代码来自cssportal.com/css-ribbon-generator,经过修改,与Bulma一起使用。谢谢。我已经删除了 webkit 浏览器和编译 sass,并且测试和工作正常,用户:root 用于声明颜色渐变。
猜你喜欢
  • 2012-11-30
  • 1970-01-01
  • 2013-04-28
  • 2011-08-15
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
相关资源
最近更新 更多