Home 〉 CSS Tutorials 〉 CSS Background Property with Examples - Learn Background Color, Image, Repeat, Size
CSS Background Property with Examples - Learn Background Color, Image, Repeat, Size !
Are you looking to enhance your webpage's visual appeal with backgrounds? The CSS `background` property allows you to set background colors, images, and control their repetition and size. This tutorial covers how to apply solid colors, insert images, and adjust their positioning and scaling. By mastering these properties, you can create visually engaging sections on your website. Explore out blog post on CSS Background Property
The 'background' property in CSS is a shorthand to set all background-related styles such as background color, background image, position, repeat, and size.
It helps control the look and feel of a webpage's background, making your layout more visually appealing.
In this Tutorial, we are going to cover the following CSS 'background' properties:
Syntax ✍
selector {
background: background-color background-image background-position background-size background-repeat background-attachment;
}
You don't have to use all values. You can use only the ones you need.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div style="background-color: lightblue;">This has a light blue background color.</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This sets a solid background color to the element. A complete blog post is availble for background-color property
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div style="background-image: url('/images/image.jpg'); height: 200px;">Background image applied</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Sets an image as the background.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div style="background-image: url('/images/image.jpg'); background-repeat: no-repeat; height: 200px;"></div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
'no-repeat prevents the image from repeating. Other options: 'repeat', 'repeat-x', 'repeat-y'.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div style="height: 200px; background-color: lightgray; background-image: url('/images/image.jpg'); background-repeat: no-repeat; background-position: center;"></div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
'center' positions the image in the center. Other values: 'top', 'bottom', 'left', 'right'.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div style="background-image: url('/images/image.jpg'); background-size: cover; height: 200px;"></div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
'cover' scales the image to fully cover the element.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div style="background-image: url('/images/image.jpg'); background-attachment: fixed; height: 300px;"></div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
'fixed Makes the background image fixed while scrolling.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div style="width: 100%; height: 300px; background: lightgray url('/images/image.jpg') no-repeat fixed center;"></div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This sets color, image, no-repeat, position, and size in one line.
The CSS 'background' property is a powerful way to control the visual design of your elements. From simple color fills to full-width responsive background images, mastering this property helps you create beautiful layouts easily.
Yes. The background color will be shown if the image fails to load.
Use 'background-repeat: no-repeat;'
'cover' fills the entire element. 'contain' fits the whole image inside the element without cropping.
No, but it helps write cleaner code when setting multiple background properties.
Yes, apply it to the '<body>' tag in your CSS.