- background-color
- background-image
- background-repeat
- background-attachment
- background-position
Here is an element with a background color.
By default, the image is tiled until it covers the entire element.
- repeat -x
The value repeat-x ensures that the background-image is repeated only along the x axis (that is, the horizontal axis in both directions—left and right) until the element’s background is fully covered along that axis.
repeat -y
The value repeat-y ensures that the background -image is repeated only along the y axis (that is, the vertical axis in both directions—up and down) until the element’s background is fully covered along that axis.
no-repeat
The value no -repeat ensures that the background -image is not repeated in any direction, and that only a single instance of the image will be placed at the coordinates specified by the background-position.
If no background-position has been specified,
the image is placed at the element’s default left-top position (0,0). - Code
<!DOCTYPE html>
<html>
<head>
<title>HTML Tutorials</title>
<style>
body {
background-image: url("pattern.png");
background-repeat: repeat-x;
height: 200px;
}
</style>
</head>
<body>
</body></html>
Output- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: repeat-y;height: 200px;}</style></head><body></body></html>
- Output
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;height: 200px;}</style></head><body></body></html>
- Output
- The background-position property sets the position of the background image.
An element with a background image positioned at the top right corner. - Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-position: top right;height: 200px;}</style></head><body></body></html>
- Output
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-position: right 80px top 20px;height: 200px;}</style></head><body></body></html>
- Output
- The background-attachment property specifies how a background image will attach to the element. Background images may scroll with the rest of the page or have a fixed position.
- A background image with a fixed position in the document.
- Scroll the page and the images will stay fixed.
- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");height: 300px;width: 300px;background-attachment: fixed;}</style></head><body></body></html>
- Output
- Background-size
- The background-size property sets the background image size.
- It accepts two values : width and height.
- Syntax:
- background-size : auto | length | contain | cover | initial | inherit
Value
DESCRIPTION
auto
Default. Displays background image original size.
length
Sets width (first value) and height (second value) of background image. If the second value is not given, it will be set to auto.
percentage
Sets width (first value) and height (second value) of background image in percent. If the second value is not given, it will be set to auto.
cover
Stretch or crop background image to cover the entire container.
contain
Resize background image to make it fully visible.
initial
Sets the value to its default value.
inherit
Inherits the value from its parent element.
- auto
- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-size: auto;height:200px;}</style></head><body></body></html>
- Output
- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-size: 200px 200px;}</style></head><body></body></html>
- Output
- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-size: 50%;}</style></head><body></body></html>Output
- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-size: cover;}</style></head><body></body></html>
- Output
- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-size: contain;height:200px;}</style></head><body></body></html>
- Output
- Code
- <!DOCTYPE html><html><head><title>HTML Tutorials</title><style>body {background-image: url("pattern.png");background-repeat: no-repeat;background-size: inherit;height:200px;}</style></head><body></body></html>
- Output
No comments:
Post a Comment