I want to have a background gradient, e.g.
background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), rgba(2, 126, 174, 0.9))
on the body of my document which extends across the entire height of the body - and scrolls with the body.
Using
background-attachment: fixed;
I get the entire height (of the viewport), but it, well, stays fixed.
background-attachment: scroll;
gets me the scrolling, but then it just extends across the viewport height.
Any way to get both?
Edit:
As Boltclock pointed out, I do get the expected behavior with 'background-attachment: scroll' on a basic test page.
With
<body>
<div id="stretch">Content</div>
</body>
and
body {
background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), rgba(2, 126, 174, 0.9));
}
#stretch {
height: 2000px;
}
everything works fine: The gradient extends across the entire body (and the body is 2000px tall), and scrolls with the content.
Then I add
html,
body {
height: 100%;
}
This is something which the basic template the page is based on does.
In order to get the body to expand again, I add
height: auto;
and the body is back to 2000px height.
However, the gradient remains at the HTML height - and repeats then.
What am I missing here?
Thy this ( add any
background-size
you need):Here's the fiddle: http://jsfiddle.net/BfH8D/1/
Is this the result you want?
This happens because of the way gradient backgrounds are sized according to their containers (this includes both
html
andbody
), as well as the calculations used in determining the dimensions ofhtml
andbody
respective to the viewport.I've actually written about something similar before in an answer to another question. Yours is a little different, so I'll quote the relevant portion first:
In your case, if the template that you're using doesn't specify any backgrounds for
html
orbody
, then this background propagating behavior is what causes the gradient to take the height ofhtml
instead ofbody
.To fix this you need to reset the
height
value for thehtml
element only, and assignmin-height
instead:This removes the height constraint on the
html
element, allowing the gradient to stretch along with the contents of your page, while keeping it at least as tall as the viewport if the page doesn't have enough content (if that's not important, you don't need to setmin-height
).