The CSS background properties are used to define the background effects for elements.
CSS background properties:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
Background Color
The
background-color
property specifies the background color of an element.The background color of a page is set like this:
Example
body {
background-color: lightblue;}
background-color: lightblue;}
With CSS, a color is most often specified by:
- a valid color name – like “red”
- a HEX value – like “#ff0000”
- an RGB value – like “rgb(255,0,0)”
Look at CSS Color Values for a complete list of possible color values.
In the example below, the <h1>, <p>, and <div> elements have different background colors:
Example
h1 {
background-color: green;}
div {
background-color: lightblue;}
p {
background-color: yellow;}
background-color: green;}
div {
background-color: lightblue;}
p {
background-color: yellow;}
Background Image
The
background-image
property specifies an image to use as the background of an element.By default, the image is repeated so it covers the entire element.
The background image for a page can be set like this:
Example
body {
background-image: url(“paper.gif”);}
background-image: url(“paper.gif”);}
Below is an example of a bad combination of text and background image. The text is hardly readable:
Example
body {
background-image: url(“bgdesert.jpg”);}
background-image: url(“bgdesert.jpg”);}
Note: When using a background image, use an image that does not disturb the text.
Background Image – Repeat Horizontally or Vertically
By default, the
background-image
property repeats an image both horizontally and vertically.Some images should be repeated only horizontally or vertically, or they will look strange, like this:
Example
body {
background-image: url(“gradient_bg.png”);}
background-image: url(“gradient_bg.png”);}
If the image above is repeated only horizontally (
background-repeat: repeat-x;
), the background will look better:Example
body {
background-image: url(“gradient_bg.png”);
background-repeat: repeat-x;}
background-image: url(“gradient_bg.png”);
background-repeat: repeat-x;}
Tip: To repeat an image vertically, set
background-repeat: repeat-y;
Background Image – Set position and no-repeat
Showing the background image only once is also specified by the
background-repeat
property:Example
body {
background-image: url(“img_tree.png”);
background-repeat: no-repeat;}
background-image: url(“img_tree.png”);
background-repeat: no-repeat;}
In the example above, the background image is shown in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much.
The position of the image is specified by the
background-position
property:Example
body {
background-image: url(“img_tree.png”);
background-repeat: no-repeat;
background-position: right top;}
background-image: url(“img_tree.png”);
background-repeat: no-repeat;
background-position: right top;}
Background Image – Fixed position
To specify that the background image should be fixed (will not scroll with the rest of the page), use the
background-attachment
property:Example
body {
background-image: url(“img_tree.png”);
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;}
background-image: url(“img_tree.png”);
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;}
Background – Shorthand property
To shorten the code, it is also possible to specify all the background properties in one single property. This is called a shorthand property.
The shorthand property for background is
background
:Example
body {
background: #ffffff url(“img_tree.png”) no-repeat right top;}
background: #ffffff url(“img_tree.png”) no-repeat right top;}
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
It does not matter if one of the property values is missing, as long as the other ones are in this order.
All CSS Background Properties
Property | Description |
---|---|
background | Sets all the background properties in one declaration |
background-attachment | Sets whether a background image is fixed or scrolls with the rest of the page |
background-clip | Specifies the painting area of the background |
background-color | Sets the background color of an element |
background-image | Sets the background image for an element |
background-origin | Specifies where the background image(s) is/are positioned |
background-position | Sets the starting position of a background image |
background-repeat | Sets how a background image will be repeated |
background-size | Specifies the size of the background image(s) |
Hi,
Very helpful blog abou, Thanks for sharing this blog with us, your blog contains very important information for us, I appreciate your work and effort. Please keep sharing more blogs.
Advantages of Artificial Intelligence
Water Hack Burns 2lb of Fat OVERNIGHT
More than 160k women and men are utilizing a simple and secret "water hack" to lose 1-2lbs each and every night while they sleep.
It's very simple and works on everybody.
Here are the easy steps for this hack:
1) Grab a drinking glass and fill it half full
2) Then use this strange HACK
and become 1-2lbs thinner the next day!
I have been meaning to read this and just never obtained a chance. It’s an issue that I’m really interested in, I just started reading and I’m glad I did. You’re a fantastic blogger, one of the best that I’ve seen. This weblog undoubtedly has some facts on topic that I just wasn’t aware of. Thanks for bringing this stuff to light. ux san francisco
bless you with regard to the particular blog post ive really been looking with regard to this kind of advice on the net for sum time these days hence with thanks ux san francisco
I’d should talk to you here. Which is not some thing I do! I quite like reading a post which will make people believe. Also, many thanks permitting me to comment! device mockups
This is certainly a remarkably amazing powerful resource that you’re offering and you simply provide it away cost-free!! I that can compare with discovering websites ones understand the particular in providing you fantastic learning resource for zero cost. We truly dearly loved examining this web site. Appreciate it! device mockup