An image is automatically repeated both vertically and horizontally by the background-image attribute.
Certain images, like this one, should only be duplicated vertically or horizontally to avoid weird visual effects.
body {
background-image: url("gradient_bg.png");
}
The background will appear better if the preceding image is merely repeated horizontally (background-repeat: repeat-x;).
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
Note: To repeat an image vertically, set background-repeat: repeat-y;
The background-repeat attribute also specifies that the background image should only be displayed once:
Show the background image only once:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
The background image and text are positioned in the same spot in the example above. In order to avoid overly disturbing the text, we would want to move the image.
The background image’s location can be specified using the background-position attribute.
Position the background image in the top-right corner:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
Property | Description |
---|---|
background-position | Sets the starting position of a background image |
background-repeat | Sets how a background image will be repeated |
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.