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
Milesplit Com Colorado
Zillow Palm Harbor
12 Rue Gotlib 21St Arrondissem*nt
Jps Occupational Health Clinic
No Hard Feelings Showtimes Near Metropolitan Fiesta 5 Theatre
Proto Ultima Exoplating
All Obituaries | Sneath Strilchuk Funeral Services | Funeral Home Roblin Dauphin Ste Rose McCreary MB
Terraria Artisan Loaf
Which Universal Life Option Has A Gradually
Nail Salons Open Now Near My Location
Psf Condition Permanent Sad Face
Survivor Australia Wiki
8776685260
The Meaning Behind The Song: Waymore's Blues by Waylon Jennings - Beat Crave
Craigslist Carroll Iowa
Betty Rea Ice Cream
Celebrating Kat Dennings' Birthday: A Look Into The Life Of A Unique Talent
Kamala Harris is making climate action patriotic. It just might work
DRAGON BALL Z - Goku Evolution - Light Canvas 40X3 NEU • EUR 37,63
Does Publix Have Sephora Gift Cards
Bootyandthebeast69 Swap
Onderdelen | Onderdelen en services
Bx11
Tani Ahrefs
The Secret Powers Of Doodling
Pair sentenced for May 2023 murder of Roger Driesel
Uganda: The tiny flea making it painful for people to walk and work | African Arguments
Dayinew
Does Dollar General Have Humidifiers
By Association Only Watsonville
Nc Scratch Off Left
Courtney Callaway Matthew Boynton
Air Quality Index Endicott Ny
Lesley Ann Downey Transcript
Odawa Hypixel
Shipstation Commercial Actress
7066642123
Lacy Aaron Schmidt Where Is He Now
Today's Final Jeopardy Clue
Arsenal’s Auston Trusty: Inspired by Ronaldinho, World Cup dreams and Birmingham loan
Weather Radar Jamestown
Best Th13 Base
Barbarian Frenzy Build with the Horde of the Ninety Savages set (Patch 2.7.7 / Season 32)
Blog:Vyond-styled rants -- List of nicknames (blog edition) (TouhouWonder version)
Locate Td Bank Near Me
Shaws Myaci
Intoxalock Calibration Locations Near Me
Oxford House Peoria Il
Explain the difference between a bar chart and a histogram. | Numerade
Corn And Tater Fest 2023
Upgrading Fedora Linux to a New Release
Randstad Westside
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.