CSS -渐变覆盖图片?

时间:2022-03-25 03:52:33

How can I have a gradient layer over a cover image?

如何在覆盖图像上设置渐变层?

For instance:

例如:

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>

I want this gradient over that image:

我想要这个图像的梯度:

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

Is it possible?

是可能的吗?

4 个解决方案

#1


4  

Use rgba with transparency and double background-image.

使用rgba透明度和双重背景图像。

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


6  

You can define multiple backgrounds and then set background-blend-mode to multiply. Something like this

您可以定义多个背景,然后设置背景切换模式为乘法。像这样的东西

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  

You could put an overlay on the :before or :after elements

你可以在: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  

Note: You can tweak with the opacity to change the strength of gradient.

注意:可以调整不透明度来改变渐变的强度。

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>

#1


4  

Use rgba with transparency and double background-image.

使用rgba透明度和双重背景图像。

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


6  

You can define multiple backgrounds and then set background-blend-mode to multiply. Something like this

您可以定义多个背景,然后设置背景切换模式为乘法。像这样的东西

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  

You could put an overlay on the :before or :after elements

你可以在: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  

Note: You can tweak with the opacity to change the strength of gradient.

注意:可以调整不透明度来改变渐变的强度。

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>