亲爱的, 我正在使用下面的代码来显示数据库中的图像列表,并在悬停时在图像上显示一些文本(也来自数据库)。 我需要保留当前设置,但还要添加一个文本(来自数据库),但要始终(静态)显示在图像上:
CSS:
.wrapper {
position: relative;
padding: 0;
width:100px;
display:block;
}
.text {
position: absolute;
top: 0;
color:#333;
background-color:rgba(255,255,255,0.2);
width: 500px;
font-size:18px;
height: 100%;
line-height:100px;
text-align: center;
z-index: 10;
opacity: 0;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
.text:hover {
opacity:1;
}
.img {
z-index:1;
}
.grid {
position:relative;
float:left;
width: 500px;
height:333px;
margin-bottom: 10px;
margin-right: 10px;
border: 0;
text-align:center;
vertical-align:middle;
}
代码:
foreach($dbrow as $row) {
?>
<div class="grid">
<a href="<?php echo $row['hotel_url']; ?>" class="wrapper">
<span class="text">
<b><?php echo $row['name']; ?></b>
<br/>
<?php echo $row['desc_en']; ?>
<br/>
<i>Book Now</i>
</span>
<img align="center" src="<?php echo $row['photo_url']; ?>">
</a>
</div>
<?php
}
最佳答案
您可以使用 HTML5 稍微更改您的 HTML,以便跨度保持不变(尽管我也会在不使用 br 的情况下更改它)并且图像和标题都包含在 HTML5 图形标签下。
<a href="#">
<span class="text">Hover text</span>
<figure>
<img src="#" alt="foo" width=500 height=400>
<figcaption>
Text underneath
</figcaption>
</figure>
</a>
关于php - 如何使用 CSS 同时在图像上添加静态文本和悬停文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40638533/