在body上设置的CSS3渐变背景不会拉伸而是重复?

问题描述 投票:0回答:13

好吧,

<body>
内的内容总共 300 像素高。

如果我使用

<body>
-webkit-gradient
 设置我的 
-moz-linear-gradient

的背景

然后我最大化我的窗口(或者只是使其高于 300 像素),渐变将恰好为 300 像素高(内容的高度),然后重复填充窗口的其余部分。

我假设这不是一个错误,因为它在 webkit 和 gecko 中都是相同的。

但是有没有办法让渐变拉伸以填充窗口而不是重复?

css gradient
13个回答
845
投票

应用以下 CSS:

html {
    height: 100%;
}
body {
    height: 100%;
    margin: 0;
    background-repeat: no-repeat;
    background-attachment: fixed;
}

编辑:在每个评论的正文声明中添加了

margin: 0;
Martin)。

编辑:在每个评论的主体声明中添加了

background-attachment: fixed;
Johe Green)。


184
投票

关于之前的答案,如果内容需要滚动,将

html
body
设置为
height: 100%
似乎不起作用。在背景中添加
fixed
似乎可以解决这个问题 - 不需要
height: 100%;

例如:

body {
  background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#cbccc8)) fixed;
}


45
投票

我知道我参加聚会迟到了,但这里有一个更可靠的答案。

您需要做的就是使用

min-height: 100%;
而不是
height: 100%;
,您的渐变背景将扩展 content 的整个高度而不重复,即使内容是可滚动的。

像这样:

html {
    min-height: 100%;
}

