正文上设置的 CSS3 渐变背景不会拉伸而是重复?
好吧,假设 内的内容总共 300px 高。
如果我使用 -webkit-gradient
或 -moz-linear-gradient
设置 的背景,
那么我将最大化窗口(或者只是使其高于 300 像素)渐变将恰好为 300 像素高(内容的高度),然后重复填充窗口的其余部分。
我假设这不是一个错误,因为它在 webkit 和 gecko 中都是相同的。
但是有没有办法让渐变拉伸以填充窗口而不是重复呢?
ok say the content inside the <body>
totals 300px high.
If I set the background of my <body>
using -webkit-gradient
or -moz-linear-gradient
Then I maximize my window (or just make it taller than 300px) the gradient will be exactly 300px tall (the height of the content) and just repeat to fill the rest of the window.
I am assuming this is not a bug since it is the same in both webkit and gecko.
But is there a way to make the gradient stretch to fill the window instead of repeat?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
应用以下 CSS:
编辑: 将
margin: 0;
添加到每个评论的正文声明 (马丁)。编辑:在每个评论的正文声明中添加了
background-attachment:fixed;
(Johe Green )。Apply the following CSS:
Edit: Added
margin: 0;
to body declaration per comments (Martin).Edit: Added
background-attachment: fixed;
to body declaration per comments (Johe Green).关于之前的答案,如果内容需要滚动,将
html
和body
设置为height: 100%
似乎不起作用。在背景中添加fixed
似乎可以解决这个问题 - 不需要height: 100%;
例如:
Regarding a previous answer, setting
html
andbody
toheight: 100%
doesn't seem to work if the content needs to scroll. Addingfixed
to the background seems to fix that - no need forheight: 100%;
E.g.:
我知道我参加聚会迟到了,但这里有一个更可靠的答案。
您需要做的就是使用
min-height: 100%;
而不是height: 100%;
并且您的渐变背景将扩展内容的整个高度 不重复,即使内容是可滚动的。像这样:
不过还有第二种解决方案。
正如其他人所说,将值
fixed
添加到background
声明中,将使渐变延伸到视口的整个高度。像这样:
当然,您仍然需要在
html
中声明min-height: 100%;
。这是 CodePen 中的演示,您可以在其中使用这两种解决方案: https://codepen.io/ ricardozea/pen/abwGBmz?editors=1100
I know I'm late to the party, but here's a more solid answer.
All you need to do is use
min-height: 100%;
rather thanheight: 100%;
and your gradient background will extend the entire height of the content without repeating, even if the content is scrollable.Like this:
There's a second solution though.
As others have said, adding the value
fixed
to thebackground
declaration, will make the gradient extend the full height of the viewport.Like this:
Granted, you still need to declare
min-height: 100%;
in thehtml
.Here's a demo in CodePen where you can play with both solutions: https://codepen.io/ricardozea/pen/abwGBmz?editors=1100
这就是我为解决这个问题所做的...它将显示内容的完整长度的渐变,然后简单地回退到背景颜色(通常是渐变中的最后一种颜色)。
我已经在 FireFox 3.6、Safari 4 和 Chrome 中对此进行了测试,对于由于某种原因不支持 HTML 标记样式的任何浏览器,我将背景颜色保留在正文中。
Here's what I did to solve this problem... it will show the gradient for the full length of the content, then simply fallback to the background color (normally the last color in the gradient).
I've tested this in FireFox 3.6, Safari 4, and Chrome, I keep the background-color in the body for any browsers that for some reason don't support styling the HTML tag.
设置
html { height: 100%}
会对 IE 造成严重破坏。 这是一个示例 (png)。 但您知道什么效果最好吗?只需在标记上设置背景即可。
背景延伸到底部,不会发生奇怪的滚动行为。您可以跳过所有其他修复。这得到了广泛的支持。我还没有找到不允许您将背景应用于 html 标记的浏览器。它是完全有效的 CSS,并且已经存在了一段时间了。 :)
Setting
html { height: 100%}
can wreak havoc with IE. Here's an example (png). But you know what works great? Just set your background on the<html>
tag.Background extends to the bottom and no weird scrolling behavior occurs. You can skip all of the other fixes. And this is broadly supported. I haven't found a browser that doesn't let you apply a background to the html tag. It's perfectly valid CSS and has been for a while. :)
此页面上有很多部分信息,但不完整。这就是我所做的:
将所有内容放入其中
这是一个示例:
这已经过测试可以使用 IE、Chrome 和 Firefox 来处理各种尺寸和滚动需求的页面。
There is a lot of partial information on this page, but not a complete one. Here is what I do:
<DIV id='container'>
that I put all of my content inHere is an example:
This has been tested with IE, Chrome, and Firefox on pages of various sizes and scrolling needs.
在末尾添加一个空格和单词fixed就足够了。无需设置高度。
Adding a space and the word fixed to the end should be sufficient. No need to set heights.
肮脏的;也许你可以添加一个 min-height: 100%;到 html 和 body 标签?那或至少设置一个默认背景颜色,它也是最终渐变颜色。
Dirty; maybe could you just add a min-height: 100%; to the html, and body tags? That or at least set a default background color that is the end gradient color as well.
我很难在这里找到答案。
我发现在 body 中修复一个全尺寸的 div,给它一个负的 z 索引,然后给它附加渐变效果会更好。
这是完整的示例
https://gist.github.com/morefromalan/8a4f6db5ce43b5240a6ddab611afdc55
I had trouble getting the answers in here to work.
I found it worked better to fix a full-size div in the body, give it a negative z-index, and attach the gradient to it.
Here's a full sample
https://gist.github.com/morefromalan/8a4f6db5ce43b5240a6ddab611afdc55
我使用了这个 CSS 代码,它对我有用:
相关信息是,您可以在 http://www.colorzilla.com/gradient-editor/
/Sten
I have used this CSS code and it worked for me:
A related information is that you can create your own great gradients at http://www.colorzilla.com/gradient-editor/
/Sten
这就是我所做的:
在我浮动主体之前,顶部有一个间隙,它显示了 html 的背景颜色。如果我删除html的bgcolor,当我向下滚动时,渐变就会被剪切。所以我浮动了主体并将其位置设置为相对,宽度设置为 100%。它适用于 safari、chrome、firefox、opera、internet expl.. 哦等等。 :P
你们觉得怎么样?
this is what I did:
before I floated the body, there was a gap on top, and it was showing the background color of html. if I remove the bgcolor of html, when I scroll down, the gradient is cut. so I floated the body and set it's position to relative and the width to 100%. it worked on safari, chrome, firefox, opera, internet expl.. oh wait. :P
what do you guys think?
我只是添加一些像素,而不是 100%,现在得到了这个,它适用于整个页面,没有间隙:
instead of 100% i just add some pixxel got this now and it works for whole page without gap: