CSS Background Property: Definition and Usage
CSS Background Property: Definition and Usage
</style>
</body>
</head>
</html>
<body>
Definition and Usage
Note: If using multiple background-image sources but also want a background-color, the
background-color parameter needs to be last in the list.
Property Values
background- Specifies whether the background images are fixed or scrolls with the 1
attachment rest of the page
CSS background-attachment Property
Example <p>The background-image is fixed. Try to scroll
down the page.</p>
A background-image that will not scroll with
<p>The background-image is fixed. Try to scroll
the page (fixed):
down the page.</p>
<html> <p>The background-image is fixed. Try to scroll
down the page.</p>
<head>
<p>The background-image is fixed. Try to scroll
<style>
down the page.</p>
body {
<p>The background-image is fixed. Try to scroll
background-image: url("img_tree.gif"); down the page.</p>
<p>The background-image is fixed. Try to scroll <p>The background-image is fixed. Try to scroll
down the page.</p> down the page.</p>
<p>The background-image is fixed. Try to scroll <p>If you do not see any scrollbars, try to resize
down the page.</p> the browser window.</p>
CSS Syntax
background-attachment: scroll | fixed | local | initial | inherit;
Property Values
Value Description
scroll The background image will scroll with the page. This is default
fixed The background image will not scroll with the page
local The background image will scroll with the element's contents
Example
}
</body>
</style>
</html>
</head>
<body>
CSS background-blend-mode Property
<html> </head>
<head> <body>
<style>
Value Description
CSS background-image Property
<html>
<head>
<style>
body {
background-image: url("img_tree.gif"), url("paper.gif");
background-color: #cccccc;
}
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Definition and Usage
Tip: The background of an element is the total size of the element, including padding and
border (but not the margin).
Value Description
url('URL') The URL to the image. To specify more than one image, separate the URLs with
a comma
linear- Sets a linear gradient as the background image. Define at least two colors (top to
gradient() bottom)
radial- Sets a radial gradient as the background image. Define at least two colors (center
gradient() to edges)
Example
Example
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
background-color: #cccccc;
background-image: linear-gradient(red, yellow);
}
</style>
</head>
<body>
<div id="grad1"></div>
</body>
</html>
CSS background-origin Property
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
border: 10px double black;
padding: 25px;
background: url(paper.gif);
background-repeat: no-repeat;
background-origin: padding-box;
}
#example2 {
border: 10px double black;
padding: 25px;
background: url(paper.gif);
background-repeat: no-repeat;
background-origin: border-box;
}
#example3 {
border: 10px double black;
padding: 25px;
background: url(paper.gif);
background-repeat: no-repeat;
background-origin: content-box;
}
</style>
</head>
<body>
<h2>background-origin: border-box:</h2>
<div id="example2">
<h2>Hello World</h2>
<p>The background image starts from the upper left corner of the border.</p>
</div>
<h2>background-origin: content-box:</h2>
<div id="example3">
<h2>Hello World</h2>
<p>The background image starts from the upper left corner of the content.</p>
</div>
</body>
</html>
Definition and Usage
CSS Syntax
background-origin: padding-box|border-box|content-box|initial|inherit;
Property Values
Value Description
padding- Default value. The background image starts from the upper left corner of the
box padding edge
border- The background image starts from the upper left corner of the border
box
content- The background image starts from the upper left corner of the content
box