代码之家  ›  专栏  ›  技术社区  ›  Flimm D. Ben Knoble

如何以渐变方式淡出背景图像,以便看到其下面的元素?

css
  •  0
  • Flimm D. Ben Knoble  · 技术社区  · 8 年前

    假设我有一个 div 有一只小猫的背景图片。如何在渐变模式中淡出背景图像,而不淡出div的内容?

    我还想确保通过逐渐淡出的背景图像可以看到下面的任何元素,我不希望它只是淡出为白色。

    例子:

    下面是一些示例html代码,我将如何用类淡出元素的背景图像 fade-out-background 是吗?

    <div style="background-image: url('https://i.stack.imgur.com/jypCc.jpg')">
        <div class="fade-out-background" style="background-image: url('https://i.stack.imgur.com/BCbFO.jpg')">
            This text should not fade out with the background image.
        </div>
    </div>
    

    以下是我的最终结果:

    Desired result

    资源: link to kitten image link to pattern image 是的。

    1 回复  |  直到 8 年前
        1
  •  -1
  •   Edunikki    8 年前

    这是有可能的,但这实在是太老套了:

    HTML格式:

    <head>
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <link type="text/css" href="css/main.css" rel="stylesheet">
        </head>
        <body><div class="whole-page">
          <div class="content-area-bg1"></div>
          <div class="content-area-bg2"></div>
          <div class="content-area-bg3"></div>
          <div class="content-area-bg4"></div>
          <div class="content-area-bg5"></div>
          <div class="content-area-bg6"></div>
          <div class="content-area-bg7"></div>
          <div class="content-area-bg8"></div>
          <div class="content-area-bg9"></div>
          <div class="content-area-bg10"></div>
          <div class="content-area"><h1>This text should<br>not fade out with<br>the background<br>image</h1></div>
        </div>
    

    CSS:

    :root{
    
      --textcolor-1: #fff;
    
      }
    
    html{
    
      font-size: 62.5%;
      box-sizing: border-box;
    
    }
    
    *{
    
      margin: 0;
      padding: 0;
    
    }
    
    *, *::before, *::after{
    
      box-sizing: inherit;
    
    }
    
    body {
    
    background-image: url("../kittenbg.jpg");
    font-family: 'Open Sans', sans-serif;
    font-weight: 400;
    line-height: 1.6rem;
    font-size: 1.25rem;
    color: var(--textcolor-1);
    
    }
    
    .whole-page{
    
    width: 100%;
    margin: 0;
    padding: 0;
    
    }
    
    .header{
    
    height: 7rem;
    text-align: center;
    
    }
    
    h1 {
    
      font-size: 4rem;
      line-height: 4.8rem;
    
    }
    
    .content-area-bg1
    {
    
      width: 408px;
      height: 287px;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg1::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: 0;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -10;   
    }
    
    .content-area-bg2
    {
    
      width: 408px;
      height: 260px;
      top: -287;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg2::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -9;   
    }
    
    .content-area-bg3
    {
    
      width: 408px;
      height: 230px;
      top: -547;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg3::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -8;   
    }
    
    
    .content-area-bg4
    {
    
      width: 408px;
      height: 200px;
      top: -777;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg4::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -7;   
    }
    
    .content-area-bg5
    {
    
      width: 408px;
      height: 170px;
      top: -977;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg5::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -6;   
    }
    
    .content-area-bg6
    {
    
      width: 408px;
      height: 140px;
      top: -1147;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg6::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -5;   
    }
    
    .content-area-bg7
    {
    
      width: 408px;
      height: 110px;
      top: -1287;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg7::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -4;   
    }
    
    .content-area-bg8
    {
    
      width: 408px;
      height: 80px;
      top: -1397;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg8::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -3;   
    }
    
    .content-area-bg9
    {
    
      width: 408px;
      height: 50px;
      top: -1477;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg9::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: .25;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -2;   
    }
    
    .content-area-bg10
    {
    
      width: 408px;
      height: 20px;
      top: -1527;
      display: block;
      position: relative;
    
    }
    
    .content-area-bg10::after {
      content: "";
      background: url("../kitten.jpg");
      opacity: 1;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      position: absolute;
      z-index: -1;   
    }
    
    .content-area
    {
    
      width: 408px;
      height: 287px;
      top: -1547;
      display: block;
      position: relative;
      padding: 1rem;
    
    }