【问题标题】:Blurred portion over image, solid edges, responsive images图像上的模糊部分、实心边缘、响应式图像
【发布时间】:2015-12-12 05:58:12
【问题描述】:

我正在尝试使用纯 HTML/CSS 复制以下模糊效果。我目前的方法使用 2 张图片,原始 cover-image,然后是图片的第二个副本 blurred-bg-image 使用 CSS filter: blur(5px);

想要的效果:

source


我找不到任何方法来保持工具栏底部的高度,同时还保持background-image 等于整个封面图像的尺寸。

当父元素不是position: relative 时,overflow: hidden 不适用于子元素。但如果父级是相对的,则内部 blurred-bg-imagecover-image

的尺寸不同

这是基本设置:

<div class="cover-image">
    <div class="toolbar">
        <div class="blurred-bg-image"></div>
    </div>
</div>

到目前为止,我能找到的唯一解决方案是在 blurred-bg-image 上使用 clip rect(),然后计算将其剪辑到的位置。但是,这没有响应性,并且包含 JS。

【问题讨论】:

标签: html css css-filters


【解决方案1】:

Here is a CodePen by the OP 显示最终结果。

方法

您可以使用方法described here on CSS Tricks

此方法利用以下内容:

  • 绝对定位
  • 转换
  • 一张图片作为背景和模糊效果

您必须使用媒体查询进行调整,但这并不难。我看到的唯一主要缺点是您必须在工具栏内容上设置固定高度,因为该高度用于转换。但同样,这很容易通过媒体查询完成。

源代码和演示请参见下面的 sn-p。我在 CSS 中放了一些 cmets。

html,
body {
    width: 100%;
    height: 100%;
}

.cover-image {
    position: relative;
    max-width: 1860px;
    width: 100%;
    max-height: 560px;
    height: 100%;
    overflow: hidden;
    background-color: #005FE5;
}

.toolbar {
    position: absolute; /* put .toolbar at the bottom of .cover-image */
    bottom: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    overflow: hidden; /* keep pseduo-element from breaking out */
    -webkit-transform: translateY(100%) translateY(-10rem); /* translate down all the way, then back up by height of .toolbar-content */
    -moz-transform: translateY(100%) translateY(-10rem);
    -ms-transform: translateY(100%) translateY(-10rem);
    -o-transform: translateY(100%) translateY(-10rem);
    transform: translateY(100%) translateY(-10rem);
}

/* the background will be the same for both elements but we will blur the pseudo-element later */
.cover-image,
.toolbar::before {
    background-image: url("https://dl.dropboxusercontent.com/s/3vzuc6vmfito1zg/austin-cityscape-night-hdr-1.jpg?dl=0");
    background-repeat: no-repeat;
    background-position: center bottom;
    background-size: cover; /* scales the background accordingly */
}

/* use this pseudo-element for the blur effect */
.toolbar::before {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    -webkit-transform: translateY(-100%) translateY(10rem); /* translate inversely to what we translated .toolbar */
    -moz-transform: translateY(-100%) translateY(10rem);
    -ms-transform: translateY(-100%) translateY(10rem);
    -o-transform: translateY(-100%) translateY(10rem);
    transform: translateY(-100%) translateY(10rem);
    -webkit-filter: blur(10px); /* finally! the blur effect */
    filter: blur(10px);
}

.toolbar-content {
    position: relative;
    height: 10rem; /* use this value in the transforms */
    color: #FFF;
}

.toolbar-content ul {
    position: absolute;
    top: 50%;
    left: 5%;
    margin: 0;
    padding: 0;
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transform: translateY(-50%);
    list-style: none;
}

.toolbar-title {
    color: #A6BFC9;
    text-transform: uppercase;
}

.edit-profile {
    position: absolute;
    top: 50%;
    right: 5%;
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transform: translateY(-50%);
    -webkit-appearance: none;
    background-color: #00A9F3;
    border: none;
}

@media only screen and (max-width: 66.25rem) {
    .toolbar-content ul li {
        margin-bottom: 0.25rem;
    }

    .toolbar-title,
    .toolbar-detail {
        display: inline-block;
    }

    .toolbar-title::after {
        content: ":";
    }
}

