How to apply CSS to iframe?


Define an inline frame with HTML tag <iframe>. The <iframe> tag is not somehow related to <frameset> tag, instead, it can appear anywhere in your document. The <iframe> tag defines a rectangular region within the document in which the browser can display a separate document, including scrollbars and borders. The src attribute is used to specify the URL of the document that occupies the inline frame.

We can easily apply CSS to an iframe, but let us first look at the attributes of the <iframe> −

  • src − This attribute is used to give the file name that should be loaded in the frame. Its value can be any URL.

  • name − This attribute allows you to give a name to a frame. It is used to indicate which frame a document should be loaded into.

  • marginheight − This attribute allows you to specify the height of the space between the top and bottom of the frame's borders and its contents. The value is given in pixels.

  • height − This attribute specifies the height of <iframe>.

  • scrolling − This attribute controls the appearance of the scrollbars that appear on the frame. This takes values either "yes", "no" or "auto".

  • longdesc − This attribute allows you to provide a link to another page containing a long description of the contents of the frame.

  • width − This attribute specifies the width of <iframe>.

Let us now apply CSS to iframe.

Applying Inline CSS to iframe

The style attribute is used to set inline css. We gave set the border, width and height −

<iframe style="border: 2px solid gray;
   width: 500px; height: 400px;" src="https://www.tutorialspoint.com/market/index.asp"
id="Ebooks"> 

Example

Let us see the example −

<!DOCTYPE html> <html> <head> </head> <body> <h1>Courses</h1> <iframe style="border: 2px solid gray; width: 500px; height: 400px;" src="https://www.tutorialspoint.com/market/index.asp" id="Ebooks"> </iframe> </body> </html>

Applying Internal CSS to iframe

The style tag is used to set internal css to iframe. We have set the iFrame style to a new border, width and height −

iframe { border: 3px solid green; width: 500px; height: 400px; }

Example

Let us see an example −

<!DOCTYPE html> <html> <head> <style> iframe { border: 3px solid green; width: 500px; height: 400px; } </style> </head> <body> <h1>Courses</h1> <iframe src="https://www.tutorialspoint.com/market/index.asp" id="Ebooks"> </iframe> </body> </html>

Advertisements