【问题标题】:Mask position incorrect when I stop using a background image当我停止使用背景图像时,遮罩位置不正确
【发布时间】:2017-05-27 17:48:02
【问题描述】:

http://jsfiddle.net/4UNuB/5/为例,图片已设置为背景

.box1 {
    border: #999 2px solid;
    width: 180px;
    height: 250px;
    background-image:  url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);
    background-position: center;
    background-repeat: no-repeat;
}

但是,如果我不能使用背景图片,而是像这样在 div 本身中有一个 img src

<div class="box1"> 
    <a href="https://placehold.it"><img src="http://placehold.it/180x250"></a>
</div>

蒙版不再覆盖图像,而是位于图像下方。

看这个小提琴http://jsfiddle.net/4UNuB/6/

但是蒙版仍然像以前一样被应用到同一个地方,那么它为什么会移动,以及如何阻止它移动?

【问题讨论】:

    标签: html css


    【解决方案1】:

    为盒子添加位置绝对和相对 css。

    在此处查看Fiddle

      .box1 {
          border: #999 2px solid;
          width: 180px;
          height: 250px;
          /*background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);*/
          background-position: center;
          background-repeat: no-repeat;
          position: relative;
      }
    
    
     .black-box {
        text-align: center;
        font-size: 16px;
        color: #fff;
        background-color: rgba(00,00,00,0.8);
        width: 100%;
        height: 100%;
        opacity: 1.0;
        transition: all 0.5s ease-in-out;
        position: absolute;
        top: 0;
    }
    

    【讨论】:

      【解决方案2】:

      您可以将两者放在同一个锚点中并使用定位+ z-index:

      .box1 {
        border: #999 2px solid;
        width: 180px;
        height: 250px;
        /*background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);
        background-position: center;
        background-repeat: no-repeat;*/
      }
      
      img {
        position: absolute;
        z-index: 0;
      }
      
      .black-box {
        position: relative;
        z-index: 1;
        text-align: center;
        font-size: 16px;
        color: #fff;
        background-color: rgba(00,00,00,0.8);
        width: 100%;
        height: 100%;
        opacity: 1.0;
        transition: all 0.5s ease-in-out;
      }
      
      .black-box:hover {
        opacity: 0.0;
        transition: all 0.5s ease-in-out;
      }
      
      h2 {
        padding-top: 110px;
        margin: 0px;
      }
      <div class="box1"> 
        <a href="http://placehold.it">
          <img src="http://placehold.it/180x250">
          <div class="black-box">
            <h2>View Details</h2>
          </div>
        </a>
      </div>

      另外,我必须删除h2 的边距。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-04
        • 2021-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-06
        • 1970-01-01
        相关资源
        最近更新 更多