鼠标悬停时更改图像不透明度

cssweb developmentfront end technology更新于 2025/11/27 14:04:17

使用 opacity 属性和 :hover 选择器来更改鼠标悬停时的不透明度。以下代码用于更改鼠标悬停时图像的不透明度 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
body{
   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.transparent{
   width:270px;
   height:200px;
   opacity: 1;
   transition: 0.3s;
}
.transparent:hover{
   opacity: 0.3;
}
</style>
</head>
<body>
<h1>Image opacity on hover example</h1>
<img class="transparent" src="https://i.picsum.photos/id/505/800/800.jpg" >
<h2>Hover over the above image to change its opacity</h2>
</body>
</html>

输出

上述代码将产生以下输出 −


相关文章


有用资源