Prelims - CSS backgrounds Flashcards
properties are used to add background effects for elements
CSS Backgrounds
property specifies the background color of an element
background-color
body { background-color: lightblue; }
specifies the opacity/transparency of an element. It can take a value from 0.0 - 1.0. The lower value, the more transparent
opacity
div { background-color: green; opacity: 0.3; }
property specifies an image to use as the background of an element
background-image
body { background-image: url("paper.gif"); }
By default, the ____ property repeats an image both horizontally and vertically
background-image
To repeat an image vertically, set
background-repeat: repeat-y;
body { background-image: url("gradient_bg.png"); background-repeat: repeat-y; }
To repeat an image horizontally, set
background-repeat: repeat-x;
body { background-image: url("gradient_bg.png"); background-repeat: repeat-x; }
Show the background image only once
background-repeat: no-repeat;
body { background-image: url("img_tree.png"); background-repeat: no-repeat; }
property is used to specify the position of the background image
background-position
property specifies whether the background image should scroll or be fixed (will not scroll with the rest of the page
background-attachment
Specify that the background image should be fixed
background-attachment: fixed
body { background-image: url("img_tree.png"); background-repeat: no-repeat; background-position: right top; background-attachment: fixed; }
Specify that the background image should scroll with the rest of the page
background-attachment: scroll
body { background-image: url("img_tree.png"); background-repeat: no-repeat; background-position: right top; background-attachment: scroll; }
To shorten the code, it is also possible to specify all the background properties in one single property. This is called a
CSS background - Shorthand property
Instead of writing:
body { background-color: #ffffff; background-image: url("img_tree.png"); background-repeat: no-repeat; background-position: right top; }
Use the shorthand property to set the background properties in one declaration
body { background: #ffffff url("img_tree.png") no-repeat right top; }
CSS backgrounds
When using the shorthand property the order of the property values is
- background-color
- background-image
- background-repeat
- background-attachment
- background-position