@media only screen and (min-width: 66.3125em) {
    .toolbar {
        -webkit-transform: translateY(100%) translateY(-6.25rem);
        -moz-transform: translateY(100%) translateY(-6.25rem);
        -ms-transform: translateY(100%) translateY(-6.25rem);
        -o-transform: translateY(100%) translateY(-6.25rem);
        transform: translateY(100%) translateY(-6.25rem);
    }

    .toolbar::before {
        -webkit-transform: translateY(-100%) translateY(6.25rem);
        -moz-transform: translateY(-100%) translateY(6.25rem);
        -ms-transform: translateY(-100%) translateY(6.25rem);
        -o-transform: translateY(-100%) translateY(6.25rem);
        transform: translateY(-100%) translateY(6.25rem);
    }

    .toolbar-content {
        height: 6.25rem;
    }

    .toolbar-content ul li {
        display: inline-block;
        padding: 0.625rem 1.25rem;
        text-align: center;
    }

}
<div class="cover-image">
    <div class="toolbar">
        <div class="toolbar-content">
            <ul>
                <li>
                    <div class="toolbar-title">Edad</div>
                    <div class="toolbar-detail">20 años</div>
                </li>
                <li>
                    <div class="toolbar-title">Cumpleaños</div>
                    <div class="toolbar-detail">8 de septiembre de 1994</div>
                </li>
                <li>
                    <div class="toolbar-title">Primera Conexión</div>
                    <div class="toolbar-detail">14 de enero de 2009</div>
                </li>
                <li>
                    <div class="toolbar-title">Klout</div>
                    <div class="toolbar-detail">87</div>
                </li>
                <li>
                    <div class="toolbar-title">Twitter</div>
                    <div class="toolbar-detail">1.806</div>
                </li>
                <li>
                    <div class="toolbar-title">Facebook</div>
                    <div class="toolbar-detail">345</div>
                </li>
            </ul>
            <button class="edit-profile" type="button">Editar perfil</button>
        </div>
    </div>
</div>
<div class="some-other-content">
    <p>You can add more content here</p>
    <p>You can add more content here</p>
    <p>You can add more content here</p>
    <p>You can add more content here</p>
    <p>You can add more content here</p>
    <p>You can add more content here</p>
    <p>You can add more content here</p>
    <p>You can add more content here</p>
    <p>You can add more content here</p>
</div>

【讨论】:

  • 我会接受这个答案,尽管我做了一些调整以满足我自己的个人需求。主要变化是使模糊工具栏具有负位置值(顶部等),以便模糊边缘明确且清晰,而不是像本示例中那样褪色。我还添加了深蓝色背景叠加层,因为 just 本身模糊会很奇怪。 Here is my fully updated example,它使用 Bootstrap 3 和 Font Awesome。您能否在答案的顶部提供这支笔,以便其他人可以看到我所做的补充?
  • @FengHuo 正常图像和模糊图像在您的 Pen 上没有正确对齐。
  • 啊,你不是在开玩笑,我在将 Sass 和 ERB 更改为 HTML/CSS 时一定漏掉了一个细节。
  • 有一个重复的 background-position 被导出并通过 ERB 变量覆盖。现在一切都好。
  • @FengHuo 太棒了,很高兴你能成功!边缘褪色很烦人。为您的笔添加了链接。
【解决方案2】:

今天解决了一段时间。这是我得到的:

glass.html

<html><head><link rel="stylesheet" type="text/css" href="glass.css"/></head>
<body>
    <div id="bkgrd">
        <div class="blur-bkgrd-position cropper flip ">
            <div class="blur-bkgrd-position glass flip">

            </div>
        </div>
    </div>
</body>

glass.css

