【问题标题】:Further Scale CSS Background Cover Property进一步缩放 CSS 背景封面属性
【发布时间】:2016-09-23 20:03:59
【问题描述】:

我有一个启动图像,它通过 CSS 设置为包含 <div> 的背景。元素样式目前设置如下:

#splash{
    background: url('image');
    background-repeat: no-repeat;
    background-size: cover;
    filter: blur(5px);
}

这一切都很好,除了filter:blur(5px) 行。这会在此处显示的图像周围留下 5 像素的“软边缘”:

Example of soft edge on image

如果我可以进一步覆盖<div> 5px,我将实现相同的模糊效果并保持锐利的边缘。

我可以重新定位图像渲染点并“作弊”,但我想知道是否有更好的方法。

谢谢。

【问题讨论】:

    标签: css css-filters


    【解决方案1】:

    我玩了一下,发现了一个甚至不需要触摸 html 的有效解决方案。它通过:before:after 堆叠两个生成的元素来工作。它确实需要您将overflow: hidden; 添加到您的div#splash 中,否则模糊图像会超出容器边界,并且还需要position: relative;position: absolute; 才能使伪元素的绝对定位和“大小”起作用。

    #splash {
      position: relative;
      overflow: hidden;
      height: 200px;
      width: 280px;
    }
    #splash:before {
      border: 5px solid white; // user the corresponding backgroundcolor of your page here
      bottom: 0;
      content: "";
      display: block;
      left: 0;
      position: absolute;
      right: 0;
      top: 0;
      z-index: 5;
    }
    #splash:after {
      background: url(http://lorempixel.com/200/280);
      background-repeat: no-repeat;
      background-size: cover;
      bottom: 0;
      content: "";
      display: block;
      filter: blur(5px);
      left: 0;
      position: absolute;
      right: 0;
      top: 0;
      z-index: 3;
    }
    <div id="splash"></div>

    http://codepen.io/anon/pen/jrBVrp

    【讨论】:

    • 谢谢,我还没来得及实施它,但我检查了你的笔。这似乎是我遇到的最好的解决方案。感谢您的帮助。
    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2010-09-27
    相关资源
    最近更新 更多