We can set colors, gradients, and many images as the background on a web page. We can change the properties of the background as per our choice. Here we will learn to use different background properties i.e. background-image, Background-gradients, Background size, etc.
Set different background properties in one declaration:
<style>
body {
background: black url("image.svg") no-repeat fixed center;
}
</style>
SYNTAX
<style>
body {
background: black url("image.svg") no-repeat fixed center;
}
</style>
<!--body-->
<body>
</body>
background-image property is used to specify a background image to be applied to all matched elements.
<style>
div{background-image:url('image.svg')}
</style>
Other Properties
<!DOCTYPE html>
<html>
<head>
<!--internal style sheet-->
<style>
div{color:rgb(255,255,255);
background-image:url('image.svg');
background-color:black;
height:400px;
border-radius:5px}
</style>
</head>
<!--Body-->
<body>
<div>
</div>
</body>
</html>
When we use the background-image property then the background repeats on both the x-direction and the y-direction. The solution is CSS background-repeat property to stop repeating the background. By using this property we can remove background repeating in both directions.
<style>
div{background-image:url('image.svg');
background-repeat:no-repeat;}
</style>
SYNTAX
<style>
body{
background-repeat:repeat-x;
}
</style>
<style>body{background-repeat:repeat-y;}</style>
{background-repeat: no-repeat} is used to stop repeating the background.
<!DOCTYPE html>
<html>
<head>
<!--style sheet-->
<style>
div{
color:rgb(255,255,255);
background-image:url('image.svg');
background-color:black;
background-repeat:no-repeat;
background-size:200px;
height:380px;
border-radius:5px
}
</style>
</head>
<!--Body-->
<body>
<div>
</div>
</body>
</html>
background-position property is used to set the position of the background-images.
<style>
div{
background-image:url('image.svg');
background-position:top right;
}
</style>
SYNTAX
EXPLANATION
<!DOCTYPE html>
<html>
<head>
<style>
div{color:rgb(255,255,255);
background-image:url('image.svg');
background-position:top right;
background-color:black;
background-repeat:no-repeat;
background-size:100px;
height:380px;
border-radius:5px}
</style>
</head>
<!--Body-->
<body>
<div>
</div>
</body>
</html>
Gradients are added in CSS3. As an image, gradients are set with the background-image property or the background shorthand method.
TYPES
#linear-gradient() Syntax
background: linear-gradient (direction, color-1, color-2, ...);
#radial-gradient() Syntax
background: radial-gradient(red, blue);
MULTIPLE GRADIENTS
You can separate multiple linear-gradient properties with a comma.
<style>
body {
background: linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
}
</style>
<!DOCTYPE html>
<html>
<head>
<style>
/*linear gradient*/
.class1{background: linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%);height:100px}
/*radial gradient*/
.class2{background: radial-gradient(white, gray, black);height:100px}
/*multiple linear gradients*/
.class3{background: linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
height:300px;}
</style>
</head>
<body>
<div class="class1"></div><br>
<div class="class2"></div><br>
<div class="class3"></div>
</body>
</html>
You can specify the size of a background image in px, %, and auto.
VALUES
# when we set the width of the image the height becomes "auto"
<style>
.class1{background-size:200px;}
</style>
# we can also set both width and height values.
<style>
.class2{background-size:200px 300px;}
</style>
# Default value: auto
<style>
.class3{background-size:auto;}
</style>
<!DOCTYPE html>
<html>
<head>
<style>
div{color:rgb(255,255,255);
background-image:url('image.svg');
background-color:black;
background-repeat:no-repeat;
height:380px;
border-radius:5px}
.class1{background-size:200px;}
.class2{background-size:auto;}
</style>
</head>
<!--Body-->
<body>
<div class="class1"></div>
<div class="class2"></div>
</body>
</html>
We can use multiple background properties in one declaration
SYNTAX
<style>
body{background:bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial|inherit;}
</style>
<!DOCTYPE html>
<html>
<head>
<style>
div{
color:rgb(255,255,255);
background:black
url('image.svg')
center
no-repeat
fixed;
height:400px;
border-radius:5px
}
</style>
</head>
<!--Body-->
<body>
<div></div>
</body>
</html>