2016-02-28 113 views
1

我想一个梯度添加到我的CSS背景图像。我发现了一堆关于对于具有梯度的背景下,具有图像坐在上面的其他职位,但我想图像本身是一个梯度。我尝试这样做:制作背景图像的渐变

body { 
margin: 0; 
width: 100%; 
height: 100%; 
font-family: rexlia; 
background-size: cover; 
background: -moz-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 59%, rgba(0, 0, 0, 0.65) 100%), url(cubes.jpg) no-repeat; 
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(0, 0, 0, 0)), color-stop(59%, rgba(0, 0, 0, 0)), color-stop(100%, rgba(0, 0, 0, 0.65))), url(cubes.jpg) no-repeat; 
background: -webkit-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 59%, rgba(0, 0, 0, 0.65) 100%), url(cubes.jpg) no-repeat; 
background: -o-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 59%, rgba(0, 0, 0, 0.65) 100%), url(cubes.jpg) no-repeat; 
background: -ms-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 59%, rgba(0, 0, 0, 0.65) 100%), url(cubes.jpg) no-repeat; 
background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 59%, rgba(0, 0, 0, 0.65) 100%), url(cubes.jpg) no-repeat; 

}

所有然而,这是显示未修改的图像。这个snippit实际上来自另一个帖子,显然它工作。虽然不适合我。任何人都可以对此有所了解吗?谢谢。

+1

http://www.colorzilla.com/gradient-editor/ – Martin

+0

最后一行:'来bottom'?我认为这是一个错字或水手。 – Chris

+0

@克里斯不......这对垂直梯度正确的语法。 –

回答

2

我想这是这是只有你正在使用,那么你已经省略,使html元件100%高大,以便body也可以是100%高大的代码。

html { 
 
    height: 100%; 
 
} 
 
body { 
 
    margin: 0; 
 
    width: 100%; 
 
    min-height: 100%; 
 
    background-size: cover; 
 
    background-image: linear-gradient(to bottom, rgba(255, 0, 0, .5) 0%, rgba(255, 0, 0, .5) 59%, rgba(0, 0, 255, 0.65) 100%), url(http://lorempixel.com/image_output/technics-q-c-640-480-2.jpg); 
 
}

JSfiddle Demo

+0

好了,所以我想除了我已经改成了灰色和白色做了渐变色,差异是如此微小,我甚至不能告诉,这是以前的工作。当我使用上面的默认颜色时,我可以看到它。但是就这样说来,我在这里所得到的并不是我想要的。我想要做的是褪色的图像右出什么(所以它只是变淡白色的顶部和消失......)。那可能吗?还是在图像被添加之前必须将图像格式化? – user3226170

+0

另外,我怎样才能使HTML高度100%?不会,它会根据页面内容自动设置它的大小吗?通过将body设置为100%,它将占用html块的大小? – user3226170

1

如果您删除margin:0;它的工作原理和梯度strippes图像(因为defaut保证金身体开始填充html),或添加html{height:100%;}它的工作原理。

HTML或机构背景mixe如果一起两个标签中

一些测试没有设定,所以你可以看到和理解的行为

body { 
 
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 59%, rgba(0, 0, 0, 0.75) 100%), url(http://lorempixel.com/300/200/abstract/10) center no-repeat; 
 
    background-size: cover, cover; 
 
} 
 
html:hover body { 
 
    margin: 4em; 
 
    /* gradient is repeated and takes margin value as reference to repeat itself in html background */ 
 
}

如果身体有内容或有效高度固定的,它的工作方式相同

body { 
 
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 59%, rgba(0, 0, 0, 0.75) 100%), url(http://lorempixel.com/300/200/abstract/10) center no-repeat; 
 
    background-size: cover, cover; 
 
} 
 
html:hover body { 
 
    margin: 4em; 
 
    /* gradient is repeated body's height and keeps being repetead in html background*/ 
 
    height: 100px;

如果添加HTML,身体{身高:100%;}然后身体有一个有效的高度。

html, body {height:100%;} 
 
body { 
 
    background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0, 0) 59%, rgba(0, 0, 0, 0.75) 100%), url(http://lorempixel.com/300/200/abstract/10) center no-repeat; 
 
    background-size: cover, cover; 
 
}

最后,如果你给一个背景值,以HTML,身体会在自身保持自己的背景。

html{background:lime;} 
 
body { 
 
padding:2em; 
 
    background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0, 0) 59%, rgba(0, 0, 0, 0.75) 100%), url(http://lorempixel.com/300/200/abstract/10) center no-repeat; 
 
    background-size: cover, cover; 
 
}