html - 尽管z-Index,鼠标悬停图像显示推送文本
问题描述
我已经设置了代码,因此当您将鼠标悬停在 H2 上时,会显示图像。假设,因为文本是 z-index:2 而图像是 z-index:1,所以 H2 文本应该保持固定。但是,在鼠标悬停时,文本仍在向下移动以为图像腾出空间。
我需要文本保持固定在同一位置,并且背景图像只在悬停时出现而不轻推 h2。
你可以在这里查看测试: http ://www.rorywolfseydel.com/test3-2
h2 {
line-height: 68px !important;
text-transform: uppercase;
letter-spacing: 2px;
font-size: 80px;
font-weight: 0 !important;
color: #ffffff;
z-index: 12;
}
.artisthover {
display: none
}
h2.two:hover img {
display: block;
z-index: -1;
position: relative;
margin-top: -200px;
margin-left: -250px
}
h2.two a {
color: #ffffff;
}
h2.three:hover img {
display: block;
z-index: -1;
position: relative;
margin-top: -200px;
margin-right: -250px
}
h2.three a {
color: #ffffff;
}
<center>
<h2 class="two">
<a href="http://lawnyavawnya.com/2018/artists/absolutelyfree">ABSOLUTELY FREE</a>
<img src="http://lawnyavawnya.com/2018/2019artists/absolutelyfree.jpg" class="artisthover" width="500px">
</h2>
</center>
<center>
<h2 class="three">
<a href="http://lawnyavawnya.com/2018/artists/badgeepoqueensemble">BADGE EPOQUE ensemble</a>
<img src="http://lawnyavawnya.com/2018/2019artists/badgeepoque.jpg" class="artisthover" width="500px">
</h2>
</center>
解决方案
<!DOCTYPE html>
<html>
<head>
<style>
.artisthover {
display: none
}
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
h2.two:hover img {
display:inline ;
}
h2.three:hover img {
display:inline ;
}
</style>
</head>
<body>
<center>
<h2 class="two">
<a href="http://lawnyavawnya.com/2018/artists/absolutelyfree">ABSOLUTELY FREE</a>
<img src="http://lawnyavawnya.com/2018/2019artists/absolutelyfree.jpg" class="artisthover" width="500px">
</h2>
</center>
<center>
<h2 class="three">
<a href="http://lawnyavawnya.com/2018/artists/badgeepoqueensemble">BADGE EPOQUE ensemble</a>
<img src="http://lawnyavawnya.com/2018/2019artists/badgeepoque.jpg" class="artisthover" width="500px">
</h2>
</center>
</body>
</html>
好吧,要在图像上设置 z-index,位置必须是绝对的,图像才能保持不变,并且外部元素的显示是内联的......
推荐阅读
- mysql - 如何通过比较标签列表以降序查找具有最佳匹配标签的项目?
- javascript - 使用 discord.js 中的命令禁用机器人
- javascript - 在 WordPress 中异步加载 CSS 文件
- css - 图像从网格和尺寸问题中消失
- reactjs - 由于 HTTPS 问题 MERN 无法登录 Facebook
- c - gcc 和 clang 给出不同的结果
- javascript - 在下一个 js 中为个人资料页面定义登录用户路由?
- javascript - 如何获得包含字符串和数字的数组中的平均数
- php - 如何一起使用 laravel 分页 & makeHidden 和收集方法
- python - Py2Exe:将python文件转换为可执行文件后相机不起作用