具有悬停效果的图像(链接)

时间:2018-03-28 10:19:20

标签: html css hover

我是一名学生,也是编码新手。我有一个图像,并希望演员姓名出现在图像的中心,作为可点击的链接显示为悬停效果。 This is what I want it to look like! 我尝试了上面链接中列出的代码,但名称不会显示为链接。 Here's a screenshot因为它只是我计算机上的一个文件。

我已经包含了我将使用的10张图片中的两张的编码。

HTML -

<div class="pics-container">
<a href="https://en.wikipedia.org/wiki/Jason_Bateman">
<img src="pics/Jason.jpg" alt="Jason Bateman" class="image" border="2px";>
</a><div class="middle"><div class="name">Jason Bateman</div></div>

<div class="pics-container">
<a href="https://en.wikipedia.org/wiki/Rachel_McAdams">
<img src="pics/Rachel.jpg" alt="Rachel McAdams" class="image" border="2px";>
</a><div class="middle"><div class="name">Rachel McAdams</div></div>

CSS -

.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 38%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%)
}
.pics-container {
    position: relative;
    display: flex;
    width: 50%;
    padding: 20px;
}
.pics-container:hover .image {
  opacity: 0.3;
}

.pics-container:hover .middle {
  opacity: 1;
}

.name {
  background-color: #4CAF50;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}

这是我研究并试图将代码放在一起。此外,添加填充后,图像现在是阶梯式的,而不是排成一行。

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <style>
    .image {
    opacity: 1;
    display: block;
    width: 100%;
    height: auto;
    transition: .5s ease;
    backface-visibility: hidden;
    }
    .middle {
    transition: .5s ease;
    opacity: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%)
    }
    .img_hover:hover .image {
    opacity: 0.3;
    }
    .img_hover:hover .middle {
    opacity: 1;
    }
    .text {
    background-color: #4CAF50;
    color: white;
    font-size: 16px;
    padding: 16px 32px;
    }
    </style>
  </head>
  <body>
    <br><br>
    <div class="container">
      <div class="row">
        <div class="col-sm img_hover">
          <img src="your_image" alt="Avatar" class="image" style="width:100%">
          <div class="middle">
            <div class="text">John Doe</div>
          </div>
        </div> 
        <div class="col-sm img_hover">
          <img src="your_image" alt="Avatar" class="image" style="width:100%">
          <div class="middle">
            <div class="text">John Doe</div>
          </div>
        </div>
        <div class="col-sm img_hover">
          <img src="your_image" alt="Avatar" class="image" style="width:100%">
          <div class="middle">
            <div class="text">John Doe</div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

答案 1 :(得分:-1)

在此名称之外添加锚标记

<a href="#"><div class="text">John Doe</div></a>