Background image

In this article we are going to learn about background image in css, how to add image as the background of an html element in html page.

Background image

Background image is a Cascading Style Sheet property used to add the image as the background of any html element.

Let us create a div element with background image property applied, copy and paste below code.

<!DOCTYPE html>

<head>

<style>

.background-image-example {

background-image: url(image/coverphoto.png);

background-size: contain;

}

<style/>

</head>

<body>

<div class="background-image-example">This is a div element with background image added.</div>

</body>

</html>


So, we should have the following results and the image is behind the text and the image is repeated to take up full div element width.

This is a div element with background image added.

Let us create another div element with background image property applied, copy and paste below code.

<!DOCTYPE html>

<head>

<style>

.background-image-example {

background-image: url(image/photo.png);

background-size: contain;

color: red;

}

<style/>

</head>

<body>

<div class="background-image-example">This is a div element with background image added.</div>

</body>

</html>


So, we should have the following results and the image is behind the text and the image is repeated to take up full div element width.

This is a div element with background image added.

Conclusion

In this article we learnt about background image in css with examples, how to add image as the background of an html element in html page.