Layering IFrames using positioning and z-index (2024)

I was recently given a task by a client that required me to layer multiple IFrames on top of each other in order to facilitate the display of 3 separate content blocks (share price feeds) so that the content of two of them was only partially shown.

Each frame was a simple, relatively small area of text that displayed the current share price for a listed fund:

There were to be 3 frames in total. Each frame was to display a different price, in the bottom 2 frames, only the share price was to be displayed and not any of the other content.

Using standard CSS positioning techniques I was able to position the 3 frames so that the 3 share prices were aligned correctly:

(Note the CSS code here is inline for simplicity)

<div style="display:block;position:absolute;top:0px;">
|||||<iframe src="http://www.myurl.com/iframe1" frameborder="0" allowtransparency="yes" scrolling="no" style="width:200px; height:90px; position:relative; left:-20px;top:30px;margin:0;padding:0;" />
</div>
<div style="display:block;position:absolute;top:30px;">
|||||<iframe src="http://www.myurl.com/iframe2" frameborder="0" allowtransparency="yes" scrolling="no" style="width:200px; height:90px; position:relative; left:-20px;top:30px;margin:0;padding:0;" />
</div>
<div style="display:block;position:absolute;top:60px;">
|||||<iframe src="
http://www.myurl.com/iframe3" frameborder="0" allowtransparency="yes" scrolling="no" style="width:200px; height:90px; position:relative; left:-20px;top:30px;margin:0;padding:0;" />
</div>

The major problem here is the overlapping of IFrames, while the positioning is correct, the 3rd IFrame overlaps the 1st and 2nd IFrames, and the 2nd IFrame overlaps the 1st IFrame.

To get around this we use the z-index property of CSS. This property only works on elements that have been positioned, as we have done with each IFrame, in that it has been placed within an absolutely positioned HTML div element. Z-index facilitates the display order (or ‘stack’ order) of elements on a page.

Hence we get the following by setting the z-index of each div to 999, 998 and 997 respectively.

<div style="display:block;position:absolute;top:0px;z-index:999;">
|||||<iframe src="http://www.myurl.com/iframe1" frameborder="0" allowtransparency="yes" scrolling="no" style="width:200px; height:90px; position:relative; left:-20px;top:30px;margin:0;padding:0;" ></iframe>
</div>
<div style="display:block;position:absolute;top:30px;z-index:998;">
|||||<iframe src="http://www.myurl.com/iframe2" frameborder="0" allowtransparency="yes" scrolling="no" style="width:200px; height:90px; position:relative; left:-20px;top:30px;margin:0;padding:0;" ></iframe>
</div>
<div style="display:block;position:absolute;top:60px;z-index:997;">
|||||<iframe src="http://www.myurl.com/iframe3" frameborder="0" allowtransparency="yes" scrolling="no" style="width:200px; height:90px; position:relative; left:-20px;top:30px;margin:0;padding:0;" ></iframe>
</div>

Note the lower the number the z-index, the lower the priority it has in the stack. Z-indexes can also be negative.

The solution was effective enough for the client to be implemented on their site and saves the space that would otherwise be needed on the web page if there were 3 IFrames positioned separately from each other.

Written on Tuesday, July 1, 2008 on the subject of Development Programming

Layering IFrames using positioning and z-index (2024)

FAQs

Does z-index work with IFrame? ›

To get around this we use the z-index property of CSS. This property only works on elements that have been positioned, as we have done with each IFrame, in that it has been placed within an absolutely positioned HTML div element. Z-index facilitates the display order (or 'stack' order) of elements on a page.

Can I use z-index with position absolute? ›

z-index only works for positioned elements

If you want to control the stacking order of elements, you can do so with z-index . But z-index will only take affect if the element also has a position value of absolute , relative or fixed .

How to overlay an IFrame? ›

The iFrame tool inside the Overlay editor allows you to insert any URL-hosted content simply by copying and pasting in the web address. To add iFrames to your interactive overlay: Open the overlay editor. Click the iFrames button on the left-hand side of the overlay editor.

Does z-index work with position static? ›

To control an element with the z-index property, the element must have a value for position that is not static (the default). z-index will apply to elements with a position of relative, fixed, absolute, or sticky.

Why are iFrames discouraged? ›

The Downsides to Using iFrames

iFrames are killing your page speed - When you have iFrames on your page, you're really loading two (or more) pages simultaneously – and it can take forever! Anything you can do to keep the page size and load time down will improve the user experience.

What are the disadvantages of iframe? ›

The disadvantages of Iframe

The code is complicated and cannot be indexed by some search engines, which is crucial. Because current search engine crawlers are incapable of dealing with iframe content, using iframe will not aid in search engine optimization.

