悬停时文字移动

时间:2018-07-16 18:11:14

标签: html css

我有一个<div>元素,里面有一个<img>元素,中间是文本。悬停<div>时,图像的不透明度发生变化。如果将图像悬停在上方,则文本将向左移动一个像素,然后再次返回。

.mainimage {
  height: 100%;
  width: 100%;
  background: black;
}

.mainimageimg {
  opacity: 0.8;
  width: 100%;
  height: 100%;
  transition: 0.3s;
}

.mainimageimg:hover {
  opacity: 1;
}

.mainimageheading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: white;
  user-select: none;
  font-size: 2.35em;
}
<div class="mainimage">
  <img class="mainimageimg" src="https://wallpaper-house.com/data/out/8/wallpaper2you_232687.jpg" draggable="false">
  <h1 class="mainimageheading">Text here</h1>
</div>

1 个答案:

答案 0 :(得分:1)

.mainimage {
    height: 100%;
    width: 100%;
    background: black;
}
.mainimageimg {
    opacity:0.8;
    width: 100%;
    height: 100%;
    transition: 0.3s;
}
.mainimage:hover .mainimageimg {
    opacity: 1;
}
.mainimage:hover .mainimageheading{
   transform: translate(-50%, -50%);
}
.mainimageheading {
    position: absolute;
    top: 50%;
    left: 50%;
    text-align: center;
    color: white;
    user-select: none;
    font-size: 2.35em;
    transition: 1s all ease;
}