<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>搞代码-CSS文字覆盖图像悬停效果-CSS应用实例(gaodaima.com)</title> <style> .container { position: relative; width: 50%; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; top: 0; bottom: 0; left: 0; right: 0; height: 100%; width: 100%; opacity: 0; transition: .5s ease; background-color: #008CBA; } .container:hover .overlay { opacity: 1; } .text { color: white; font-size: 20px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); } </style> </head> <body> <h2>淡入效果</h2> <p>鼠标移动到图片上查看效果</p> <div class="container"> <img src="http://img.gaodaima.com/paperimg_gaodaima_chengxuyuan.jpg" alt="Avatar" class="image"> <div class="overlay"> <div class="text">Hello World, Tihs is gaodaima site</div> </div> </div> </body> </html>
本文案例为大家展示了《CSS文字覆盖图像悬停效果》的代码,若要看代码效果请大家点击“运行代码”按钮。
[code]<!DOCTYPE html> <html> <head> <meta charset=”utf-8″> <title>搞代码-CSS文字覆盖图像悬停效果-CSS应用实例(gaodaima.com)</title> <style> .container { position: relative; width: 50%; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; top: 0; bottom: 0; left: 0; right: 0; height: 100%; width: 100%; opacity: 0; transition: .5s ease; background-color: #008CBA; } .container:hover .overlay { opacity: 1; } .text { color: white; font-size: 20px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); } </style> </head> <body> <h2>淡入效果</h2> <p>鼠标移动到图片上查看效果</p> <div class=”container”> <img src=”http://img.gaodaima.com/paperimg_gaodaima_chengxuyuan.jpg” alt=”Avatar” class=”image”> <div class=”overlay”> <div class=”text”>Hello World, Tihs is gaodaima site</div> </div> </div> </body> </html>[/code]