Can you use z-index with position relative? ›

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).

Which position does the z-index not work with? ›

z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

What happens if two absolutely positioned elements overlap and neither has a specified z-index? ›

If two positioned elements overlap each other without a z-index specified, the element defined last in the HTML code will be shown on top.

Can you put an iframe in an iframe? ›

iFrames can be nested inside each other, which means that you can have an iFrame within another iFrame. Sometimes, you may need to check if an iFrame is present in another iFrame, which can be a bit tricky.

How to make an embedded iframe responsive? ›

Follow these steps to create a responsive embed
  1. Insert the player embed code into an HTML page.
  2. Set the iframe tag's width and height attributes to 100% inside of the embed code.
  3. Change iframe tag's CSS position parameter to absolute and set the left and top css parameters to 0 if needed.

How do you put a div over an iframe? ›

The div can be overlayed over iframe using z-index and absolute positioning. Iframe should have z-index lower than the div having class as overlay. Style the overlay div to the same height and width as the iframe, so that it does not affect the functionality of any other divs.

When should I use z-index? ›

If you want to create a custom stacking order, you can use the z-index property on a positioned element. Note: When no z-index property is specified, elements are rendered on the default rendering layer (Layer 0).

How to use z-index to overlap? ›

Handling overlapping elements with z-index in CSS involves assigning a stacking order to positioned elements. To implement this, ensure the elements have a specified position property (e.g., relative, absolute).

Is higher z-index better? ›

In CSS code bases, you'll often see z-index values of 999, 9999 or 99999. This is a perhaps lazy way to ensure that the element is always on top. It can lead to problems down the road when multiple elements need to be on top. Most of the time you'll find that a z-index of 1 or 2 will suffice for your needs.

Can iframe access IndexedDB? ›

Note: Third party window content (e.g. <iframe> content) can access the IndexedDB store for the origin it is embedded into, unless the browser is set to never accept third party cookies (see Firefox bug 1147821.)

Does iframe content get indexed? ›

"The content displayed via iFrames may not be indexed and available to appear in Google's search results. We recommend that you avoid the use of iFrames to display content.

What does z-index only work with? ›

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).

Are iFrames bad for SEO? ›

While iFrames don't affect SEO as much as you might think, Google still advises that you only use them when absolutely necessary. This is because they can slow down your page loading times, and it's easier for web crawlers to access information directly on your website.

Top Articles
All33 Net Worth
Who is your pick to light the cauldron in L.A. in 2028?
Washu Parking
Live Basketball Scores Flashscore
What spices do Germans cook with?
Practical Magic 123Movies
Valley Fair Tickets Costco
Phone Number For Walmart Automotive Department
Eric Rohan Justin Obituary
Teamexpress Login
Call of Duty: NEXT Event Intel, How to Watch, and Tune In Rewards
When Is the Best Time To Buy an RV?
Truist Drive Through Hours
Savage X Fenty Wiki
Revitalising marine ecosystems: D-Shape’s innovative 3D-printed reef restoration solution - StartmeupHK
Mawal Gameroom Download
Dumb Money
Learn2Serve Tabc Answers
Bahsid Mclean Uncensored Photo
7543460065
Minecraft Jar Google Drive
Kitty Piggy Ssbbw
Spectrum Field Tech Salary
Illinois VIN Check and Lookup
Craigslist Toy Hauler For Sale By Owner
Kashchey Vodka
Att.com/Myatt.
Finalize Teams Yahoo Fantasy Football
Www.craigslist.com Savannah Ga
67-72 Chevy Truck Parts Craigslist
Miltank Gamepress
Reser Funeral Home Obituaries
Cpt 90677 Reimbursem*nt 2023
Mikayla Campinos: Unveiling The Truth Behind The Leaked Content
Mississippi Craigslist
ATM, 3813 N Woodlawn Blvd, Wichita, KS 67220, US - MapQuest
47 Orchid Varieties: Different Types of Orchids (With Pictures)
Boondock Eddie's Menu
What Happened To Father Anthony Mary Ewtn
Ixl Lausd Northwest
Serenity Of Lathrop - Manteca Photos
Reborn Rich Ep 12 Eng Sub
Eleceed Mangaowl
Laurin Funeral Home | Buried In Work
Poe Flameblast
Cherry Spa Madison
Ticket To Paradise Showtimes Near Marshall 6 Theatre
Evil Dead Rise (2023) | Film, Trailer, Kritik
Craigslist - Pets for Sale or Adoption in Hawley, PA
Product Test Drive: Garnier BB Cream vs. Garnier BB Cream For Combo/Oily Skin
Nfl Espn Expert Picks 2023
login.microsoftonline.com Reviews | scam or legit check
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6149

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.