mosakashaka
mosakashaka

Reputation: 587

How to center iframe horizontally when there's a vertical scroll bar?

I have a top navigator, and an iframe below the navigator which load the content. The layout is kind of like

<body>
<div style="text-align:middle">
    <div id="nav"></div>
    <iframe></iframe>
</div>
</body>

The navigator is set to fixed width to match the width of iframe content which is not full screen width. So that the navigator and the iframe are aligned at both sides.

But when iframe's height grows beyond the screen, the vertical scroll bar for the iframe shows up and the the iframe becomes a little left(no longer in the absolute horizontal position) and not aligned with the top navigator.

How could I make the iframe always showing at the center even with a vertical bar?

I think this should be a common issue but haven't searched out a similar question here...

Edit 1: Attach a full sample here to illustrate this question.Here index is the main page, iframe2.html is a frame without vertical bar and iframe.html is the one with a bar. The blue block(iframe) is not aligned with the other two:

index.html:

 <html>
<head></head>
<style type="text/css">
iframe {
    width : 100%;
    padding : 0;
    margin: 0 auto;
    display : block;
}
</style>
<body>
    <div style="text-align:center;margin:0 auto;overflow:hidden">
    <div style="background-color:red;width:900px;margin:0 auto;padding:8px 0 8px 0">
        <span>test</span>
    </div>
    <iframe frameborder="0" scrolling="auto" src="iframe2.html" style="height:200px;"></iframe>
    <iframe frameborder="0" scrolling="auto" src="iframe.html" style="height:100%;"></iframe>
    </div>
</body>
</html>

iframe2.html

<html>
<head></head>
<body style="padding:0px;margin:0px;">
    <div style="width:900px;height:190px;background-color:green;margin:0 auto"></div>
</body>
</html>

iframe.html

<html>
<head></head>
<body style="padding:0px;margin:0px;overflow-y:scroll">
    <div style="width:900px;height:2000px;background-color:blue;margin:0 auto"></div>
</body>
</html>

Result:

enter image description here

Upvotes: 0

Views: 2634

Answers (2)

Callan Heard
Callan Heard

Reputation: 727

After my comment (as this seemed to help you with the edits you have made):

Perhaps always force scrollbar even when it is not needed, and then align the navbar to that? body { overflow-y: scroll; }

and further to your reply, I would suggest the simplest way to keep the elements aligned would be to ensure they are the same width. As you are now forcing the scrollbar permanently, perhaps the easiest way to do this would be to add to the width of the first element, or remove from the width of the second, to account for the width of the scrollbar.

Although this would be very browser dependant as each browser may use a slightly different width scrollbar, as per this article, I suggest altering whichever width by 17 pixels, and see if that achieves the effect you are after.

UPDATE

Apologies, I misunderstood what you were after. The reason you are experiencing this issue is because you are getting confused between styling the iframe element and the content within the document it is displaying.

By setting the <div> within the 'iframe.html' files to a width of 900px, you are only styling the content being displayed. The 'outer' iframe element is being styled to 100% width, and so will span the full width of the window. Because of this, the centered content will be offset by the horizontal scrollbar, giving the appearance of not being aligned - however the actual iframe is not moving at all.

It is only possible to align the edges of two elements, regardless of their position, is for them to have the same width (obviously, as otherwise the edges could never line up). To do this, style the <iframe> to be of the correct width - what you do with the content behind that is then unimportant. This way, the width of the scrollbar will then be taken into account automatically, and the total width adjusted accordingly.

Basically, in the styling for the iframe, change width: 100%; to width: 900px;.

Here's a Fiddle.

I've tried to create a diagram to help explain:

iframes

On the left the content is offset by the scrollbar, whereas on the right, the element is styled and centered, not the content, and so the scrollbar just overlaps the content.

You may also like to take a look at some documentation and tutorials for iframes.

Upvotes: 0

Jerad Rutnam
Jerad Rutnam

Reputation: 1546

You can center the iframe using css,

iframe {
  margin: 0 auto;
  display: block;
}

See the example: https://jsfiddle.net/bnby6umd/

Upvotes: 1

Related Questions