+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Centering image within an iframe

  1. #1
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Centering image within an iframe

    For reference, try this link

    http://www.qualityimagesearch.com/vi...p?img_id=41581

    The image in the center has been placed in an iframe, for reasons I won't go into in detail here other than sites preventing hotlinking have difficulty if it's in a frame.

    My problem is simple - centering! I have crawled the web trying to find a solution, but all efforts have failed.

    1) css method - text-align: center - failed
    2) inline css - also failed
    3) auto left/right margin - failed
    4) <center> tags wrapping iframe tag - failed
    5) align=center attribute for iframe - failed


    AArrrrggghhhh - why is this so hard? I take it the styling within a frame cannot be altered?

    It's just a damn shame I can't just use <img src="">

    Does anyone know of an include method... or any method?
    Last edited by learning_brain; 08-13-2010 at 05:03 PM.

  2. #2
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Centering image within an iframe

    Have you tried setting display: block and the auto margins?
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  3. #3
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Centering image within an iframe

    Quote Originally Posted by marshian View Post
    Have you tried setting display: block and the auto margins?
    That's one I haven't tried.... *Opens DW*

    ____EDIT________

    Absolutely no difference. I've tried in header style and inline style.
    Last edited by learning_brain; 08-13-2010 at 06:34 PM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Centering image within an iframe

    The document in an iframe is the same as the document you'd get when viewing the URL in a full tab/window. In the case of an image, the browser usually creates a minimal document which you have little control over.

    The style cascade doesn't cross documents, so any style set in the containing document will have no affect on anything in the iframe. When the iframe URL is in the same domain as the containing document, you can inject CSS into this document. Otherwise, there's not much you can do.

    One alternative is somehow to set the width of the iframe to the width of the image, then rely on iframe centering.
    Last edited by misson; 08-13-2010 at 07:38 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Centering image within an iframe

    An admittedly expensive workaround would be to read the image at the server, base64-encode it, and include it in a data: URI, either as the src of an img or as the background-image of the framed page's CSS (with a background-attachment of center center and a background-repeat of no-repeat). If you've never used a data: URI before, it would look like this:

    Code:
    <img src="data:image/jpg;charset=utf-8;base64,base64-encoded_image_file_goes_here">
    That gives you a lot of options, including serving the image directly on your page without incurring the HTTP_REFERER penalty, so you can do pretty much whatever you want at that point. It is bandwidth and computationally expensive, though, so it may not be practical in your situation (particularly if this needs to scale).
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  6. #6
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Centering image within an iframe

    Quote Originally Posted by misson View Post
    The document in an iframe is the same as the document you'd get when viewing the URL in a full tab/window. In the case of an image, the browser usually creates a minimal document which you have little control over.

    The style cascade doesn't cross documents, so any style set in the containing document will have no affect on anything in the iframe. When the iframe URL is in the same domain as the containing document, you can inject CSS into this document. Otherwise, there's not much you can do.

    One alternative is somehow to set the width of the iframe to the width of the image, then rely on iframe centering.
    Thanks misson

    I feared this was the case, but I like the idea of using the image width to create the iframe! My only problem here would be that most of the images are wider than I want the iframe to be, so I'll have to play with some limits or something to restrict the larger ones.

    My first solution would have been to use framesets (simpler) but this causes lots of SEO problems.

    Quote Originally Posted by essellar View Post
    An admittedly expensive workaround would be to read the image at the server, base64-encode it, and include it in a data: URI, either as the src of an img or as the background-image of the framed page's CSS (with a background-attachment of center center and a background-repeat of no-repeat). If you've never used a data: URI before, it would look like this:

    Code:
    <img src="data:image/jpg;charset=utf-8;base64,base64-encoded_image_file_goes_here">
    That gives you a lot of options, including serving the image directly on your page without incurring the HTTP_REFERER penalty, so you can do pretty much whatever you want at that point. It is bandwidth and computationally expensive, though, so it may not be practical in your situation (particularly if this needs to scale).
    This would have been an ideal solution except for the... expense! As the site references large images, this would be horrendous on my bandwidth. I also considered using the GD library to recreate the image, but this too would take more processing power and still leaves me with the bandwidth penalty.

    I'm off to play with widths a bit!

  7. #7
    clairev is offline x10Hosting Member clairev is an unknown quantity at this point
    Join Date
    Feb 2010
    Location
    UK
    Posts
    25

    Re: Centering image within an iframe

    This works for me.

    <table height="100%" align="center" border="0" width="750">
    <tr><td align="center" valign="middle">
    <table border="0" style="text-align: Center" width="100%">
    <tr><td>Centered</td></tr>
    </table>
    </td></tr>
    </table>

    Are we having fun yet?

    Claire

  8. #8
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Centering image within an iframe

    Quote Originally Posted by clairev View Post
    This works for me.

    <table height="100%" align="center" border="0" width="750">
    <tr><td align="center" valign="middle">
    <table border="0" style="text-align: Center" width="100%">
    <tr><td>Centered</td></tr>
    </table>
    </td></tr>
    </table>

    Thanks Claire, but this doesn't use the iframe.

    Yes, a table layout would work wonders but hotlinks from other sites are easily destroyed in tables or normal html. iframes are harder.

    I have however now cracked it...!

    And Claire, there is no way that is your true profile *or is it ;)*

    PHP Code:
    <?php
                $maxWidth
    =728;
                
    $maxHeight410;
                
                
    $factor_hor $maxWidth/$row_Image['WIDTH'];// factor required to reduce width to max width
                
    $testHeight $row_Image['HEIGHT'] * $factor_hor// resultant height
                
                
    $factor_ver $maxHeight/$row_Image['HEIGHT'];    // factor required to reduce height to max height    
                
    $testWidth $row_Image['WIDTH'] * $factor_ver// resultant width
                
                
    if ($testHeight <= $maxHeight){//if the test height is suitable
                    
    $newHeight $testHeight;
                    
    $newWidth $row_Image['WIDTH'] * $factor_hor;
                } else {
    //else assume test width is suitable
                    
    $newWidth $testWidth;
                    
    $newHeight $row_Image['HEIGHT'] * $factor_ver;
                }
                
                
    ?>
            
                <iframe
                    src="<?php echo $row_Image['IMAGEURL'];?>"
                    style="width: <?php echo $newWidth;?>px; height: <?php echo $newHeight;?>px; border: 1px solid #ccc;"
                ></iframe>
    Last edited by learning_brain; 08-14-2010 at 12:54 PM.

  9. #9
    clairev is offline x10Hosting Member clairev is an unknown quantity at this point
    Join Date
    Feb 2010
    Location
    UK
    Posts
    25

    Re: Centering image within an iframe

    Yes, that is me.
    Are we having fun yet?

    Claire

  10. #10
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Centering image within an iframe

    Hmm - not sure if the PM got through Claire as it may have been blocked... but thanks for the advice and your website is extremely..... inviting!

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 10-01-2009, 05:16 PM
  2. Centering website using css
    By wiileg3nd in forum Programming Help
    Replies: 8
    Last Post: 08-23-2008, 01:58 PM
  3. centering websites
    By mikel2k3 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 02-21-2007, 11:46 AM
  4. centering a banner
    By RollerC in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 07-04-2005, 05:54 PM
  5. Centering ads
    By Ericsson in forum Free Hosting
    Replies: 2
    Last Post: 03-20-2005, 12:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers