图像上的模糊部分、实心边缘、响应式图像

Blurred portion over image, solid edges, responsive images

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

想要的效果:

source


我找不到任何方法来保持工具栏底部的高度,同时还保持 background-image 等于整个 cover-image[=40= 的尺寸].

当父元素不是 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。

我使用 ::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;
}

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

方法

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

此方法利用以下内容:

  • 绝对定位
  • 变换
  • 一张图片用于背景和模糊效果

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

有关源代码和演示,请参见以下代码段。我在 CSS.

中发表了一些评论

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>

今天闲逛了一会儿。这是我得到的:

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 有帮助link:https://css-tricks.com/crop-top/

我不知道你为什么在这样做时遇到问题,我可以通过使用另一个 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 属性,现在它完全响应了! 运行 上面的代码片段。

Backdrop filters 可以做到这一点,但目前只能在带有 -webkit- 前缀的 Safari 中工作(如果启用 "Experimental Web Platform Features",则为 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>