iframes in CSS
Iframe in CSS

In web design and development, the iframe is a very useful and powerful HTML element. An iframe can embed another webpage within another webpage and style it using CSS. In this article, we’ll discuss how to style an iframe using CSS.
What is an iframe?
An iframe is an inline frame element in HTML that allows you to embed another webpage within another webpage. Using an iframe, you can display content from other websites or other pages on the same website within a webpage. You can use iframes to embed maps, videos, advertisements, and more.
Normally, iframe is used as follows:
<iframe src="https://www.example.com"></iframe>
In the example above, the src attribute specifies the URL of the webpage to be embedded. When a browser loads the webpage containing the iframe, it displays the contents of the specified webpage within the page.
How to Style an iframe with CSS?
Although an iframe is a standalone HTML element, it can be styled with CSS. Here are some CSS styles that can be applied to an iframe:
1. Setting Width and Height
You can use the width and height attributes to set the width and height of an iframe. For example:
iframe {
width: 100%;
height: 500px;
}
In the above example, we set the iframe’s width to 100% and its height to 500 pixels.
2. Setting Border Styles
You can use the border property to set the iframe’s border style. For example:
iframe {
border: 1px solid #ccc;
}
In the above example, we set the iframe’s border to a solid 1-pixel gray border.
3. Setting Border Radius
You can use the border-radius property to set the iframe’s border radius. For example:
iframe {
border-radius: 10px;
}
In the above example, we set the iframe’s border to a 10-pixel radius.
4. Setting a Shadow Effect
You can use the box-shadow property to add a shadow effect to the iframe. For example:
iframe {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
In the above example, we add a slight shadow effect to the iframe.
5. Setting Scrollbars
You can use the overflow property to control the scrollbars of the content inside the iframe. For example:
iframe {
overflow: auto;
}
In the above example, we will add an automatic scrollbar to the iframe.
Example
To demonstrate how to style an iframe with CSS, we will create a simple webpage and embed an iframe into it. We will then style the iframe with CSS.
<!DOCTYPE html>
<html>
<head>
<style>
iframe {
width: 100%;
height: 500px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
overflow: auto;
}
</style>
</head>
<body>
<iframe src="https://www.example.com"></iframe>
</body>
</html>
In the example above, we set the width, height, border, border radius, shadow effect, and scrollbar for the iframe. You can adjust these styles as needed.
Conclusion
Using CSS, we can style iframes to better suit our design needs. In web design and development, the flexible use of iframes and CSS can achieve richer and more diverse functions and effects.