在模糊的背景div中取消模糊内容

时间:2019-02-08 19:56:44

标签: html css css3 background-image wallpaper

因此,我非常喜欢https://www.w3schools.com/howto/howto_css_blurred_background.asp处的这种特殊效果,您可以在其中模糊背景并显示文本。我当前遇到的问题是我想为第二个内容添加一个墙纸,而不是第一个内容。第一个内容是好的。现在,我希望我的第二个内容在滚动时位于第二个墙纸中。有点像创建视差效果,但我正在创建自己的独特网站。

这一次,我希望背景模糊(墙纸二号),同时将要放置的东西放好并聚焦。

有没有办法做到这一点?

bg-image2的{​​{1}}和h1的{​​{1}}开始,将焦点放在模糊的背景图像上。

注意: 墙纸不会显示在下面的代码段中,因为我已经在计算机的文件夹中找到了需要的图片。< / strong>

p
bg-image2

2 个答案:

答案 0 :(得分:3)

这是您要找的结果吗?

https://codepen.io/anon/pen/exVRyy

我所做的就是在bg-image2profilePicture周围创建一个div,以使bg-image2上的模糊不影响profilePicture,然后将该div设置为position:relative;,以便可以像您一样将profilePicture放在中间。

这是您的HTML编辑内容:

<div class="bg-image"></div>

<div class="bg-text">
    <h1>My name is Stackoverflow</h1>
    <p>And I am a website</p>
</div>

<div class="bg-test">
  <div class="bg-image2">

    <!-- This is the part where I want my stuff to not be blurred and focus -->

  </div>

  <div class="profilePicture">
      <img src="https://images.pexels.com/photos/1253661/pexels-photo-1253661.jpeg?cs=srgb&dl=android-wallpaper-bluhen-blume-1253661.jpg&fm=jpg" style="width: 170px; height: 170px; border-radius:50%;">
      <h1>This is a title</h1>
      <p>This is just a paragraph</p>
    </div>
</div>

和CSS:

body,
html {
  height: 100%;
}

* {
  box-sizing: border-box;
}

.bg-image {
  /* The image used */
  background-image: url("https://images.unsplash.com/photo-1507608616759-54f48f0af0ee?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80");
  /* Add the blur effect */
  filter: blur(8px);
  -webkit-filter: blur(8px);
  /* Full height */
  height: 100%;
  /* Center and scale the image nicely */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}


/* Position text in the middle of the page/image */

.bg-test {
  position:relative;
}

.bg-text {
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/opacity/see-through */
  color: white;
  font-weight: bold;
  border: 3px solid #f1f1f1;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  width: 80%;
  padding: 20px;
  text-align: center;
}

.bg-image2 {
  /* The image used */
  background-image: url("https://whatthenewsblog.files.wordpress.com/2015/03/ecl-ann.jpg");
  /* Add the blur effect */
  filter: blur(8px);
  -webkit-filter: blur(8px);
  /* Full height */
  height: 100vh;
  /* Center and scale the image nicely */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.profilePicture {
  position: absolute;
  left: 50%;
  top:50%;
  margin: 0 auto;
  transform: translate(-50%, -50%);
}

答案 1 :(得分:0)

我面临着类似的问题。经过大量搜索,我找到了理想的解决方案。 您可以使用backdrop-filter

您的代码:

body,
    html {


      height: 100%;
    }
    
    * {
        padding: 0px;
        margin: 0px;
      box-sizing: border-box;
    }
    
    .bg-image {
      /* The image used */
      background-image: url("https://images.unsplash.com/photo-1507608616759-54f48f0af0ee?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80");
      /* Add the blur effect */
      
      /* Full height */
      height: 100%;
      /* Center and scale the image nicely */
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    
    
    /* Position text in the middle of the page/image */
    
    .bg-test {
      position:relative;
    }
    
    .bg-text {
      background-color: rgb(0, 0, 0);
      /* Fallback color */
      background-color: rgba(0, 0, 0, 0.4);
      /* Black w/opacity/see-through */
      color: white;
      font-weight: bold;
      backdrop-filter: blur(8px);
      border: 3px solid #f1f1f1;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 2;
      width: 80%;
      padding: 20px;
      text-align: center;
    }
    
    .bg-image2 {
      /* The image used */
      background-image: url("https://blog.prezi.com/wp-content/uploads/2019/03/patrick-tomasso-208114-unsplash-1024x768.jpg");
      /* Add the blur effect */
      /* Full height */
      height: 100vh;
      /* Center and scale the image nicely */
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    
    .profilePicture {
      backdrop-filter: blur(8px);
      height: 100vh;
      width: 100vw;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      position: absolute;
      left: 50%;
      color: white;
      top:50%;
      margin: 0 auto;
      transform: translate(-50%, -50%);
    }
<div class="bg-image">

<div class="bg-text">
    <h1>My name is Stackoverflow</h1>
    <p>And I am a website</p>
</div>
</div>

<div class="bg-test">
  <div class="bg-image2">

    <!-- This is the part where I want my stuff to not be blurred and focus -->
    <div class="profilePicture">
      <img src="https://images.pexels.com/photos/1253661/pexels-photo-1253661.jpeg?cs=srgb&dl=android-wallpaper-bluhen-blume-1253661.jpg&fm=jpg" style="width: 170px; height: 170px; border-radius:50%;">
      <h1>This is a title</h1>
      <p>This is just a paragraph</p>
    </div>
  </div>
  
  
</div>

我知道距您提出要求已经很久了,您已经得到了答案。 希望这对其他人有用:)

我修改了cryptomothy中的代码