代码之家  ›  专栏  ›  技术社区  ›  Run

CSS-封面图像上的渐变?

  •  5
  • Run  · 技术社区  · 9 年前

    如何在封面图像上设置渐变层?

    例如:

    header {
      position: relative;
      height: 300px;
      background-repeat: no-repeat;
      background-position: center bottom;
      background-image: url('http://www.planwallpaper.com/static/images/Free-Wallpaper-Nature-Scenes.jpg');
      background-size: cover;
      border-bottom-left-radius: 50%;
      border-bottom-right-radius: 50%;
    }
    
    h1 {
      margin: 0;
      padding: 100px 0;
      font: 44px "Arial";
      text-align: center;
    }
    
    header h1 {
      color: white;
    }
    <header>
      <h1>Header Content</h1>
    </header>
    
    <section>
      <h1>Section Content</h1>
    </section>

    background-image: linear-gradient(to bottom right, #002f4b, #dc4225);
    

    有可能吗?

    4 回复  |  直到 9 年前
        1
  •  7
  •   Gerard    9 年前

    使用 rgba 具有透明度和双重性 background-image .

    header {
      position: relative;
      height: 300px;
      background-repeat: no-repeat;
      background-position: center bottom;
      background-image: linear-gradient(to bottom right, rgba(0, 47, 75, .5), rgba(220, 66, 37, .5)), url('http://www.planwallpaper.com/static/images/Free-Wallpaper-Nature-Scenes.jpg');
      background-size: cover;
      border-bottom-left-radius: 50%;
      border-bottom-right-radius: 50%;
    }
    h1 {
      margin: 0;
      padding: 100px 0;
      font: 44px "Arial";
      text-align: center;
    }
    
    header h1 {
      color: white;
    }
    <header>
      <h1>Header Content</h1>
    </header>
    
    <section>
      <h1>Section Content</h1>
    </section>
        2
  •  7
  •   Gustaf Gunér    9 年前

    background-blend-mode 到 multiply .像这样的

    header {
      position: relative;
      height: 300px;
      background-repeat: no-repeat;
      background-position: center bottom;
      background-size: cover;
      border-bottom-left-radius: 50%;
      border-bottom-right-radius: 50%;
      background-blend-mode: multiply;
      background: linear-gradient(to bottom right, #002f4b, #dc4225), url('http://www.planwallpaper.com/static/images/Free-Wallpaper-Nature-Scenes.jpg');
      background-size: cover;
    }
    
    h1 {
      margin: 0;
      padding: 100px 0;
      font: 44px "Arial";
      text-align: center;
    }
    
    header h1 {
      color: white;
    }
    <header>
      <h1>Header Content</h1>
    </header>
    
    <section>
      <h1>Section Content</h1>
    </section>
        3
  •  4
  •   Haider Ali    9 年前

    您可以在:before或:after元素上覆盖

    header {
      position: relative;
      height: 300px;
      background-repeat: no-repeat;
      background-position: center bottom;
      background-image: url('http://www.planwallpaper.com/static/images/Free-Wallpaper-Nature-Scenes.jpg');
      background-size: cover;
      border-bottom-left-radius: 50%;
      border-bottom-right-radius: 50%;
    }
    
    header:before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-image: linear-gradient(to bottom right,#002f4b, #dc4225);
        opacity: .6; 
        border-bottom-left-radius: 50%;
        border-bottom-right-radius: 50%;
    }
    
    h1 {
      margin: 0;
      padding: 100px 0;
      font: 44px "Arial";
      text-align: center;
    }
    
    header h1 {
      color: white;
    }
    <header>
      <h1>Header Content</h1>
    </header>
    
    <section>
      <h1>Section Content</h1>
    </section>
        4
  •  1
  •   user8119116 user8119116    9 年前

    注意:您可以使用 opacity

    header {
      position: relative;
      height: 300px;
      background-repeat: no-repeat;
      background-position: center bottom;
      background-image: url('http://www.planwallpaper.com/static/images/Free-Wallpaper-Nature-Scenes.jpg');
      background-size: cover;
      border-bottom-left-radius: 50%;
      border-bottom-right-radius: 50%;
      z-index: -1;
    }
    
    h1 {
      margin: 0;
      padding: 100px 0;
      font: 44px "Arial";
      text-align: center;
    }
    
    header h1 {
      color: white;
    }
    
    div#gradient {
     position: absolute;
     top: 0;
     left: 0;
     width: 100%;
     height: 300px;
     background-image: linear-gradient(to bottom right, #002f4b, #dc422b);
     border-bottom-left-radius: 50%;
     border-bottom-right-radius: 50%;
     z-index: -1;
     opacity: 0.75;
    }
    <header>
      <div id="gradient"></div>
      <h1>Header Content</h1>
    </header>
    
    <section>
      <h1>Section Content</h1>
    </section>