CSS HTML

How to apply CSS to iframe

HTML - CSS - iframe css example - How to apply CSS to iframe - An IFrame (Inline Frame) is an HTML document embedded inside another HTML document on

 What is an Iframe?

  • An IFrame (Inline Frame) is an HTML document embedded inside another HTML document on a website.
  • The IFrame HTML element is often used to insert content from another source, such as an advertisement, into a Web page.
  • Although an IFrame behaves like an inline image, it can be configured with its own scrollbar independent of the surrounding page’s scrollbar.
  • A Web designer can change an IFrame’s content without requiring the user to reload the surrounding page.
  • This capacity is enabled through JavaScript or the target attribute of an HTML anchor.
  • Web designers use IFrames to embed interactive applications in Web pages, including those that employ Ajax (Asynchronous JavaScript and XML), like Google Maps or ecommerce applications.
  • An iframe is universally handled like a different HTML page by most browsers.
  • If you want to apply the same stylesheet to the content of the iframe, just reference it from the pages used in there.

Applying CSS to iframe

How to apply CSS to iframe

How to apply :

  • If you want to apply the same stylesheet to the content of the iframe, just reference it from the pages used in there.
  • If you control the page in the iframe, the easiest approach is to create a shared CSS file with common styles, then just link to it from your html pages.
  • When you embed an IFRAME element in your HTML, you have two opportunities to add CSS styles to it:
    • You can style the IFRAME itself.
    • You can style the page inside the IFRAME (under certain conditions).

Using CSS to style the IFRAME element: 

  • The first thing you should consider when styling your iframes is the IFRAME itself.
  • While most browsers include iframes without a lot of extra styles, it’s still a good idea to add some styles to keep them consistent.
READ  Offsetting an html anchor to adjust for fixed header

Here are some CSS styles :

    • margin: 0;
    • padding: 0;
    • border: none;
    • width: value;
    • height: value;
  • Here are examples of a frame with no styles and one with just the basics styled.
  • These styles mostly just remove the border around the iframe, but they also ensure that all browsers display that iframe with the same margins, padding, and dimensions.
  • HTML5 recommends that you use the overflow property to remove the scroll bars, but that isn’t reliable.
  • So if you want to remove or change the scroll bars, you should use the scrolling attribute on your iframe as well.
  • To use the scrolling attribute, add it like any other attribute and then choose one of three values: yes, no, or auto.
  • Yes tells the browser to always include scroll bars even if they aren’t needed.
  • No says to remove all scroll bars whether needed or not.
  • Auto is the default and includes the scroll bars when they are needed and removes them when they are not.

Here is how to turn off scrolling with the scrolling attribute

html code
<iframe src="iframe.html" scrolling="no">
This is an iframe.
</iframe>
  • To turn off scrolling in HTML5 you are supposed to use the overflow property.
  • But in these examples it doesn’t work reliably in all browsers yet. Here’s how you would turn on scrolling all the time with the overflow property:
html code
<iframe src="iframe.html" style="overflow: scroll;">
This is an iframe.
</iframe>

Using The IFRAME element

  • How  to use the IFRAME element :

    • src—the URL for the source of the frame
    • height—the height of the window
    • width—the width of the window
    • name—the name of the window
  • srcdoc—the HTML for the source of the frame. This attribute takes precedence over any URL in the src attribute
  • sandbox—a list of features that should be allowed or disallowed in the frame window
  • seamless—tells the user agent that the iframe should be rendered like it is invisibly part of the parent document
  • Long desc — instead, use an A element to link to a description
  • Align — instead, use the CSS float property
  • Allow transparency — instead, use the CSS background property to make the iframe transparent
  • Frame border — instead use the border CSS property
  • Margin height — instead, use the CSS margin property
  • Margin width — instead, use the CSS margin property
  • Scrolling — instead, use the CSS overflow property
READ  How to Modify the URL without reloading the page

IFRAME browser support :

  • The IFRAME element is supported by all modern browsers:
    • Android
    • Chrome
    • Firefox
    • Internet Explorer 2+
    • iOS / Safari Mobile
    • Netscape 7+
    • Opera 3+
    • Safari
  • Using overflow to turn off scrolling is not reliable. If you don’t want scrollbars on your iframes, you should continue to use the scrolling attribute.
  • The srcdoc, sandbox, and seamless attributes are not supported by any browsers at this time.

Sample Program :

css code
<style type="text/css" id="cssID">
.className
{
    background-color: red;
}
</style>

<iframe id="iFrameID"></iframe>

<script type="text/javascript">
    $(function () {
        $("#iFrameID").contents().find("head")[0].appendChild(cssID);
        //Or $("#iFrameID").contents().find("head")[0].appendChild($('#cssID')[0]);
    });
</script>

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

2 Comments

Click here to post a comment