You can use images to create custom webpage backgrounds. You can position your image how you want it and choose how it repeats (or doesn't repeat). You can also control the size of your image and how the background image behaves when the user scrolls your page.
Let's start by choosing a background-image for the body of our page.
body {
background-image: url("image.png");
}
➼ body
- This CSS block is for the page body.
➼ background-image
- This is the line of code for adding a background-image.
➼ Image URL
- image.png is the URL for the image.
By default, the background-image will repeat to fill the background space. However, you may not want the image to repeat, or you may want it to repeat a particular way.
body {
background-image: url("image.png");
background-repeat: no-repeat;
}
➼ body
- This code block is for the page body.
➼ background-image
- image.png is our background image.
➼ background-repeat
- It is set to no-repeat.
➼ no-repeat
means the background-image will not repeat.
background position: bottom left;
background-position: top right;
background-position: center center;
background position: 0% 100%;
background-position: 100% 0%;
background-position: 50% 50%;
Now, let's add a background-position to our background image.
body {
background-image: url("image.png");
background-repeat: no-repeat;
background-position: top center;
}
➼ body
- This code block is for the page body.
➼ background-image
- image.png is our background image.
➼ background-repeat
- It is set to no-repeat.
➼ background-position
- It is set to top center;
Using background-attachment, you can choose to have your background-image at a fixed position or you can have it scroll with the page body. Scroll is the default setting, but if you want your back-image fixed, just add this line of code:
background-attachment: fixed;
Now, let's make our background-image fixed.
body {
background-image: url("image.png");
background-repeat: no-repeat;
background-position: top center;
background-attachment: fixed;
}
➼ body
- This code block is for the page body.
➼ background-image
- image.png is our background image.
➼ background-repeat
- It is set to no-repeat.
➼ background-position
- It is set to top center;
➼ background-attchment
- It is fixed.
You can also control the size of your background image with the background-size property. There are a few different ways to describe the background-size.
background-size: 250px 350px;
background-size: 250px;
body {
background-size: 25% 50%;
}
background-size: 25%;