css如何给图片加上文字和遮罩

本文详细介绍了如何使用HTML和CSS代码创建一个宣传栏,通过子绝父相布局结合绝对和相对定位,以及伪类元素和RGBA技术,实现在鼠标悬停时的动态效果和透明背景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

鼠标未选中

鼠标选中

直接上html代码

<!-- 宣传栏 -->
    <div class="billboards">
      <img src="./images/images1/bgimg1.png" alt="" />
      <div class="mask"></div>
      <div class="world">
        <p class="p1">包图教育</p>
        <p class="p2">专注教育20年</p>
        <p class="p3">
          精品UI界面创意ICON、界面、BANNER,这里应有尽有!精品UI界面创意ICON界面、BANNER,这里应有尽有!
        </p>
        <div><a href="">点击查看</a></div>
      </div>
    </div>

css代码

.billboards{
  position: relative;
  height: 46vw;
}
.billboards img{
  position: absolute;
  width: 100%;
}
.billboards .world{
  position: absolute; 
  margin: 0 36vw;
}
.billboards .world .p1{
  text-align: center;
  margin-top: 12vw;
  font-size: 7vw;
  color: #fff;
}
.billboards .world .p2{
  margin-top: 2vw;
  text-align: center;
  font-size: 3vw;
  color: #fff;
}
.billboards .world .p3{
  margin-top: 3vw;
  width: 29vw;
  height: 3vw;
  font-size: 1vw;
  color: #fff;
  line-height: 2vw;
  text-align: center;
  font-weight: lighter;
}
.billboards .world div{
  display: flex;
  width: 16vw;
  height:5vw;
  margin: 3vw auto;
  background-color:white;
  align-items: center;
  justify-content: center;
}
.billboards .world div a{
  text-align: center;
  color: #fecf3b;
  font-size: 3vw;
}
.billboards .mask{
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 46vw;
  background-color: rgba(0,0,0,0.5);
}
.billboards:hover .mask{
  display: block;
}

整体思路就是运用子绝父相布局将整个大盒子设置为相对定位,图片和文字设置为绝对定位,再用伪类元素设置鼠标悬停效果,用rgba元素实现透明效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值