html - 在图像前显示 'SALE' 标签

标签 html css

我想在图像前显示一个“SALE”标签,我尝试使用“before”伪元素来做到这一点,但屏幕上似乎没有显示任何内容。

我正在尝试在黑色背景的圆圈内创建“SALE”标签。

下面是我用过的代码

<span class"bag-image">
  <img src="https://images-na.ssl-images- 
amazon.com/images/I/71lDa7EbWSL._UY395_.jpg" class="image">
</span>

.bag-image::before{

background-color: #red;
content: 'SALE';
border-radius: 500px;
display: inline-block;
width: 100px;
height: 100px;

}

视觉引用:

LIKE THIS

https://codepen.io/anon/pen/rgLPdp

最佳答案

  1. 创建 bag-image 类 position: relative;
  2. 制作 bag-image:before position: absolute; 并将其定位在顶部/左侧或边距位置,并将行高设置为 SALE 文本垂直居中。

你可以给伪类一个较低的z-index,这样只有上半部分是可见的,例如z-index: -1;

你可以使用 margin-top: -2.5em; margin-left: 175px; 在伪代码中定位它。

div.bag-image {
  display: inline-block;

  /* just so that we  can see in the example */
  margin-top: 3em;   
}

div.bag-image:before {
  position: absolute;
  background-color: #ff0000;
  content: 'SALE';
  text-align: center;
  color: #ffffff;
  margin-top: -2.5em;
  margin-left: 175px;

  /* optionally make it a circle */
  border-radius: 9999px;
  height: 3em;
  padding: 1em;
  line-height: 3em;
}

/* just for clarity */
img.image {
  border: 1px solid #ccc;
}
<div class="bag-image">
  <img src="https://images-na.ssl-images-amazon.com/images/I/71lDa7EbWSL._UY395_.jpg" class="image">
</div>

关于html - 在图像前显示 'SALE' 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103292/

相关文章:

html - 在 HTML 中设置新的预呈现或预取的正确方法是什么?

html - 响应式表格和显示 flex(Bootstrap)

javascript - 位置 : Fixed is disabling my scroller in IE7

javascript - 移动标题不可点击

html - Zurb Foundation - 具有固定标题和固定第一列和最后一列的响应表

javascript - 在复选框 onclick 上调用 javascript

javascript - HTML 5 灰度滤镜不起作用

java - 如何在没有 UiBinder 的情况下将 CellTable.Style 与 GWT 2.7.0 一起使用?

javascript - 在不清除整个 Canvas 的情况下替换函数?

javascript - Queryselector 返回 null,不知道为什么