Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I change the background color of a frame in HTML?
HTML frames, while deprecated in modern web development, were once used to divide web pages into separate sections. Each frame could display different HTML content with its own background color. Today, iframes are the modern equivalent for embedding external content, and similar visual effects can be achieved using CSS layouts with divs.
Understanding HTML Frames vs Modern Alternatives
Traditional HTML frames used <frameset> and <frame> elements to divide pages into sections. However, these are deprecated due to accessibility and SEO issues. Modern web development uses iframes for embedding external content or CSS Grid/Flexbox with div elements for layout purposes.
Syntax
For traditional frames (deprecated)
<frameset rows="50%,50%"> <frame src="page1.html" name="frame1"> <frame src="page2.html" name="frame2"> </frameset>
For modern iframes
<iframe src="page.html" name="myframe" width="100%" height="400"></iframe>
For CSS-based frame-like layouts
.frame-container {
display: grid;
grid-template-rows: 1fr 1fr;
height: 100vh;
}
.frame { background-color: lightblue; }
Method 1: Using Inline CSS with iframes
The most direct way to change an iframe's background color is using inline CSS styles. This method applies styles directly to the iframe element.
Example
<!DOCTYPE html>
<html>
<head>
<title>iframe Background Color - Inline CSS</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>iframe with Custom Background</h2>
<iframe
src="data:text/html,<h1>Hello from iframe!</h1>"
style="background-color: lightblue; width: 100%; height: 200px; border: 2px solid #333;"
title="Sample iframe">
</iframe>
<h3>Another iframe with Different Color</h3>
<iframe
src="data:text/html,<p style='color: white;'>White text on dark background</p>"
style="background-color: #333; width: 100%; height: 150px; border: 1px solid #ccc;"
title="Dark iframe">
</iframe>
</body>
</html>
The output shows two iframes with different background colors
iframe with Custom Background [Light blue iframe containing "Hello from iframe!"] Another iframe with Different Color [Dark gray iframe with white text]
Method 2: Using Internal CSS
Internal CSS provides better organization by defining styles within the <head> section. This method is ideal when styling multiple frames consistently.
Example
<!DOCTYPE html>
<html>
<head>
<title>iframe Background - Internal CSS</title>
<style>
.blue-frame {
background-color: #e6f3ff;
border: 3px solid #0066cc;
width: 100%;
height: 180px;
}
.green-frame {
background-color: #e6ffe6;
border: 3px solid #009900;
width: 100%;
height: 180px;
}
</style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Multiple iframes with Class-based Styling</h2>
<h3>Blue Theme iframe</h3>
<iframe
class="blue-frame"
src="data:text/html,<div style='padding: 20px;'><h2>Blue Section</h2><p>This iframe has a blue theme.</p></div>"
title="Blue themed content">
</iframe>
<h3>Green Theme iframe</h3>
<iframe
class="green-frame"
src="data:text/html,<div style='padding: 20px;'><h2>Green Section</h2><p>This iframe has a green theme.</p></div>"
title="Green themed content">
</iframe>
</body>
</html>
The output displays two iframes with distinct color themes applied through CSS classes
Multiple iframes with Class-based Styling Blue Theme iframe [Light blue iframe with blue border containing "Blue Section" content] Green Theme iframe [Light green iframe with green border containing "Green Section" content]
Method 3: Creating Frame-like Layouts with Divs
Modern web development uses div elements with CSS to create frame-like layouts without the drawbacks of traditional frames. This approach provides better accessibility and SEO performance.
Example
<!DOCTYPE html>
<html>
<head>
<title>Modern Frame Layout with Divs</title>
<style>
.frame-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
height: 300px;
}
.frame {
padding: 20px;
border: 2px solid #333;
overflow: auto;
}
.frame-red { background-color: #ffe6e6; }
.frame-yellow { background-color: #fff9e6; }
</style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Modern Frame-like Layout</h2>
<div class="frame-container">
<div class="frame frame-red">
<h3>Left Frame</h3>
<p>This is a modern div-based "frame" with a red background. It provides better accessibility and SEO than traditional HTML frames.</p>
</div>
<div class="frame frame-yellow">
<h3>Right Frame</h3>
<p>This "frame" has a yellow background. CSS Grid makes it easy to create responsive layouts that work on all devices.</p>
</div>
</div>
</body>
</html>
The output shows a modern two-column layout resembling traditional frames
Modern Frame-like Layout [Two side-by-side sections: left with red background, right with yellow background, both containing text content]
Styling Content Inside iframes
To change the background color of the content displayed inside an iframe, you must style the source document itself. The iframe's background color only shows when the content doesn't fill the entire frame.
Example
<!DOCTYPE html>
<html>
<head>
<title>iframe Content Background</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Styling Content Inside iframe</h2>
<iframe
src="data:text/html,<!DOCTYPE html><html><head><style>body{background-color: lightcoral; color: white; padding: 20px; margin: 0;}</style></head><body><h2>Styled Content</h2><p>This content has its own background color defined within the iframe source.</p></body></html>"
style="width: 100%; height: 200px; border: 2px solid #333;"
title="Styled iframe content">
</iframe>
</body>
</html>
The iframe displays content with its own internal styling and background color
Styling Content Inside iframe [iframe containing coral-colored background with white text: "Styled Content" and description]
Comparison of Methods
| Method | Best Use Case | Pros | Cons |
|---|---|---|---|
| Inline CSS | Quick styling of individual elements | Simple, immediate application | Hard to maintain, repetitive |
| Internal CSS | Single-page styling with multiple frames | Organized, reusable classes | Not reusable across pages |
| Div-based Layout | Modern responsive layouts | SEO-friendly, accessible, flexible | Requires more CSS knowledge |
| Content Styling | Styling iframe source documents | Full control over embedded content | Requires access to source files |
Browser Compatibility and Best Practices
Modern browsers support iframe background colors through CSS. For maximum compatibility and accessibility, consider these best practices
Use semantic HTML with div elements instead of deprecated frames for layout purposes.
Apply responsive design using CSS Grid or Flexbox for frame-like layouts.
Include title attributes on iframes for screen reader accessibility.
Test background colors with sufficient contrast for readability.
Use external CSS files for production websites to improve maintainability.