#bkgrd{
    position:absolute;          /*align very top left */
    top:0;                      /*align very top left */
    left:0;                     /*align very top left */
    width: 100%;                /* full screen for background cover/contain */
    padding-top: 56.25%;        /* helps "bkgrd-size contain" stretch to full width by breaking height limit */
    /*image*/
    background-image: url(yourbackground.jpg);
    background-size: contain;  /*responsive width-wise, no js */
    background-repeat: no-repeat;
    overflow: hidden;
}
.blur-bkgrd-position {
    position:absolute;          
    top:50%;                                                   /*sets up cut off point*/
    left:0;                     /*align very left */
    width: 100%;                /* full screen for background cover/contain */
    padding-top: 56.25%;        /* helps "bkgrd-size contain" stretch to full width by breaking height limit */
}
.glass {
    /*blurred image*/
    background: 
        /* dark blue */ 
        linear-gradient(
          rgba(0, 0, 30, 0.45), 
          rgba(0, 0, 30, 0.45)
        ),
        url(yourbackground.jpg);
    background-size: contain;   /*responsive width-wise, no js */
    background-repeat: no-repeat;
    background-position: center bottom.
    -webkit-filter: blur(5px);
    -moz-filter: blur(5px);
    -o-filter: blur(5px);
    -ms-filter: blur(5px);
    filter: blur(5px);
}
.cropper {
   overflow: hidden;           /* performs the cropping */
}
/* apply to both .cropper and .glass */  /* enables crop from the top */
.flip {
  -webkit-transform: rotate(180deg);
  -moz-transform:    rotate(180deg);
  -ms-transform:     rotate(180deg);
  -o-transform:      rotate(180deg);
  transform:         rotate(180deg);
}

基本上,我们得到了两个相同的背景图像,除了一个具有色调和模糊效果,并且被一个倒置的裁剪器包裹。

代码笔:http://codepen.io/vtange/pen/MajweX 有用链接:https://css-tricks.com/crop-top/

【讨论】:

  • 酷。你会把工具栏放在哪里?
  • 我更新了 Codepen 以包括工具栏的去向以及正文的其余部分可以继续的地方。工具栏进入主背景 div 以粘贴它,但避免了模糊的 div,因为在这种情况下它也会被模糊。
  • 屏幕变化时,内容似乎没有停留在工具栏内。
【解决方案3】:

我已使用位于工具栏顶部的::before 将背景图像添加到工具栏。图像区域和伪元素高度应使用基于根的单位,例如vhrem,因此无论容器大小如何,它们的大小都是相同的。此外,伪元素背景设置与主背景相同。伪元素被模糊,多余的背景被工具栏的overflow: hidden移除。

在此Fiddle demo 中调整结果面板的大小。

html, body {
    margin: 0;
    padding: 0;
}

.top-image {
    position: relative;
    min-height: 100px;
}

.top-image, .toolbar::before {
    height: 50vh; /** the image and the blurred area height **/
    background: url('http://www.theplanningboardroom.net/wp-content/uploads/2011/06/sydney-city-buildings.jpg') no-repeat;
    background-size: cover;
}
/** the .toolbar::before is position at the bottom of the toolbar, so excess height goes up **/
 .toolbar, .toolbar::before {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
}
.toolbar {
    z-index: 1;
    /** height can also be percentage such as 20% **/
    height: 100px;
    /** hide the rest of the background ::before **/
    overflow: hidden;
    
    /** some styling for the text and controls **/
    box-sizing: border-box;
    padding: 10px;
    text-align: center;
    
}

.toolbar::before {
    z-index: -1;
    display: block;
    /** the height is full screen height **/
    -webkit-filter: blur(5px);
    filter: blur(5px);
    content:'';
}
<div class="main">
    <div class="top-image">
        <div class="toolbar">
            text and controls
        </div>
    </div>
    
    <div>
        <p>The content</p>
    </div>
</div>

如果您不希望工具栏位于图像的边缘,您可以将其放置在您希望在其中的任何位置。只需将工具栏的leftrightbottom.toolbar::before 属性设置为负值(demo):

.toolbar {
    position: absolute;
    right: 100px;
    bottom: 50px;
    left: 100px;
}

.toolbar::before {  
    position: absolute;
    right: -100px;
    bottom: -50px;
    left: -100px;
}

