在卡片图像上实现CSS透明渐变叠加

时间:2016-04-26 12:15:42

标签: html css css3 materialize

我正在尝试将透明渐变覆盖到Materialise CSS框架中的图像卡。我在网上找到的共识表明,通过将img标记放入div并将渐变背景应用于所述div,可以轻松实现这一点。然后只需将z-index标记的img移到渐变后面,从而覆盖它。

但是,我遇到了这种方法的一些问题。出于某种原因,我的叠加层似乎没有透明度,这意味着只能看到渐变,而不是背后的图像。我认为它与物化框架有关,有什么方法可以解决它吗?

JSFiddle of my code

HTML:

<div class="row">
    <div class="col s12 m4 l3">
        <div class="card hoverable">
            <div class="card-image postergrad">
                <div class="postergrad">
                    <img class="poster" src="http://vignette2.wikia.nocookie.net/horrormovies/images/e/e1/28-Days-Later-Posters.jpg">
                </div>
                <span class="card-title">28 Days Later</span>
            </div>
            <div class="card-content">
                <p>Lorem ipsum...</p>
            </div>
            <div class="card-action">
                <a href="#">This is a link</a>
            </div>
        </div>
    </div>
</div>

CSS:

.postergrad { 
  background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.65) 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* IE10+ */
  background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* W3C */
  filter: progid:DXImageTransform.Microsoft.gradient(     startColorstr='#a6000000', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */
}

.poster {
  position:relative;
  z-index:-1;
  display:block;
}

1 个答案:

答案 0 :(得分:3)

我认为你误解了。

您无法在其后面的HTML中设置图片自己的包装背景。背景是背景。

您可以使用包装器创建伪元素叠加。

&#13;
&#13;
.postergrad {
  display: block;
  position: relative;
}
.postergrad::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background: -moz-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.65) 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(0, 0, 0, 0.65)), color-stop(100%, rgba(0, 0, 0, 0)));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.65) 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.65) 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.65) 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.65) 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#a6000000', endColorstr='#00000000', GradientType=0);
  /* IE6-9 */
}
.poster {
  display: block;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css" rel="stylesheet" />
<div class="row">
  <div class="col s12 m4 l3">
    <div class="card hoverable">
      <div class="card-image">
        <div class="postergrad">
          <img class="poster" src="http://vignette2.wikia.nocookie.net/horrormovies/images/e/e1/28-Days-Later-Posters.jpg">
        </div>
        <span class="card-title">28 Days Later</span>
      </div>
      <div class="card-content">
        <p>Lorem ipsum...</p>
      </div>
      <div class="card-action">
        <a href="#">This is a link</a>
      </div>
    </div>
  </div>
&#13;
&#13;
&#13;