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
Thy this ( add any background-size
you need):
body
{
background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), rgba(2, 126, 174, 0.9));
background-attachment: scroll;
background-size: 100% 900px;
overflow: hidden;
}
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
and body
), as well as the calculations used in determining the dimensions of html
and body
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:
Backgrounds have special behavior when applied to
body
and/orhtml
. When you apply a background tobody
without touchinghtml
, what happens is that the body background gets propagated to the viewport, and that background acts as if it were declared onhtml
:For documents whose root element is an HTML
HTML
element or an XHTMLhtml
element: if the computed value of ‘background-image’ on the root element is ‘none’ and its ‘background-color’ is ‘transparent’, user agents must instead propagate the computed values of the background properties from that element's first HTMLBODY
or XHTMLbody
child element. The used values of thatBODY
element's background properties are their initial values, and the propagated values are treated as if they were specified on the root element.
In your case, if the template that you're using doesn't specify any backgrounds for html
or body
, then this background propagating behavior is what causes the gradient to take the height of html
instead of body
.
To fix this you need to reset the height
value for the html
element only, and assign min-height
instead:
html {
height: auto;
min-height: 100%;
}
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 set min-height
).