Skip to content
Related Articles

Related Articles

HTML Background Images

Improve Article
Save Article
  • Last Updated : 17 Aug, 2021
Improve Article
Save Article

HTML stands for HyperText Markup Language. It is used to design or create web pages using a markup language. It is a combination of Hypertext and Markup language. Here, hypertext defines the link between the web pages whereas markup language is used to define the text document within tag which defines the structure of web pages. In HTML, we can make our web pages look more attractive and captivating by adding a background image to our HTML page. So, we can add background images in two different ways:

  1. Add a background image using the background image attribute inside the <body> tag.
  2. Add background image using the HTML style attribute.

In this article, we will learn these two methods for adding background images to web pages.

1. Using the background-image attribute inside the <body> tag

In this method we will add a background image is using the background image attribute inside the <body> tag.

Syntax:

<body background=”image.png”>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
  <title>Background Image</title>
</head>
<body background="gfg (2).png" >
  <h3>Welcome to GeeksforGeeks</h3>
</body>
</html>

Output:

2. Using the HTML style attribute.

In this method, we will add a background image using the HTML style attribute for a particular element in the webpage.

Syntax:

<div style=”background-image: url(‘img.jpg’);”>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
  <title>Background Image</title>
</head>
<body>
  <h3>Welcome to GeeksforGeeks</h3>
  <div  style="background-image: url('gfg (2).png');">
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
      
<p>A Computer Science portal for geeks.</p>
  
  </div>
</body>
</html>

In the above example we can see that there is no background image for the heading “Welcome to GeeksforGeeks”, but we have a background image for the div containing paragraphs.

Output:

How repeated background image?

In HTML, if the image is small in size as compared to the element then the image repeats itself horizontally and vertically. It repeats until it reaches the end of the element. If you don’t want to repeat the image then set the value of background-repeat property to no-repeat. Let us discuss this concept with the help of an example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        body{
            background-image: url('example.png');
        }
    </style>
</head>
<body>
<h2>welcome</h2>
</body>
</html>

Output:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!