老师帮忙检查一下作业谢谢!
来源:2-9 自由编程
寿限无的丸子
2022-03-01 22:15:30
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; } .box8{ margin: 0 auto; width: 400px; height: 400px; position: relative; overflow: hidden; } .box8 img{ width: 400px; height: 400px; } .box8 p{ position: absolute; top: 400px; left: 0; width: 400px; padding-left: 20px; height: 50px; line-height: 50px; background-color: rgba(0, 0, 0, .5); transition:top 1s linear 0s; color: white; } .box8:hover p{ top: 350px; } </style> </head> <body> <div class="box8 box"> <img src="images/b3.jpg"> <p>这是一张图片</p> </div> </body> </html>
老师我有个问题,能不能给img添加hover属性实现最终效果,如果给它添加是不是得给它转块呢?我自己试了一下不大行。
1回答
好帮手慕小李
2022-03-02
同学你好,代码可以实现需求,很棒!直接给img添加hover是可以的,但是需要使用到兄弟选择器如:img:hover+p。另hover属性建议要作用在父级元素上,因为在实际开发中涉及的元素过多那么就会出现很多多余的代码量,另按照同学的需求,那么使用js也是可以实现的。
祝学习愉快!
相似问题
回答 1
回答 1
回答 1
回答 1
回答 1