| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | title: CSS3 Backgrounds | 
					
						
							|  |  |  |  | --- | 
					
						
							|  |  |  |  | ## CSS3 Backgrounds
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 08:21:00 +01:00
										 |  |  |  | The CSS `background` shorthand property is used to define multiple properties like : | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | `background-color`, `background-image`, `background-repeat`, `background-attachment` and `background-position` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Background Color
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | The `background-color` property specifies the background color of an element. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |   background-color : #F00; | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Background Image
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | The `background-image` property specifies an image to use as background of an element. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 08:21:00 +01:00
										 |  |  |  | By default, the image repeats itself to cover the entire surface of the element. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |  background-image: url("GitHub-Mark.png"); | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Background Image - Repetition
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-04 16:41:19 -06:00
										 |  |  |  | By default, the `background-image` property repeats on the X and Y axis, to cover the area of its container. | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | If you want to set an axis, like X axis, use `background-repeat` property type: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |   background-image: url("GitHub-Mark.png"); | 
					
						
							|  |  |  |  |   background-repeat: repeat-x; | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 08:21:00 +01:00
										 |  |  |  | But sometimes you don't want to have your background image cover the whole surface, so you've to specify it by typing: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |   background-image: url("GitHub-Mark.png"); | 
					
						
							|  |  |  |  |   background-repeat: no-repeat; | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Background Image - Position
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  | You can specify the position of the background by typing : | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |   background-image: url("GitHub-Mark.png"); | 
					
						
							|  |  |  |  |   background-repeat: no-repeat; | 
					
						
							|  |  |  |  |   background-position : left bottom; | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 08:21:00 +01:00
										 |  |  |  | It will set your background image at the bottom left of the element. | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Background Image - Fixed Position
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-04 16:41:19 -06:00
										 |  |  |  | If you do not want the background image to scroll with the rest of the page, use the `background-attachement` property: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |   background-image: url("GitHub-Mark.png"); | 
					
						
							|  |  |  |  |   background-repeat: no-repeat; | 
					
						
							|  |  |  |  |   background-position: left bottom; | 
					
						
							|  |  |  |  |   background-attachment: fixed; | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Shorthand property
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 08:21:00 +01:00
										 |  |  |  | You can pass all the properties in one super-property: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |   background: #F00 url("GitHub-Mark.png") no-repeat fixed left bottom; | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-04 16:41:19 -06:00
										 |  |  |  | When you use this shorthand property, it must be in this order: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 1. Background color | 
					
						
							|  |  |  |  | 2. Background image | 
					
						
							|  |  |  |  | 3. Background repeat | 
					
						
							|  |  |  |  | 4. Background attachment | 
					
						
							|  |  |  |  | 5. Background position | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-04 16:41:19 -06:00
										 |  |  |  | It doesn't matter if one property is missing, so long as the order is maintained: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							| 
									
										
										
										
											2018-12-13 05:03:03 +00:00
										 |  |  |  |   background: url("GitHub-Mark.png") no-repeat left bottom; | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | This will work even if the color and the attachment are missing. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 08:26:21 +05:30
										 |  |  |  | ### Multiple Backgrounds
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | You can pass in multple background images and set their properties simultaneously. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							|  |  |  |  |    background-image: url("firstimage.jpg"), url("secondimage.jpg"); | 
					
						
							|  |  |  |  |    background-repeat: no-repeat, repeat; | 
					
						
							|  |  |  |  |    background-position: left bottom, right bottom; | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | You can add individual properties to individual images in their respective order. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  | #### More Information:
 | 
					
						
							|  |  |  |  | <!-- Please add any articles you think might be helpful to read before writing the article --> | 
					
						
							|  |  |  |  | <a href='https://developer.mozilla.org/en-US/docs/Web/CSS/background' target='_blank' rel='nofollow'>MDN</a> |