html - 悬停时文本移动

标签 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>

最佳答案

.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;
}

关于html - 悬停时文本移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51367817/

相关文章:

javascript - 在不使用禁用属性的情况下使输入字段看起来已禁用

html - IE7 奇怪的背景位置行为

html - 表 TD 在使用空白 : nowrap 时仍然换行

css - 具有固定宽度/高度比的流体图像

html - 使用 HTML Colspan 时的 CSS 单元格边框

javascript - 限制路由到 Express 和 Nodejs 中的静态文件

html - 简单的 CSS 查询 : linking. .. 并请求教程

css - 如何使用 CSS 调整图像以适应窗口宽度和高度

html - 即使窗口调整大小时也将图像保持在特定位置

javascript - 更改高度适用于我的两个输入字段,但不适用于其他输入字段。为什么?