全屏背景颜色CSS3

7

enter image description here我有一个渐变作为背景颜色,但是它只是重复出现。我想让渐变延伸到整个屏幕,这样就不可能缩小并看到任何白色空间或重复的渐变。

这是我的body的css:

body {
text-align:center;
background: rgb(238,238,238); /* Old browsers */
background: -moz-linear-gradient(top,  rgba(238,238,238,1) 0%, rgba(89,89,89,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(238,238,238,1)), color-stop(100%,rgba(89,89,89,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  rgba(238,238,238,1) 0%,rgba(89,89,89,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  rgba(238,238,238,1) 0%,rgba(89,89,89,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  rgba(238,238,238,1) 0%,rgba(89,89,89,1) 100%); /* IE10+ */
background: linear-gradient(to bottom,  rgba(238,238,238,1) 0%,rgba(89,89,89,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eeeeee', endColorstr='#595959',GradientType=0 ); /* IE6-9 */
}

你尝试过设置 html { height: 100%; } 吗? - hsan
@hsan,当我缩小视图时它可以工作,但是现在当我放大视图时它会重复渐变。我将上传一张图片,这样你就可以看到现在发生了什么。 - Lawrence Smith
4个回答

13

尝试在 body 上设置高度和宽度,同时尝试使用 background-attachment

html, body
{
    width: 100%;
    height: 100%;
}
body
{
    …
    width: 100%;
    height: 100%;
    background-attachment: fixed;
}

3
我将您的代码粘贴到测试页面上,它似乎正在按照您想要的方式运行。如果有问题,您可以尝试添加 background-size: 100%;
希望能帮到您。

我现在已经发布了放大后的样子。这段代码不能正常工作。 - Lawrence Smith

0

以下方法对我有效:

body
{
...
background-size: cover;
}

0
晚回答对我有用,使用了css
:not(:root):fullscreen::backdrop{
    background: var(--bg-body);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接