【问题标题】:Create clip mask image src with using html/css使用 html/css 创建剪辑蒙版图像 src
【发布时间】:2018-04-26 14:49:07
【问题描述】:

我想只使用 html 和 css 创建图像掩码。不使用 svg 或背景图片。

剪辑蒙版需要使用<img src='">。没有背景图片我无法做到这一点。

.pic-mask {
    position:relative;
}
.pic-mask:before {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(63, 72, 48, 0.8);
    -moz-transition: background .3s linear;
    -webkit-transition: background .3s linear;
    -ms-transition: background .3s linear;
    -o-transition: background .3s linear;
    transition: background .3s linear;
}
<div class="pic-mask">
<img src="https://im.ezgif.com/tmp/ezgif-1-1bf1c27255.jpg" width="500">
</div>

我的预期输出如下图所示

【问题讨论】:

    标签: html css twitter-bootstrap image


    【解决方案1】:

    你必须设置图片的动态宽度和高度,试试这样:

    HTML:

    <div class="image-container">
     <div class="pic-mask">
      <img src="https://im.ezgif.com/tmp/ezgif-1-1bf1c27255.jpg">
     </div>
    </div>
    

    CSS:

    .image-container img {
      width:100%;
      height:100%;
    }
    .pic-mask {
        position:relative;
    }
    .pic-mask:before {
        content: "";
        display: block;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: rgba(63, 72, 48, 0.8);
        -moz-transition: background .3s linear;
        -webkit-transition: background .3s linear;
        -ms-transition: background .3s linear;
        -o-transition: background .3s linear;
        transition: background .3s linear;
    }
    

    如果你想限制图片的宽度,你可以在图片容器上这样做,例如:

    .image-container {
      max-width:500px;
    }
    

    它的原因是 pic-mask 是相对父元素的绝对值,而 right:0 将把它放在相对父元素右侧 0px 处,但图片本身设置为比父元素更小的宽度。

    【讨论】:

    • 对不起。不符合我的预期。你检查我上面的图像输出。
    • 哪部分行为不是预期的?
    • 你的意思是颜色不一样?您必须具体说明您的问题,是背景与图像重叠还是您无法匹配背景颜色?
    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 2013-07-20
    • 2013-06-10
    相关资源
    最近更新 更多