上的 css 渐变背景拉伸至 100% 宽度和高度
所以我想要一个填充 100% 网页背景的渐变。对于无法处理它的浏览器,纯色就可以了。
这是我当前的CSS:
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
padding: 0;
background-repeat: no-repeat;
background: #afb1b4; /* Old browsers */
background: -moz-linear-gradient(top, #afb1b4 0%, #696a6d 100%) no-repeat; /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#afb1b4), color-stop(100%,#696a6d)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#AFB1B4', endColorstr='#696A6D',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* W3C */
}
当页面内容很少时,它似乎可以正常工作,但是当我用更多内容、导航等填充页面时,现在底部有一些白色。也许100px左右。我这样做错了吗?我需要在某处抵消一些填充吗?
So I'd like to have a gradient that fills 100% of the background of a webpage. For browsers that can't handle it, a solid color is fine.
here is my current css:
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
padding: 0;
background-repeat: no-repeat;
background: #afb1b4; /* Old browsers */
background: -moz-linear-gradient(top, #afb1b4 0%, #696a6d 100%) no-repeat; /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#afb1b4), color-stop(100%,#696a6d)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#AFB1B4', endColorstr='#696A6D',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, #afb1b4 0%,#696a6d 100%); /* W3C */
}
It seemed to work out while the page had little content, but as I've filled out the page with more content, navigation, etcetera, there is now some white at the bottom. maybe 100px or so. Am I doing this wrong? Do I need to be offsetting some padding somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除您的
height: 100%
声明。看起来将高度设置为 100% 只是将其设置为视口的 100%,而不是页面本身的 100%。Get rid of your
height: 100%
declarations. It seems like setting the height to 100% just sets it to 100% of the viewport, not actually 100% of the page itself.这是我的评论的扩展,使用 SVG 而不是供应商前缀和专有扩展。这减少了 CSS 的大小,并且通过使用一些巧妙的策略,可以允许您使用单个 SVG 文件作为渐变的精灵包(减少 HTTP 请求的总数)。
首先创建您的 SVG 文件和渐变(根据您的问题规范):
接下来,这是您的新声明:
现在,如果您想使用 png 图像支持旧版浏览器,您可以进行一点小小的更改。由于任何使用
url()
的属性都不支持提示(如@font-face
的src
属性),因此您必须更改规则一点。如果你想变得愚蠢疯狂,你可以对 SVG 文件进行 Base64 编码,这样你就不必从服务器下载另一个文件,然后将其添加为要重用的类(防止在多个位置重新粘贴 Base64)。
然后更新您的
body
标记以包含.svg-sprite
类。Here's an expansion of my comment to use SVG instead of vendor prefix and proprietary extensions. This reduces the size of the CSS and, with the employment of some ingenius tactics, can allow you to use a single SVG file as a sprite pack for gradients (reducing the total number of HTTP requests).
First create your SVG file and gradient (per your question specs):
Next, here's your new declaration:
Now, if you want to support older browsers with png image, you can with one little change. Since any property that uses
url()
does not support hinting (like@font-face
'ssrc
property), you have to alter the rule a little.If you want to get stupid crazy, you could base64encode the SVG file so that you don't have to download another file from the server then add it as a class to be reused (prevent repasting the base64 in multiple place).
Then update your
body
tag to include the.svg-sprite
class.我还发现在末尾添加“固定”似乎可以解决问题:
I also found that adding 'fixed' to the end seemed to do the trick: