html - 图像叠加上不需要的填充底部?

标签 html css layout padding

我在图像类中添加了一个填充底部,并希望在图像上有一个悬停叠加层,但它超出了图像,包括添加的填充。有没有办法确保悬停只跨越图像宽度和高度而没有填充?谢谢!

.work-image {
  width: 100%;
  height: 100%;
  position: relative;
  padding-bottom: 15px;
  vertical-align: bottom;
}
.work-image img {
  width: 100%;
}

.work-image:after, .work-image:before {
  opacity: 0;
  position: absolute;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
}
.work-image:after {
  top: 0;
  left: 0;
  width: 100%;
  height:100%;
  content: '\A';
  background:rgba(0,0,0,0.6);
}
.work-image:before {
  top: 50%;
  z-index: 1;
  width: 100%;
  color: #fff;
  text-align: center;
  transform: translateY(-50%);
  content: attr(data-content);
}

.work-image:hover:after, .work-image:hover:before {
  opacity:1;
}
<div data-content="Here is a caption" class="work-image">
  <img src="http://i65.tinypic.com/2l8w0hc.jpg" alt="" />
</div>

<div data-content="Here is a caption" class="work-image">
  <img src="http://i64.tinypic.com/2zodetx.jpg" alt="" />
</div>

最佳答案

vertical-align: bottom;.work-image 移动到 .work-image img,并替换 padding- bottommargin-bottom

.work-image {
  width: 100%;
  height: 100%;
  position: relative;
  margin-bottom: 15px;      
}
.work-image img {
  width: 100%;
  vertical-align: bottom;
}

.work-image:after, .work-image:before {
  opacity: 0;
  position: absolute;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
}
.work-image:after {
  top: 0;
  left: 0;
  width: 100%;
  height:100%;
  content: '\A';
  background:rgba(0,0,0,0.6);
}
.work-image:before {
  top: 50%;
  z-index: 1;
  width: 100%;
  color: #fff;
  text-align: center;
  transform: translateY(-50%);
  content: attr(data-content);
}

.work-image:hover:after, .work-image:hover:before {
  opacity:1;
}
<div data-content="Here is a caption" class="work-image">
  <img src="http://i65.tinypic.com/2l8w0hc.jpg" alt="" />
</div>

<div data-content="Here is a caption" class="work-image">
  <img src="http://i64.tinypic.com/2zodetx.jpg" alt="" />
</div>

关于html - 图像叠加上不需要的填充底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45197762/

相关文章:

php - 谷歌地图地理定位不请求许可

html - 将图标居中放置在元素边框的顶部

css - 在 Eclipse-Java EE 上,如何更改 TreeView 中选定元素的颜色设置?

jquery - 数据表中红色的负值

css - 我如何使用在 ejs 中使用 render 传递的变量作为 css 内联样式值添加?

html - 将 flexbox 用于布局和菜单/其他子组件的良好做法?

javascript - 在 html 选项卡中管理长文本 block

javascript - 在 JavaScript 函数中获取 HTML DOM 元素

apache-flex - 如何防止 itemRenderer 中的容器超出列表容器的宽度?

Qt 和 Qt Designer - 使小部件填充父级而不填充