Вы находитесь на странице: 1из 3

8/4/13

CSS Background
Search w3schools.com: Select Language

HOME

HTML

C SS

JAVASC RIPT

JQUERY

XML

ASP.NET

PHP

SQL

MORE...

R EFER ENC ES

EXAMPLES

FO R UM

ABO UT

Get Certified
Study Web Technologies and get a diploma at w3schools.com

SHARE THIS PAGE

Like

76k

CSS Basic
CSS HOME CSS Introduction CSS Syntax CSS Id & Class CSS How To

CSS Background
Previous
CSS background properties are used to define the background effects of an element. CSS properties used for background effects: background-color background-image background-repeat background-attachment background-position

WEB HOSTING

Next Chapter

Best Web Hosting eUK Web Hosting UK Reseller Hosting Domain, Hosting & Email

CSS Styling
Styling Backgrounds Styling Text Styling Fonts Styling Links Styling Lists Styling Tables

WEB BUILDING
XML Editor - Free Trial! FREE Website BUILDER FREE Website C reator Best Website Templates

Background Color
The background-color property specifies the background color of an element. The background color of a page is defined in the body selector: STATISTICS
Browser Statistics OS Statistics

CSS Box Model


CSS Box Model CSS Border CSS Outline CSS Margin CSS Padding

Example
b o d y{ b a c k g r o u n d c o l o r : # b 0 c 4 d e ; }
Try it yourself With CSS, a color is most often specified by: a HEX value - like "#ff0000" an RGB value - like "rgb(255,0,0)" a color name - like "red" 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:

Display Statistics

CSS Advanced
CSS Grouping/Nesting CSS Dimension CSS Display CSS Positioning CSS Floating CSS Align CSS Pseudo-class CSS Pseudo-element CSS Navigation Bar CSS Image Gallery CSS Image Opacity CSS Image Sprites CSS Media Types CSS Attribute Selectors CSS Summary

Example
h 1{ b a c k g r o u n d c o l o r : # 6 4 9 5 e d ; } p{ b a c k g r o u n d c o l o r : # e 0 f f f f ; } d i v{ b a c k g r o u n d c o l o r : # b 0 c 4 d e ; }
Try it yourself

CSS Examples
CSS Examples CSS Quiz CSS Certificate

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:

CSS References
CSS Reference CSS Selectors CSS Reference Aural CSS Web Safe Fonts CSS Units CSS Colors CSS Color Values CSS Color Names CSS Color HEX

Example
b o d y{ b a c k g r o u n d i m a g e : u r l ( ' p a p e r . g i f ' ) ; }
Try it yourself Below is an example of a bad combination of text and background image. The text is almost not readable:

Example
b o d y{ b a c k g r o u n d i m a g e : u r l ( ' b g d e s e r t . j p g ' ) ; }
Try it yourself

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

w3schools.com/css/css_background.asp

1/3

8/4/13

CSS Background
b o d y { b a c k g r o u n d i m a g e : u r l ( ' g r a d i e n t 2 . p n g ' ) ; }
Try it yourself If the image is repeated only horizontally (repeat-x), the background will look better:

Example
b o d y { b a c k g r o u n d i m a g e : u r l ( ' g r a d i e n t 2 . p n g ' ) ; b a c k g r o u n d r e p e a t : r e p e a t x ; }
Try it yourself

Background Image - Set position and no-repeat


When using a background image, use an image that does not disturb the text. Showing the image only once is specified by the background-repeat property:

Example
b o d y { b a c k g r o u n d i m a g e : u r l ( ' i m g _ t r e e . p n g ' ) ; b a c k g r o u n d r e p e a t : n o r e p e a t ; }
Try it yourself 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
b o d y { b a c k g r o u n d i m a g e : u r l ( ' i m g _ t r e e . p n g ' ) ; b a c k g r o u n d r e p e a t : n o r e p e a t ; b a c k g r o u n d p o s i t i o n : r i g h tt o p ; }
Try it yourself

Background - Shorthand property


As you can see from the examples above, there are many properties to consider when dealing with backgrounds. To shorten the code, it is also possible to specify all the properties in one single property. This is called a shorthand property. The shorthand property for background is simply "background":

Example
b o d y{ b a c k g r o u n d : # f f f f f fu r l ( ' i m g _ t r e e . p n g ' )n o r e p e a tr i g h tt o p ; }
Try it yourself 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 ones that are present are in this order. This example uses more advanced CSS. Take a look: Advanced example

More Examples
How to set a fixed background image This example demonstrates how to set a fixed background image. The image will not scroll with the rest of the page.

All CSS Background Properties


w3schools.com/css/css_background.asp 2/3

8/4/13
Property background background-attachment background-color background-image background-position background-repeat Description

CSS Background
Sets all the background properties in one declaration Sets whether a background image is fixed or scrolls with the rest of the page Sets the background color of an element Sets the background image for an element Sets the starting position of a background image Sets how a background image will be repeated

Previous

Next Chapter

Top 10 Tutorials
HTML Tutorial HTML5 Tutorial C SS Tutorial C SS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial PHP Tutorial ASP.NET Tutorial XML Tutorial

Top 10 References
HTML/HTML5 Reference C SS 1,2,3 Reference C SS 3 Browser Support JavaScript HTML DOM XML DOM PHP Reference jQuery Reference ASP.NET Reference HTML C olors

Examples
HTML Examples C SS Examples XML Examples JavaScript Examples HTML DOM Examples XML DOM Examples AJAX Examples ASP.NET Examples Razor Examples ASP Examples SVG Examples

Quizzes
HTML Quiz HTML5 Quiz XHTML Quiz C SS Quiz JavaScript Quiz jQuery Quiz XML Quiz ASP Quiz PHP Quiz SQL Quiz

Color Picker

Statistics
Browser Statistics Browser OS Browser Display

RE P O RT E RRO R

HO ME

TO P

P RI N T

FO RU M

A BO U T

A D V E RT I SE WI T H U S

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. C opyright 1999-2013 by Refsnes Data. All Rights Reserved.

w3schools.com/css/css_background.asp

3/3

Вам также может понравиться