css 实现标签

113 阅读1分钟

1、斜角标签

思路:

  • 标签相对外层容器绝对定位
  • 把标签倾斜45°(transform: rotate(-45deg);)
  • 外层容器设置超出的内容隐藏(overflow: hidden;)
<div class='conBg'>
    <div class='tag'> 补货补货 </div>
</div>

<style>
.conBg {
  width: 400px;
  height: 200px;
  background-color: #fff;
  margin: 10px 0;
  overflow: hidden;
  position: relative;
  border: 1px solid lightgray;
  border-radius: 4px;
}

.tag {
  background-color: lightgray;
  color: white;
  height: 25px;
  line-height: 25px;
  text-align: center;
  position: absolute;
  padding: 0 40px;
  min-width: 80px;
  transform: rotate(-45deg);
  top: 25px;
  left: -45px;
}
</style>

image.png

2、箭头标签

思路: 标签由三部分组成,长方形、箭头、圆点

<div class='tagText'> 补货补货 </div>

<style>
//长方形
.tagText {
  display: inline-block;
  font-size: 10px;
  color: #fff;
  background-color: #04303d;
  height: 20px;
  line-height: 20px;
  min-width: 20px;
  padding: 4px 10px 4px 12px;
  position: relative;
  border-radius: 0 3px 3px 0;
  margin-left: 23px;
}
//三角形
.tagText:before {
  content: " ";
  width: 0px;
  height: 0px;
  position: absolute;
  top: 0;
  left: -28px;
  border: 14px solid transparent;
  border-right-color: #04303d;
}
//圆点
.tagText:after {
  content: " ";
  width: 4px;
  height: 4px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 12px;
  left: 3px;
}
</style>

image.png