body {
    background: linear-gradient(#b5e48c, #457b9d);
}

还有第二种解决方案。

正如其他人所说,将值

fixed
添加到
background
声明中,将使渐变延伸到 viewport 的整个高度。

像这样:

body {
    background: linear-gradient(#b5e48c, #457b9d) fixed;
}

当然,您仍然需要在

min-height: 100%;
中声明
html

这是 CodePen 中的演示,您可以在其中使用两种解决方案:https://codepen.io/ricardozea/pen/abwGBmz?editors=1100


15
投票

这就是我为解决这个问题所做的...它将显示内容全长的渐变,然后简单地回退到背景颜色(通常是渐变中的最后一种颜色)。

   html {
     background: #cbccc8;
   }
   body {
     background-repeat: no-repeat;
     background: #cbccc8;
     background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#cbccc8));
     background: -moz-linear-gradient(top, #fff, #cbccc8);
     filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#cbccc8');
   }
<body>
  <h1>Hello world!</h1>
</body>

我已经在 FireFox 3.6、Safari 4 和 Chrome 中对此进行了测试,对于由于某种原因不支持 HTML 标记样式的任何浏览器,我将背景颜色保留在正文中。


14
投票

设置

html { height: 100%}
可能会对 IE 造成严重破坏。 Here's an example (png). 但你知道什么效果最好吗?只需在
<html>
标签上设置背景即可。

html {
  -moz-linear-gradient(top, #fff, #000);
  /* etc. */
}

背景延伸至底部,不会发生奇怪的滚动行为。您可以跳过所有其他修复。这得到了广泛的支持。我还没有找到不允许您将背景应用于 html 标记的浏览器。它是完全有效的 CSS,并且已经存在了一段时间了。 :)


12
投票

此页面上有很多部分信息,但不完整。这就是我所做的:

  1. 在此处创建渐变:http://www.colorzilla.com/gradient-editor/
  2. 在 HTML 而不是 BODY 上设置渐变。
  3. 使用“background-attachment:fixed;”修复 HTML 上的背景
  4. 关闭 BODY 的顶部和底部边距
  5. (可选)我通常会创建一个
    <DIV id='container'>
    ,将所有内容放入其中

这是一个例子:

html {  
  background: #a9e4f7; /* Old browsers */
  background: -moz-linear-gradient(-45deg,  #a9e4f7 0%, #0fb4e7 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#a9e4f7), color-stop(100%,#0fb4e7)); /* Chrome,Safari4+ */ 
  background: -webkit-linear-gradient(-45deg,  #a9e4f7 0%,#0fb4e7 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(-45deg,  #a9e4f7 0%,#0fb4e7 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(-45deg,  #a9e4f7 0%,#0fb4e7 100%); /* IE10+ */
  background: linear-gradient(135deg,  #a9e4f7 0%,#0fb4e7 100%); /* W3C */ 
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a9e4f7', endColorstr='#0fb4e7',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */

  background-attachment: fixed;
}

body {
  margin-top: 0px;
  margin-bottom: 0px;
}

/* OPTIONAL: div to store content.  Many of these attributes should be changed to suit your needs */
#container
{
  width: 800px;
  margin: auto;
  background-color: white;
  border: 1px solid gray;
  border-top: none;
  border-bottom: none;
  box-shadow: 3px 0px 20px #333;
  padding: 10px;
}

已在 IE、Chrome 和 Firefox 上对各种尺寸和滚动需求的页面进行了测试。


7
投票

在末尾添加一个空格和单词 fixed 就足够了。无需设置高度。

body{
    background: linear-gradient(#e4efe9,#93a5cf) fixed;
}

4
投票

脏;也许你可以添加一个 min-height: 100%;到 html 和 body 标签?那或至少设置一个默认背景颜色,它也是最终渐变颜色。


2
投票

我很难在这里找到答案。
我发现在 body 中修复一个全尺寸的 div,给它一个负的 z 索引,然后给它附加渐变效果会更好。

<style>

  .fixed-background {
    position:fixed;
    margin-left: auto;
    margin-right: auto;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: -1000;
    background-position: top center;
    background-size: cover;
    background-repeat: no-repeat;
  }

  .blue-gradient-bg {
    background: #134659; /* For browsers that do not support gradients */
    background: -webkit-linear-gradient(top, #134659 , #2b7692); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(bottom, #134659, #2b7692); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(top, #134659, #2b7692); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to bottom, #134659 , #2b7692); /* Standard syntax */
  }

  body{
    margin: 0;
  }

</style>

<body >
 <div class="fixed-background blue-gradient-bg"></div>
</body>

这是完整的示例 https://gist.github.com/morefromalan/8a4f6db5ce43b5240a6ddab611afdc55


0
投票

我使用了这个 CSS 代码,它对我有用:

html {
  height: 100%;
}
body {
  background: #f6cb4a; /* Old browsers */
  background: -moz-linear-gradient(top, #f2b600 0%, #f6cb4a 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f2b600), color-stop(100%,#f6cb4a)); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #f2b600 0%,#f6cb4a 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #f2b600 0%,#f6cb4a 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #f2b600 0%,#f6cb4a 100%); /* IE10+ */
  background: linear-gradient(top, #f2b600 0%,#f6cb4a 100%); /* W3C */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f2b600', endColorstr='#f6cb4a',GradientType=0 ); /* IE6-9 */
  height: 100%;
  background-repeat: no-repeat;
  background-attachment: fixed;
  width: 100%;
  background-position: 0px 0px;
}

相关信息是,您可以在 http://www.colorzilla.com/gradient-editor/

创建自己的出色渐变

/斯坦


-1
投票
background: #13486d; /* for non-css3 browsers */
background-image: -webkit-gradient(linear, left top, left bottom, from(#9dc3c3),   to(#13486d));  background: -moz-linear-gradient(top,  #9dc3c3,  #13486d);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#9dc3c3', endColorstr='#13486d');
background-repeat:no-repeat;

-1
投票

这就是我所做的:

html, body {
height:100%;
background: #014298 ;
}
body {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#5c9cf2), color-stop(100%,#014298));
background: -moz-linear-gradient(top, rgba(92,156,242,1) 0%, rgba(1,66,152,1) 100%);
background: -o-linear-gradient(top, #5c9cf2 0%,#014298 100%);

/*I added these codes*/
margin:0;
float:left;
position:relative;
width:100%;
}

在我浮动body之前,顶部有一个间隙,它显示的是html的背景颜色。如果我删除html的bgcolor,当我向下滚动时,渐变就会被剪切。所以我浮动了主体并将其位置设置为相对并将宽度设置为 100%。它适用于 safari、chrome、firefox、opera、internet expl.. 哦等等。 :P

大家觉得怎么样?


-4
投票

而不是 100%,我只是添加一些像素,现在得到了这个,它适用于整个页面,没有间隙:

html {     
height: 1420px; } 
body {     
height: 1400px;     
margin: 0;     
background-repeat: no-repeat; }
© www.soinside.com 2019 - 2024. All rights reserved.