悬停时将背景图像更改为颜色
Changing background image to color on hover
我正在尝试在鼠标悬停在 div 上时将其背景从图像更改为颜色。我几乎完成了它,但是颜色也覆盖了 div.
中的文本
HTML:
<div>
<h3>LIPSUM DOLORES DILORES DUM DUM</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five
centuries, but also the leap into electronic typesetting, remaining
essentially unchanged.</p>
</div>
CSS:
div {
background-image: url("https://static.pexels.com/photos/127028/pexels-photo-127028.jpeg");
background-size: cover;
width: 300px;
height: 300px;
}
div:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 300px;
height: 300px;
background: rgba(0, 0, 0, 0.6);
opacity: 0;
transition: all 0.8s;
}
div:hover:before {
opacity: 1;
background: rgba(0, 0, 0, 1);
transition: all 0.8s;
}
div h3,
div p,
body {
margin: 0;
padding: 0;
}
div h3,
div p {
color: red;
z-index: 1000;
}
你可以这样做
.outer {
background-image: url("https://static.pexels.com/photos/127028/pexels-photo-127028.jpeg");
background-size: cover;
width: 300px;
height: 300px;
}
.outer:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 300px;
height: 300px;
background-color: rgba(0, 0, 0, 0.6);
opacity: 0;
transition: all 0.8s;
}
.outer:hover:before {
opacity: 1;
}
.inner{
position: relative;
}
div h3,
div p,
body {
margin: 0;
padding: 0;
}
div h3,
div p {
color: red;
z-index: 1000;
}
<div class="outer">
<div class="inner">
<h3>
LIPSUM DOLORES DILORES DUM DUM
</h3>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
</div>
创建内部容器并将其定位为相对。
我正在尝试在鼠标悬停在 div 上时将其背景从图像更改为颜色。我几乎完成了它,但是颜色也覆盖了 div.
中的文本HTML:
<div>
<h3>LIPSUM DOLORES DILORES DUM DUM</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five
centuries, but also the leap into electronic typesetting, remaining
essentially unchanged.</p>
</div>
CSS:
div {
background-image: url("https://static.pexels.com/photos/127028/pexels-photo-127028.jpeg");
background-size: cover;
width: 300px;
height: 300px;
}
div:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 300px;
height: 300px;
background: rgba(0, 0, 0, 0.6);
opacity: 0;
transition: all 0.8s;
}
div:hover:before {
opacity: 1;
background: rgba(0, 0, 0, 1);
transition: all 0.8s;
}
div h3,
div p,
body {
margin: 0;
padding: 0;
}
div h3,
div p {
color: red;
z-index: 1000;
}
你可以这样做
.outer {
background-image: url("https://static.pexels.com/photos/127028/pexels-photo-127028.jpeg");
background-size: cover;
width: 300px;
height: 300px;
}
.outer:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 300px;
height: 300px;
background-color: rgba(0, 0, 0, 0.6);
opacity: 0;
transition: all 0.8s;
}
.outer:hover:before {
opacity: 1;
}
.inner{
position: relative;
}
div h3,
div p,
body {
margin: 0;
padding: 0;
}
div h3,
div p {
color: red;
z-index: 1000;
}
<div class="outer">
<div class="inner">
<h3>
LIPSUM DOLORES DILORES DUM DUM
</h3>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
</div>
创建内部容器并将其定位为相对。