【问题标题】:Unwanted Padding-Bottom on Image Overlay?图像叠加层上不需要的填充底部?
【发布时间】:2017-12-25 03:36:34
【问题描述】:

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

.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>

【问题讨论】:

    标签: html css layout padding


    【解决方案1】:

    vertical-align: bottom;.work-image移动到.work-image img,并将padding-bottom替换为margin-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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 2014-08-28
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多