如何使用 CSS 功能区生成器代码生成特定特色图片

How to use CSS Ribbon Generator code for specific featured images

我想使用来自 CSS Ribbon Generator 的这段代码,但只需要它用于某些特色图片 - 不是全部。

我有 CSS 和 HTML。目前尚不清楚我将在我的子主题中包含哪个文件 HTML 以使 CSS 工作。

我只想要某些特色图片的丝带。我见过的所有插件都已过时。

.box {
  width: 200px; height: 300px;
  position: relative;
  border: 1px solid #BBB;
  background: #EEE;
}
.ribbon {
  position: absolute;
  right: -5px; top: -5px;
  z-index: 1;
  overflow: hidden;
  width: 75px; height: 75px;
  text-align: right;
}
.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; right: -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;
}
<div class="box">
   <div class="ribbon"><span>PREMIUM</span></div>
</div>

如何添加此代码以用于特定的特色图片?我应该将 HTML 代码放入哪个文件才能使 CSS 起作用?

您可以通过将特定图像与其他图像分开来设置仅在特定图像上使用色带,然后使用 JS 仅将色带添加到满足特定条件的图像。

这里我使用featured class 将图像与其他图像分开,然后使用javascript 将ribbon class 添加到这些图像中。

let feat = document.querySelectorAll('.featured')
feat.forEach((img)=>{
 img.classList.add('ribbon')
})
.box {
  width: 200px; height: 300px;
  position: relative;
  border: 1px solid #BBB;
  background: #EEE;
}
.ribbon {
  position: absolute;
  right: -5px; top: -5px;
  z-index: 1;
  overflow: hidden;
  width: 75px; height: 75px;
  text-align: right;
}
.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; right: -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;
}
<div style="display:flex; justify-content: space-between; flex-wrap: wrap;">
<div class="box">
   <div class="featured"><span>PREMIUM</span></div>
</div>
<div class="box">
   <div class="featured"><span>PREMIUM</span></div>
</div>
<div class="box">
   <div class=""><span>PREMIUM</span></div>
</div>
<div class="box">
   <div class="featured"><span>PREMIUM</span></div>
</div>
<div class="box">
   <div class=""><span>PREMIUM</span></div>
</div>
<div class="box">
   <div class="featured"><span>PREMIUM</span></div>
</div>
</div>