如何将css的hover中的背景图覆盖img
时间: 2023-06-01 21:04:44 浏览: 64
抱歉,我可以回答这个问题。您可以使用 ::before 或 ::after 伪元素来实现覆盖。具体代码如下:
html代码:
```
<div class="container">
<img src="your-img-url.jpg" alt="your image">
</div>
```
CSS 代码:
```
.container {
position: relative;
display: inline-block;
}
.container::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('your-hover-img-url.jpg');
background-repeat: no-repeat;
background-size: cover;
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
.container:hover::before {
opacity: 1;
}
```
这样,当鼠标悬停在图片上时,背景图就会覆盖在图片上。
相关问题
css hover显示图片
你可以使用CSS的:hover伪类来实现hover切换图片的效果。具体的做法是在:hover伪类下,通过修改元素的背景图或者content属性来切换图片。
以下是一个示例代码,展示了如何利用CSS实现hover切换图片效果:
```css
/* 默认状态下的图片 */
.img {
background-image: url("default.jpg");
width: 200px;
height: 200px;
}
/* hover状态下的图片 */
.img:hover {
background-image: url("hover.jpg");
}
```
在上述代码中,我们首先定义了一个类名为.img的元素,设置了默认状态下的背景图片为default.jpg。然后,在.img:hover伪类下,我们修改了背景图片为hover.jpg。当鼠标悬停在.img元素上时,就会触发:hover伪类,从而切换图片。
你可以根据自己的需求修改图片的路径和尺寸,以及其他样式属性。
css鼠标悬停背景图放大
以下是一个简单好看的鼠标悬停图文列表CSS3动画特效,实现了鼠标悬停图片时中间的圆形文字背景放大的效果:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS3鼠标悬停图文列表动画特效</title>
<style type="text/css">
ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
li {
margin: 20px;
position: relative;
overflow: hidden;
width: 300px;
height: 300px;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
transition: all 0.3s ease-in-out;
}
li:hover {
transform: scale(1.1);
box-shadow: 0 0 20px rgba(0, 0, 0, 0.6);
}
li img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
transition: all 0.3s ease-in-out;
}
li:hover img {
transform: scale(1.2);
}
li::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 0;
height: 0;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 50%;
transition: all 0.3s ease-in-out;
z-index: -1;
}
li:hover::before {
width: 200%;
height: 200%;
}
li h3 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin: 0;
padding: 0;
font-size: 24px;
color: #333;
transition: all 0.3s ease-in-out;
z-index: 1;
}
li:hover h3 {
color: #fff;
text-shadow: 0 0 10px rgba(0, 0, 0, 0.6);
}
</style>
</head>
<body>
<ul>
<li>
<img src="https://picsum.photos/300/300?random=1">
<h3>图片1</h3>
</li>
<li>
<img src="https://picsum.photos/300/300?random=2">
<h3>图片2</h3>
</li>
<li>
<img src="https://picsum.photos/300/300?random=3">
<h3>图片3</h3>
</li>
<li>
<img src="https://picsum.photos/300/300?random=4">
<h3>图片4</h3>
</li>
<li>
<img src="https://picsum.photos/300/300?random=5">
<h3>图片5</h3>
</li>
<li>
<img src="https://picsum.photos/300/300?random=6">
<h3>图片6</h3>
</li>
</ul>
</body>
</html>
```