The Cascading Style Sheets (CSS) language is a powerful tool that can transform the look and feel of your HTML documents. One such property that offers a lot of flexibility is the ‘background’ property.
What is the CSS Background Property?
The CSS background property is a shorthand for setting several individual properties at once, including:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
A Simple Example
<style>
body {
background: url('img_flwr.gif');
background-repeat: no-repeat;
background-position: right top;
}
</style>
In this example, we’ve used the `background` shorthand property to set several properties at once. The image ‘img_flwr.gif’ will be displayed as the body’s background, it will not repeat and it’s positioned at the top-right corner.
Tutorial: How to Use CSS Background Property?
- Create an HTML file and add some basic elements like <body>, <head>, <p>, etc.
- In your <head> section, create a <style> tag where you’ll write your CSS code.
- To set a background image that does not repeat and is positioned at the top-right corner, use this code:
- Replace ‘your-image.jpg’ with the path to your actual image.
<style>
body {
background: url('your-image.jpg');
background-repeat: no-repeat;
background-position: right top;
}
</style>
And that’s it! You’ve successfully used the CSS background property. Experiment with different values and images to see how it changes your webpage.
“`