【讨论】:

  • 如果您不希望图像跨越整个视口,则此方法无效。
  • @divinecomedian - 谢谢。不知怎的,我错过了其余的内容:)
  • 这是一个很好的解决方案,但它依赖于指定的vh。我希望背景图像(.top-image 内部)根据里面的内容而变化。我尝试使用height: 100% 和上/下/左/右定位来获得.toolbar-before 以匹配.top-image 的完整尺寸,但它最终成为.toolbar 的尺寸,即使.toolbar 是绝对定位的。我很难过,这种方法感觉 soooo 很接近 :(
  • 此解决方案基于通过vhrem 具有相同尺寸的图像和模糊区域。如果背景图像没有设置高度,@divinecomedian 解决方案看起来很合适。
  • 他的解决方案接近,但不太合适。采取this simple codepen example。它说明.child 对象不是.grandparent 的高度,它最终是.parent 的高度,而实际上我希望它看起来像this image,浅蓝色方块是完整的.grandparent 的高度。
【解决方案4】:

我不知道你为什么在这样做时遇到问题,我可以通过使用另一个 div 来做到这一点,将它完全扩展到工具栏的宽度和高度,然后设置相同的 background-image 然后模糊了它。

这是代码-

html,
body {
  margin: 0;
  padding: 0;
}
#container {
  width: 100%;
  background: black url('http://tinyurl.com/pgfnxag') bottom center no-repeat;
  background-size: 100% auto;
  position: relative;
  overflow: hidden;
  color: white;
  font-size: 30px;
  text-align: center;
}
#glass {
  background-color: rgba(255, 255, 255, 0.25);
  padding: 35px 0;
  width: 100%;
  color: white;
  position: absolute;
  bottom: 0;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}
#blurred {
  position: absolute;
  width: 100%;
  background: url('http://tinyurl.com/pgfnxag') bottom center no-repeat;
  background-size: 100% auto;
  filter: blur(5px);
  -webkit-filter: blur(5px);
  -moz-filter: blur(5px);
  -o-filter: blur(5px);
  -ms-filter: blur(5px);
  top: 0;
  left: 0;
  height: 100%;
}
#content {
  z-index: 999;
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  font-size: 20px;
  font-weight: 100;
  transform: translate(-50%, -50%);
  font-family: Segoe UI;
  text-align: center;
}
<div id="container">This is content above blurred part... Lorem ipsum dolor sit amet... and after that so more contents can go here..
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <div id="glass">
    <div id="blurred"></div>
    <div id="content">Here goes the content..</div>
  </div>
</div>

Fiddle demo#glassheight 一起玩并编辑#content 中的内容,#blurreddiv 持有相同的background-image。这里真正的技巧是将所有背景属性(background-sizebackground-position 等)设置为与#container 完全相同。

更新:更改为background-size: 100% auto;,现在适用于容器或视口的任何heightwidth

编辑:删除了所有height 属性,现在它完全响应了!运行上面的代码片段。

【讨论】:

  • 您的解决方案无法响应,请尝试移动窗口并观察两个图像不匹配。此外,添加filter 供应商前缀以获得更好的Fiddle
  • @FengHuo 检查我更新的答案现在效果很好!只是改为使用background-size: 100% auto; 而不是cover
  • 这种方法的另一个我以前没有意识到的问题是它依赖于height: 70vh。我想要模糊工具栏上方的内容,然后希望整个对象的高度基于里面内容的大小。
  • 不,这不是真的,它不依赖于height: 70vh 我只是用它来帮助您检查响应能力,即使您调整窗口的高度,检查这个小提琴 - jsfiddle.net/qw222wuh/4
  • @FengHuo 检查这个小提琴 - jsfiddle.net/qw222wuh/5 我没有在容器上定义任何高度,它仍然根据内容调整其高度,并且模糊也可以响应......
【解决方案5】:

Backdrop filters 可以做到这一点,但目前只能在带有 -webkit- 前缀的 Safari 中工作(如果您启用“实验性 Web 平台功能”,则可以在 Chrome 中使用)。

.toolbar{
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
}

更多信息here

body {
  background: url(https://farm2.staticflickr.com/1551/25178575880_1449360954_k_d.jpg);
  background-size: cover;
  padding: 0;
  margin: 0;
}
.box {
  border-bottom: 1px solid rgba(0, 0, 0, 0.5);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  padding: 10px;
  color: #fff;
  font: 24px Arial, sans-serif;
  background: rgba(255, 255, 255, 0.25);
}
<div class="box">
  test box
</div>

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 2015-03-26
    • 2023-03-17
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多