How to apply CSS to iframe?

To apply CSS to iframe is a straightforward process using any of the three methods of adding CSS to HTML documents. An iframe element can be styled just like any other HTML element using inline, internal, or external CSS.

Styling iframes is commonly needed to control their appearance, set dimensions, add borders, and ensure they integrate seamlessly with your page design. Each CSS approach offers different advantages depending on your specific requirements.

Syntax

The basic syntax for styling an iframe with CSS

iframe {
    /* CSS properties */
    width: value;
    height: value;
    border: value;
}

For inline styling

<iframe style="property: value;" src="url"></iframe>

Using Inline CSS with iframe

Inline CSS applies styles directly to the iframe element using the style attribute. This method is useful for unique, one-time styling of specific iframe elements.

Example

Following example demonstrates applying inline CSS to style an iframe

<!DOCTYPE html>
<html>
<head>
    <title>Inline CSS for iframe</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
    <h3>Applying Inline CSS to iframe</h3>
    <p>This iframe uses inline CSS for styling:</p>
    
    <iframe style="border: 3px solid #2c5aa0; 
                   width: 100%; 
                   height: 300px; 
                   border-radius: 8px;
                   box-shadow: 0 4px 8px rgba(0,0,0,0.1);" 
            src="https://www.example.com">
        Your browser does not support iframes.
    </iframe>
</body>
</html>

The iframe displays with a blue border, rounded corners, and a subtle shadow effect applied through inline styling.

Using Internal CSS with iframe

Internal CSS uses the style tag in the document head to define iframe styles. This approach is ideal when you want consistent iframe styling across multiple iframes within the same page.

Example

Following example shows how to apply internal CSS to iframe elements

<!DOCTYPE html>
<html>
<head>
    <title>Internal CSS for iframe</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        iframe {
            border: 2px dashed #e74c3c;
            width: 90%;
            height: 250px;
            display: block;
            margin: 20px auto;
            background-color: #f8f9fa;
        }
        .special-iframe {
            border-color: #27ae60;
            border-style: solid;
        }
    </style>
</head>
<body>
    <h3>Applying Internal CSS to iframe</h3>
    <p>Multiple iframes styled with internal CSS:</p>
    
    <iframe src="https://www.example.com"></iframe>
    <iframe class="special-iframe" src="https://www.example.com"></iframe>
</body>
</html>

The first iframe uses the default styling with a red dashed border, while the second iframe combines the base styles with additional class-specific styling (green solid border).

Using External CSS with iframe

External CSS stores iframe styles in a separate stylesheet file, making it reusable across multiple HTML pages. This approach promotes better code organization and maintainability.

External CSS File

Create a CSS file named iframe-styles.css

/* iframe-styles.css */
iframe {
    border: 2px solid #6c757d;
    width: 100%;
    height: 400px;
    border-radius: 10px;
    transition: all 0.3s ease;
}

iframe:hover {
    border-color: #007bff;
    transform: scale(1.02);
    box-shadow: 0 6px 20px rgba(0, 123, 255, 0.3);
}

.responsive-iframe {
    max-width: 800px;
    margin: 0 auto;
    display: block;
}

HTML File Using External CSS

Following example demonstrates linking the external CSS file to style iframes

<!DOCTYPE html>
<html>
<head>
    <title>External CSS for iframe</title>
    <link rel="stylesheet" href="iframe-styles.css">
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
    <h3>Applying External CSS to iframe</h3>
    <p>This iframe is styled using an external stylesheet with hover effects:</p>
    
    <iframe class="responsive-iframe" src="https://www.example.com">
        Your browser does not support iframes.
    </iframe>
</body>
</html>

The iframe includes hover effects that change the border color, apply a scaling transform, and add a blue shadow when the user hovers over it.

Advanced iframe Styling

Beyond basic styling, iframes can be enhanced with responsive design and advanced CSS properties for better user experience.

Example Responsive iframe with Aspect Ratio

<!DOCTYPE html>
<html>
<head>
    <title>Responsive iframe Styling</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        .iframe-container {
            position: relative;
            width: 100%;
            height: 0;
            padding-bottom: 56.25%; /* 16:9 aspect ratio */
            border: 3px solid #17a2b8;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 8px 25px rgba(23, 162, 184, 0.2);
        }
        .iframe-container iframe {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>
<body>
    <h3>Responsive iframe with Aspect Ratio</h3>
    <p>This iframe maintains a 16:9 aspect ratio and is fully responsive:</p>
    
    <div class="iframe-container">
        <iframe src="https://www.example.com"></iframe>
    </div>
</body>
</html>

This example creates a responsive iframe that maintains its 16:9 aspect ratio across different screen sizes while adding visual enhancements like rounded corners and shadow effects.

CSS Methods for iframe Styling Inline CSS ? Direct styling ? Quick implementation ? Single iframe ? style="..." ? Higher specificity ? Immediate ? Not reusable Internal CSS ? <style> in head ? Page-wide styling ? Multiple iframes ? Element selectors ? Class/ID targeting ? Organized ? Single page External CSS ? Separate .css file ? Multi-page reuse ? Better maintenance ? <link> tag ? Caching benefits ? Reusable ? Extra HTTP

Comparison of CSS Methods for iframe

Method Best Use Case Advantages Disadvantages
Inline CSS Quick styling of individual iframes Immediate application, high specificity, no external dependencies Not reusable, harder to maintain, increases HTML file size
Internal CSS Consistent styling within a single page Organized code, reusable within page, supports complex selectors Limited to one page, increases HTML file size
External CSS Consistent styling across multiple pages Highly reusable, easy maintenance, browser caching, clean HTML Additional HTTP request, potential loading delay

Conclusion

Applying CSS to iframe elements can be accomplished using inline, internal, or external CSS methods. Choose inline CSS for quick, one-off styling; internal CSS for page-specific iframe designs; and external CSS for consistent styling across multiple pages. Each method offers unique benefits depending on your project's scope and maintenance requirements.

Updated on: 2026-03-16T21:38:54+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements