鼠标悬停的图像:透明框的大小与图像不同



我有一些代码在带有文本叠加的图像上添加了透明框的悬停效果,但是透明框没有捕捉到图像的大小。透明框似乎太小(让下面的图像的顶部和底部不透明)或太大(导致我的页面完全不对齐)。这可能是盒子尺寸问题?我想我已经设置了它,以便它应该自动变为图像大小的 100%,但我可能错过了一些东西。

链接到我的网站:http://jchambliss.aisites.com/imd311/portfolio/sites.html

此处包含必要的代码:

#preview {
  max-width: 100%;
  border-collapse: collapse;
}
#text {
  position: absolute;
  opacity: 0;
  max-width: 100%;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}
/* Web Design Projects */
.sitesAds {
  position: relative;
  border-collapse: collapse;
}
.sitesAds:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
.sitesTokyo {
  position: relative;
  border-collapse: collapse;
}
.sitesTokyo:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
.sitesMobile {
  position: relative;
  border-collapse: collapse;
}
.sitesMobile:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
.sitesRoux {
  position: relative;
  border-collapse: collapse;
}
.sitesRoux:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
<!DOCTYPE html>
<html>
<head>
<link href="portfolio.css" rel="stylesheet" type="text/css">
    <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
        </script>
    <![endif]-->
    
<link href="https://fonts.googleapis.com/css?family=Amita|Encode+Sans+Expanded|Leckerli+One|Merienda+One|Source+Sans+Pro" rel="stylesheet">
</head>
<body>
<div>
<table>
<tr>
   <td><a href="ads.html"><div class="sitesAds"><img src="images/ads.jpg" id="preview" alt="Banner Ads"/>
	   <h3 id="text">Banner Ads</h3></div></a>
   </td>
   <td><a href="tokyo.html"><div class="sitesTokyo"><img src="images/tokyo.jpg" id="preview" alt="One Page Ad Site: Tokyo, Japan"/>
	   <h3 id="text">One Page Ad Site: Tokyo, Japan</h3></div></a>
   </td>
</tr>
<tr>
   <td><a href="mobile.html"><div class="sitesMobile"><img src="images/mobile.jpg" id="preview" alt="Mobile App: Tea Shoppe"/>
	   <h3 id="text">Mobile App: Tea Shoppe</h3></div></a>
   </td>
   <td><a href="roux.html"><div class="sitesRoux"><img src="images/roux.jpg" id="preview" alt="Roux Academy of Art, Media, and Design"/>
	   <h3 id="text">Roux Academy of Art, Media, and Design</h3></div></a>
   </td>
</tr>
</table>
	</div>
</body>
</html>

img上使用width: 100%而不是max-width &从#text中删除padding: 0

确保应为每个h3元素使用唯一的 id s,因为单个页面上的 id 不能相同

例:

.sitesTokyo img {
  width: 100%;
}
#text {
  position: absolute;
  display: flex;
  align-items: center; /* For vertical centering */
  justify-content: center; /* For horizontal centering */
  opacity: 0;
  max-width: 100%;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: 0; /* Remove padding */
  padding: 0; /* Remove margin */
}

另请查看下面的工作片段:

#preview {
  max-width: 100%;
  border-collapse: collapse;
}
#text {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  max-width: 100%;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: 0;
  padding: 0;
}
/* Web Design Projects */
.sitesAds {
  position: relative;
  border-collapse: collapse;
}
.sitesAds:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
.sitesTokyo {
  position: relative;
  border-collapse: collapse;
}
.sitesTokyo:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
.sitesMobile {
  position: relative;
  border-collapse: collapse;
}
.sitesMobile:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
.sitesRoux {
  position: relative;
  border-collapse: collapse;
}
.sitesRoux:hover #text {
  box-sizing: content-box;
  padding: 60px 0;
  opacity: 0.8;
  color: #663366;
  background: #FFFFFF;
  text-decoration: none;
  text-align: center;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms; 
}
<!DOCTYPE html>
<html>
<head>
<link href="portfolio.css" rel="stylesheet" type="text/css">
    <!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
        </script>
    <![endif]-->
    
<link href="https://fonts.googleapis.com/css?family=Amita|Encode+Sans+Expanded|Leckerli+One|Merienda+One|Source+Sans+Pro" rel="stylesheet">
</head>
<body>
<div>
<table>
<tr>
   <td><a href="ads.html"><div class="sitesAds"><img src="http://placehold.it/200x200" id="preview" alt="Banner Ads"/>
	   <h3 id="text">Banner Ads</h3></div></a>
   </td>
   <td><a href="tokyo.html"><div class="sitesTokyo"><img src="http://placehold.it/200x200" id="preview" alt="One Page Ad Site: Tokyo, Japan"/>
	   <h3 id="text">One Page Ad Site: Tokyo, Japan</h3></div></a>
   </td>
</tr>
<tr>
   <td><a href="mobile.html"><div class="sitesMobile"><img src="http://placehold.it/200x200" id="preview" alt="Mobile App: Tea Shoppe"/>
	   <h3 id="text">Mobile App: Tea Shoppe</h3></div></a>
   </td>
   <td><a href="roux.html"><div class="sitesRoux"><img src="http://placehold.it/200x200" id="preview" alt="Roux Academy of Art, Media, and Design"/>
	   <h3 id="text">Roux Academy of Art, Media, and Design</h3></div></a>
   </td>
</tr>
</table>
	</div>
</body>
</html>

希望这有帮助!

问题出在您的图像大小上。您悬停在上面的div 比图像大。您必须将图像宽度设置为 100%;

.sitesMobile img { width: 100%}

最新更新