How do I put background images to frames in HTML?

To add background images to frames in HTML, you use CSS (Cascading Style Sheets). The background-image property specifies the URL of the image you want to use as the background. You can also control additional background properties like background-size, background-repeat, and background-position to customize how the image appears within the frame.

Note: HTML frames using the <frameset> and <frame> elements are deprecated in HTML5. Modern web development uses CSS to create frame-like layouts with <div> elements or CSS Grid and Flexbox.

Syntax

Following is the basic syntax for applying background images using CSS

.frame {
   background-image: url('image-path.jpg');
   background-size: cover | contain | auto;
   background-repeat: no-repeat | repeat;
   background-position: center | top | bottom;
}

Methods for Adding Background Images

There are three primary methods to apply CSS styling for background images

  • Inline CSS Style applied directly to individual elements using the style attribute

  • Internal CSS CSS rules defined within the <style> section in the document head

  • External CSS CSS rules stored in a separate .css file and linked to the HTML document

Using Inline CSS

Inline CSS applies styling directly to HTML elements using the style attribute. This method provides immediate, element-specific styling but is not recommended for multiple elements as it makes maintenance difficult.

Example

Following example shows how to add a background image using inline CSS

<!DOCTYPE html>
<html>
<head>
   <title>Inline CSS Background Image</title>
</head>
<body style="font-family: Arial, sans-serif; margin: 0;">
   <div style="background-image: url('https://via.placeholder.com/600x400/4CAF50/white?text=Frame+1'); 
               background-size: cover; 
               background-position: center; 
               width: 100%; 
               height: 300px; 
               border: 2px solid #333; 
               display: flex; 
               align-items: center; 
               justify-content: center; 
               color: white; 
               font-size: 24px; 
               font-weight: bold;">
      Frame with Background Image
   </div>
</body>
</html>

The output shows a frame with a background image covering the entire area

[A 600x300 frame with a green background image and white centered text reading "Frame with Background Image"]

Using Internal CSS

Internal CSS defines styles within the <style> section in the document head. This approach keeps CSS organized within the HTML document and allows you to style multiple elements using classes and IDs.

Example

Following example demonstrates creating multiple frame-like sections with different background images

<!DOCTYPE html>
<html>
<head>
   <title>Internal CSS Background Images</title>
   <style>
      body {
         font-family: Arial, sans-serif;
         margin: 0;
         padding: 20px;
      }
      
      .frame {
         width: 100%;
         height: 200px;
         border: 2px solid #333;
         margin: 10px 0;
         display: flex;
         align-items: center;
         justify-content: center;
         color: white;
         font-size: 18px;
         font-weight: bold;
         text-shadow: 2px 2px 4px rgba(0,0,0,0.7);
      }
      
      .frame1 {
         background-image: url('https://via.placeholder.com/600x200/FF6B6B/white?text=Nature');
         background-size: cover;
         background-position: center;
      }
      
      .frame2 {
         background-image: url('https://via.placeholder.com/600x200/4ECDC4/white?text=Ocean');
         background-size: cover;
         background-position: center;
      }
      
      .frame3 {
         background-image: url('https://via.placeholder.com/600x200/45B7D1/white?text=Sky');
         background-size: cover;
         background-position: center;
      }
   </style>
</head>
<body>
   <h2>Multiple Frames with Background Images</h2>
   <div class="frame frame1">Nature Frame</div>
   <div class="frame frame2">Ocean Frame</div>
   <div class="frame frame3">Sky Frame</div>
</body>
</html>

The output displays three frames, each with a different background image and text overlay

Multiple Frames with Background Images

[Red nature-themed frame with "Nature Frame" text]
[Teal ocean-themed frame with "Ocean Frame" text]
[Blue sky-themed frame with "Sky Frame" text]

Using External CSS

External CSS stores styles in a separate .css file linked to the HTML document. This method promotes code organization, reusability, and easier maintenance across multiple pages.

Example

Following example shows how to link an external CSS file for background images

<!DOCTYPE html>
<html>
<head>
   <title>External CSS Background Images</title>
   <style>
      /* This would typically be in an external file called styles.css */
      body {
         font-family: Arial, sans-serif;
         margin: 0;
         padding: 20px;
      }
      
      .container {
         display: grid;
         grid-template-columns: 1fr 1fr;
         gap: 20px;
         height: 400px;
      }
      
      .frame {
         background-size: cover;
         background-position: center;
         background-repeat: no-repeat;
         border: 3px solid #333;
         display: flex;
         align-items: center;
         justify-content: center;
         color: white;
         font-size: 20px;
         font-weight: bold;
         text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
      }
      
      .mountain-frame {
         background-image: url('https://via.placeholder.com/400x200/8B4513/white?text=Mountains');
      }
      
      .forest-frame {
         background-image: url('https://via.placeholder.com/400x200/228B22/white?text=Forest');
      }
      
      .desert-frame {
         background-image: url('https://via.placeholder.com/400x200/F4A460/white?text=Desert');
         grid-column: 1 / -1;
      }
   </style>
</head>
<body>
   <h2>Grid Layout with Background Images</h2>
   <div class="container">
      <div class="frame mountain-frame">Mountain View</div>
      <div class="frame forest-frame">Forest Scene</div>
      <div class="frame desert-frame">Desert Landscape</div>
   </div>
</body>
</html>

The output shows a grid layout with frames containing different background images

Grid Layout with Background Images

[Two frames side by side:]
[Mountain View - brown background] [Forest Scene - green background]
[Desert Landscape - sandy background spanning full width]
Background Image Properties background-size cover - fills container contain - fits inside auto - original size 100% 50% - custom background-repeat no-repeat - once repeat - tile image repeat-x - horizontal repeat-y - vertical background-position center - middle top left - corners 50% 25% - custom right bottom

Background Properties Comparison

Following table compares the key background properties used with frame backgrounds

Property Purpose Common Values
background-image Specifies the image URL url('image.jpg'), none
background-size Controls image scaling cover, contain, auto, 100% 50%
background-repeat Controls image repetition no-repeat, repeat, repeat-x, repeat-y
background-position Sets image placement center, top left, 50% 25%, right bottom
background-attachment Controls scrolling behavior fixed, scroll, local

Responsive Frame Backgrounds

For responsive design, you can use CSS media queries to change background images based on screen size.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Responsive Background Images</title>
   <style>
      body {
         font-family: Arial, sans-serif;
         margin: 0;
         padding: 20px;
      }
      
      .responsive-frame {
         width: 100%;
         height: 250px;
         background-image: url('https://via.placeholder.com/800x250/3498db/white?text=Desktop+View');
         background-size: cover;
         background-position: center;
         border: 2px solid #333;
         display: flex;
         align-items: center;
         justify-content: center;
         color: white;
         font-size: 24px;
         font-weight: bold;
         text-shadow: 2px 2px 4px rgba(0,0,0,0.7);
      }
      
      @media (max-width: 768px) {
         .responsive-frame {
            background-image: url('https://via.placeholder.com/400x250/e74c3c/white?text=Mobile+View');
            height: 200px;
            font-size
Updated on: 2026-03-16T21:38:54+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements