Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

anyone know how to center an image in an iframe?

Status
Not open for further replies.

leo57

Programmer
Oct 28, 2013
33
US
this dose not work
Code:
<iframe  valign="center" src="images/woodscrew.jpg" name="content2"  backcolor height="140" width="140"></iframe>


 
Images are inline elements so just apply
CSS:
text-align: center;
to the element.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Hi

Chris said:
Images are inline elements
Correct. But inline elements exist only in HTML documents. However, there is no HTML document inside the OP's [tt]iframe[/tt], only the bare image. Are you sure you can apply CSS to it ?


Feherke.
feherke.github.io
 
No idea, not something I've tested and the TS is going to learn more by trying it rather than having a ready made solution "spoonfed" to them.

But an <iframe> without a URL value for the src attribute is just a block level container.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Hi

Chris said:
No idea, not something I've tested and the TS is going to learn more by trying it rather than having a ready made solution "spoonfed" to them.
I tried it out. Although I was sure will not work. Actually, I am sure there is no way to solve this with HTML and/or CSS.

leo57, I see absolutely no reason to load an image in an [tt]iframe[/tt]. I suggest to not do it. But if you really have to, you may try something like this :
JavaScript:
document.querySelector([green][i]'iframe[name="content2"]'[/i][/green]).contentWindow.addEventListener([green][i]'load'[/i][/green],[b]function[/b](e){e.target.body.style.textAlign=[green][i]'center'[/i][/green]},[b]false[/b])
Works in Gecko and newer Trident. Should work in WebKit too, but I was unable to test it. In Presto there is no need to do anything as the image inside the [tt]iframe[/tt] appears centered by default.


Feherke.
feherke.github.io
 
I agree with feherke, there's no real reason to load a single image in an iframe. If you load an html page that shows the image that may be one way to get it to center, bur really why are you loading an image in an iframe? Why not simply use a img tag in a div?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top