温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用css伪类解决图片加载失败的样式问题

发布时间:2022-12-01 11:40:10 来源:亿速云 阅读:144 作者:iii 栏目:编程语言

本文小编为大家详细介绍“怎么用css伪类解决图片加载失败的样式问题”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用css伪类解决图片加载失败的样式问题”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

实现代码

index.html

    <h3>未做错误处理</h3>
     <div style="font-size: 0">
      <img src="./img/image.jpg" alt="" />

      <img src="./img/image-1.jpg" alt="" />
    </div>

    <h3>有错误处理</h3>
    <div style="margin-top: 20px; font-size: 0; display: flex">
      <img class="mo-image" src="./img/image.jpg" alt="标题" />
      <img class="mo-image" src="./img/image-1.jpg" alt="标题" />
    </div>

style.css

img {
  width: 300px;
  height: 150px;
  object-fit: cover;
  display: inline-block;
}

.mo-image {
  display: inline-block;
  /* transform: scale(1); */
  position: relative;
}

/* 显示占位图片 */
.mo-image::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #f5f5f5 url(../img/image-error.jpg) no-repeat center / 50% 50%;
  color: transparent;
}

/* 显示alt中的文字 */
.mo-image::after {
  content: attr(alt);
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  line-height: 2;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  font-size: 12px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

读到这里,这篇“怎么用css伪类解决图片加载失败的样式问题”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI