Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 gradient background set on body doesn't stretch but instead repeats?

Tags:

css

gradient

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?

like image 833
JD Isaacks Avatar asked May 19 '10 20:05

JD Isaacks


People also ask

How do I stop a repeating linear gradient in CSS?

A color-stop's <color> value, followed by one or two optional stop positions, (each being either a <percentage> or a <length> along the gradient's axis). A percentage of 0% , or a length of 0 , represents the start of the gradient; the value 100% is 100% of the image size, meaning the gradient will not repeat.

How do you apply a linear gradient to the body in CSS?

CSS Linear GradientsTo create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

How do you transition a background gradient in CSS?

In CSS, you can't transition a background gradient. It jumps from one gradient to the other immediately, with no smooth transition between the two. He documents a clever tactic of positioning a pseudo element covering the element with a different background and transitioning the opacity of that pseudo element.


2 Answers

Apply the following CSS:

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

Edit: Added margin: 0; to body declaration per comments (Martin).

Edit: Added background-attachment: fixed; to body declaration per comments (Johe Green).

like image 193
Bryan Downing Avatar answered Sep 29 '22 12:09

Bryan Downing


Regarding a previous answer, setting html and body to height: 100% doesn't seem to work if the content needs to scroll. Adding fixed to the background seems to fix that - no need for height: 100%;

E.g.:

body {    background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#cbccc8)) fixed;  }
like image 25
Joshua Rudd Avatar answered Sep 29 '22 10:09

Joshua Rudd