css - 渐变全尺寸背景 CSS

标签 css background height width

整页 CSS3 渐变背景:

好吧,我试图让我的背景成为一个始终为 100% 宽度和高度的渐变,并且它有效,除了它上面不会显示任何内容。这是我目前拥有的代码。

CSS:

<style type="text/css" media="screen">
#bodyBg{
    background:rgb(79,143,249) no-repeat center center fixed; /* Old browsers */
    background: -moz-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%, rgba(79,143,249,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(1%,rgba(164,197,252,1)), color-stop(100%,rgba(79,143,249,1))); /* Chrome,Safari4+ */
    background: -webkit-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* Opera 12+ */
    background: -ms-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* IE10+ */
    background: radial-gradient(ellipse at center,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a4c5fc', endColorstr='#4f8ff9',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
    min-height: 100%;
    min-width: 1024px;
    width: 100%;
    height: auto;
    position: fixed;
    top: 0;
    left: 0;
    z-index:0;
}
    @media screen and (max-width: 1024px) { /* Specific to this particular image */
#bodyBg {
    left: 50%;
    margin-left: -512px;   /* 50% */
}
#body-404 {
    position: relative;
    margin: 50px auto;
    width:880px;
    text-align:center;
    color:white;
    font-family: "Helvetica Neue", "URW Palladio L", Arial, Helvetica, "Palatino Linotype";
    z-index:99;
}
</style>

HTML:

<div id="body-404">
    <img src="img/logo.png" width="100%">
    <h1><span>Sorry the page you were looking for was not found</span></h1>
    <p>If you believe you got to this page in error, please contact us via <a href="contactus.php">this page</a>.</p><br />
    <font size="120%"><a href="index.php">Return to Homepage</font>
</div>
<div id="bodyBg"></div>

演示:

http://mysteriousorigin.com/missing.html

最佳答案

#bodyBg 这样的空元素通常被认为是不好的做法。只需将背景渐变放在 html(或 body)元素上并摆脱无用的 div:

http://jsfiddle.net/2yVRA/1/

html {
    background:rgb(79,143,249) no-repeat center center fixed; /* Old browsers */
    background: -moz-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%, rgba(79,143,249,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(1%,rgba(164,197,252,1)), color-stop(100%,rgba(79,143,249,1))); /* Chrome,Safari4+ */
    background: -webkit-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* Opera 12+ */
    background: -ms-radial-gradient(center, ellipse cover,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* IE10+ */
    background: radial-gradient(ellipse at center,  rgba(164,197,252,1) 1%,rgba(79,143,249,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a4c5fc', endColorstr='#4f8ff9',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
    min-height: 100%;
}
    @media screen and (max-width: 1024px) { /* Specific to this particular image */

body-404 {
    position: relative;
    margin: 50px auto;
    width:880px;
    text-align:center;
    color:white;
    font-family: "Helvetica Neue", "URW Palladio L", Arial, Helvetica, "Palatino Linotype";
    z-index:99;
}

关于css - 渐变全尺寸背景 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14388678/

相关文章:

css - 如何将 HTML5 全屏 API 应用于 div 背景图像

javascript - 通过 cookie 的 jQuery 自定义背景

javascript - Sencha 触摸 : container height needs to fit content

css - 如何更改滚动条按钮的悬停颜色?

css - 绝对父级 - 在子内容上显示溢出

css - 背景图像大小不适用于移动设备

javascript - jQuery $(window).height 动态添加内容错误

html - css div hover 对子元素的影响不同

html - 响应式网页设计中的表格

jquery - 你如何使用 jQuery 获得全高