2017-10-07 135 views
-2

我希望我的背景图像具有从左(粉红色)到右(蓝色)而不是从上到下的线性渐变颜色。我怎样才能做到这一点?背景图像上的线性渐变

代码:

body{ 
    background-image:linear-gradient(rgba(198, 115, 115, 0.5),rgba(47, 52, 172, 0.5)),url(people.jpg); 
    background-repeat: no-repeat; 
    background-size: cover; 
    height:100vh; 
} 
+0

使用最新的和常规梯度语法:'背景图像:线性梯度(向右,RGBA(198,115,115 ,0.5),rgba(47,52,172,0.5)),url(people.jpg); ' –

+0

@ G-Cyr不要在评论中回答问题:“回答问题或为现有答案提供备用解决方案;相反,发布实际答案(或编辑以扩展现有答案);” https://stackoverflow.com/help/privileges/comment – Jesus

+0

@RyanEarnshaw当它看起来像一个错字或者只是拼写错误的语法时,如果这真的值得真正的答案,它永远不会是显而易见的。不可能在大多数时间评估OP的知识或疲倦...... :) –

回答

0

下面是简单的例子,随便问,如果有什么不太明白:

html, body { 
 
    width: 100%; 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
.bg-img { 
 
    width: 100%; 
 
    height: 100%; 
 
    background: url("http://unsplash.it/1200x800") center center no-repeat; 
 
    background-size: cover; 
 
} 
 
.bg-img:before { 
 
    content: ''; 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    background-image: linear-gradient(to bottom right, #002f4b, #dc4225); 
 
    opacity: .8; 
 
}
<div class="bg-img"></div>

0

您需要在添加to right您梯度线

就像这样:

body{ 
 
    background-image:linear-gradient(to right, rgba(198, 115, 115, 0.5),rgba(47, 52, 172, 0.5)),url(people.jpg); 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    height:100vh; 
 
}

您可以了解从本评论here

0

演示:看https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient

official on w3c

线性渐变信达x是:

<linear-gradient> = linear-gradient(
[ [ <angle> | to <side-or-corner> ] ,]? 
<color-stop>[, <color-stop>]+ 
) 

<side-or-corner> = [left | right] || [top | bottom]

的第一个参数的函数指定渐变线,这使梯度的方向,并且确定颜色挡块如何定位。它可能被省略;如果是这样,它默认为'到底部'。

body{ 
 
    background-image:linear-gradient(to right,rgba(198, 115, 115, 0.5),rgba(47, 52, 172, 0.5)),url(http://lorempixel.com/600/400/people); 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    height:100vh; 
 
}

0

body{ 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    height:100vh; 
 
    background-image: linear-gradient(to bottom, rgba(198, 115, 115, 0.5) ,rgba(47, 52, 172, 0.5) 100%),url(https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png); 
 

 
}