Open In App

How to Apply CSS Property To an iframe ?

Last Updated : 29 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

HTML iframe is used to embed external content such as videos, maps, or entire web pages into another webpage. The iframe stylings are used to set the width, height, border, …, etc. of the iframe box. You can apply CSS styles to iframe in three different ways i.e. inline, internal and external CSS.

Apply CSS Property to an Iframe using Inline CSS

To apply CSS to an iframe using inline CSS, add the style attribute directly inside the iframe tag. Inside the style attribute, write the CSS properties that you need to apply.

html
<!-- iframe with inline CSS -->
<iframe src="https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
		style="border: 3px dotted black; width: 300px; height: 300px;">
</iframe>

Output

Iframe-styling

Apply CSS Property to an Iframe using Internal CSS

To apply CSS to an iframe using internal CSS, add the styling properties to the element inside <style> tag in head section. To style the iframe with CSS, use the selector to select iframe and apply CSS styling.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        #Iframe {
            border: 3px dotted black;
            width: 300px;
            height: 300px;
        }
    </style>
</head>

<body>
    <!-- iframe tag -->
    <iframe src="https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html" 
            id="Iframe"></iframe>
</body>

</html>

Apply CSS Property to an iframe using External CSS

To apply CSS to an iframe using external CSS, add the styles properties into a seperate file with .css extension, and link the CSS file in HTML document inside the head section.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="iframeCss.css" />
</head>

<body>
    <!-- iframe tag -->
    <iframe src="https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html" 
            id="Iframe" title="GFG Content"
            aria-label="GFG Content">
  	</iframe>
</body>

</html>
CSS
#Iframe {
    border: 3px dotted;
    width: 300px;
    height: 300px;
}

Why to use CSS Styles to an Iframe?

  • Custom Appearance: CSS allows you to control the iframe’s look, such as adding borders, rounded corners, or shadows, making your webpage’s design look good.
  • Responsive Design: CSS enables you to adjust the iframe’s size dynamically, which fits well on various screen sizes, improving the mobile and tablet user experience.
  • Layout Control: CSS helps position the iframe, add padding, and manage spacing around it, making it aligns properly within your webpage layout.


Similar Reads