模糊部分覆盖图像,实心边缘,响应图像

时间:2015-09-15 20:38:07

标签: html css css-filters

我尝试使用纯HTML / CSS复制以下模糊效果。我当前的appraoch使用了2张图片,原始的封面图片,然后是使用CSS filter: blur(5px);的图片 blur-bg-image 的第二个副本。

期望效果:

enter image description here source

我无法找到任何方法将底部保持为工具栏的高度,同时保留background-image等于整个封面图片的尺寸。< / p> 当父级除了overflow: hidden之外,

position: relative不会处理子元素。但如果父亲是亲戚,则内部 blur-bg-image 封面图片

的尺寸不同

以下是基本设置:

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

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

5 个答案:

答案 0 :(得分:3)

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

方法

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

此方法使用以下内容:

  • 绝对定位
  • 变换
  • 背景和模糊效果的一个图像

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

有关源代码和演示,请参阅以下代码段。我在CSS中添加了一些注释。

&#13;
&#13;
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;
    }

}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

今天捣乱了一会儿。这是我得到的:

<强> 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);
}

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

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

答案 2 :(得分:1)

我已使用位于工具栏顶部的a<- letters[seq( from = 1, to = 10 )] b<- letters[seq( from = 6, to = 15 )] common = "f" "g" "h" "i" "j" unique_a = "a" "b" "c" "d" "e" unique_b = "k" "l" "m" "n" "o" 将背景图片添加到工具栏中。图像区域和伪元素高度应使用基于根的单位,例如::beforevh,因此无论其容器大小如何,它们都将具有相同的大小。另外,伪元素背景设置与主背景的设置相同。伪元素模糊,使用工具栏的rem删除多余的背景。

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

overflow: hidden
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>leftright以及bottom属性设置为负值({​​{3}}):

.toolbar::before

答案 3 :(得分:0)

我不知道为什么你这样做会遇到问题,我可以通过使用另一个div将其完全展开并且高度等于工具栏然后设置相同的{{1然后模糊了它。

这是代码 -

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;
}

Fiddle demo<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>的{​​{1}}一起使用并编辑height中的内容,#glass#content持有#blurred { {1}}。这里的真正诀窍是将所有背景属性(divbackground-image等)设置为与background-size完全相同。

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

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

答案 4 :(得分:0)

Backdrop filters可以执行此操作,但目前仅在具有-webkit-前缀的Safari中使用(如果启用“实验性网络平台功能”,则使用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>