我有一些代码在文本覆盖的图像上添加了透明框的悬停效果,但是透明框没有捕捉到图像的大小。透明盒子似乎太小(下面的图像的顶部和底部不透明)或太大(导致我的页面完全失去对齐)。这可能是一个盒子大小问题?我想我已将它设置为自动变为图像大小的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>
答案 0 :(得分:0)
问题在于图像尺寸。你悬停的div比图像大。您必须将图像宽度设置为100%;
.sitesMobile img { width: 100%}
答案 1 :(得分:0)
在width: 100%
上使用 img
代替max-width
&amp;从padding: 0
#text
确保您应为每个
id
元素使用唯一的h3
,因为在单个页面上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>
希望这有帮助!