HTML Background Images - GeeksforGeeks

17 Aug, 2021
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:
Add a background image using the background image attribute inside the <body> tag.Add background image using the HTML style attribute.
Add a background image using the background image attribute inside the <body> tag.
Add background image using the HTML style attribute.
In this article, we will learn these two methods for adding background images to web pages.
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:
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:
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:
Picked
class 7
School Learning
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Amazon Interview Experience (Off-Campus) 2022
How to Read Text Files with Pandas?
Must Do Coding Questions for Product Based Companies
TCS NQT Coding Sheet
Inshackle - Tool for Instagram Hacks in Kali Linux
Libraries in Python
Cloud Deployment Models
What is a Storage Device? Definition, Types, Examples
Generations of Computers - Computer Fundamentals