利用伪元素给破碎的图片添加样式

98 阅读1分钟

业务需求:公司的资源存储(图片、pdf)等上传到后端返回的是一串编码,不含有文件格式,所有无法知晓文件格式,在展示 PDF 的时候就会显示一个破碎的图片,希望给破碎图片添加一个文件样式 icon。

解决方案:利用伪元素 :after 等来解决,在 content 中添加图片等。

代码


.ant-upload-list-item-image {
  padding: 0;
  font-size: 14px;
  line-height: 16px;
  text-align: center;
  &::after {
    content: url("~src/sites/assets/image/icon.png") "\A ""\A "attr(alt);
    white-space: pre;
    font-size: 16px;
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding-top: 6px;
    background-color: #fff;
    overflow: hidden;
  }
}