简体   繁体   English

如何将2个iframe彼此相邻放置?

[英]How can I place 2 iframes next to each other?

I need to create a page where I have 2 iframes next to each other with 100% height. 我需要创建一个页面,在其中我有2个iframe并排,且高度均为100%。

The left frame needs to have a fixed width of 140px and the right one needs to take the width of the rest of the screen. 左框架的宽度必须固定为140px,右框架的宽度应为屏幕其余部分的宽度。 Keep in mind that both frames need to have 100% height. 请记住,两个框架都必须具有100%的高度。

Since there are different size screens I can't set a fixed with on the right iframe as I want it to take all the screen after the first 140px; 由于屏幕尺寸不同,因此我无法在右侧iframe上设置固定尺寸,因为我希望它占据前140像素之后的所有屏幕;

I kinds got it to work while using precentage. 在使用precentage时,我觉得它可以正常工作。 But the problem with percentage is the the left menu sometime show very wide 但是百分比的问题是左侧菜单有时显示得很宽

I created a fiddle to show you what I have done so far 我创建了一个小提琴以向您展示到目前为止我做了什么

http://jsfiddle.net/mwg3j17d/16/ http://jsfiddle.net/mwg3j17d/16/

 #main_block { display: block; width: 100% height: 100%; } #left_frame { width: 25%; } #right_frame { width: 75%; } #left_frame, #right_frame { float: left; } iframe { box-sizing: border-box; } .b_footer { padding: 10px; width: 100%; background-color: blue; text-align: center; color: white: font-weight: bold; } 
 <div id="main_block"> <iframe id="left_frame" src=""></iframe> <iframe id="right_frame" src=""></iframe> </div> <div class="b_footer"> Footer </div> 

As you can tell, there are couple of problems with my code. 如您所知,我的代码有几个问题。

  1. The footer background's color for some reason is also showing where under the iframs. 由于某些原因,页脚背景的颜色也显示了iframs的下方。
  2. The second problem is that I am using 25% width for the left iframe where it should be set to 140px 第二个问题是我为左iframe使用25%的宽度,应将其设置为140px
  3. Finally, the height of the iframe is not taking the entire height of the screen. 最后,iframe的高度不会占据整个屏幕的高度。

How can I correct the problems mention above? 我该如何纠正上述问题?

EDITED I also tried to use Table to get the job done but the left iframe does not have the correct width. 编辑我也尝试使用表格来完成工作,但左侧iframe的宽度不正确。 Here is an updated Fiddle to show you http://jsfiddle.net/mwg3j17d/19/ 这是更新的小提琴,向您展示了http://jsfiddle.net/mwg3j17d/19/

You can use width: calc(100% - 140px) to create your right column. 您可以使用width: calc(100% - 140px)创建右列。 Also, your .b_footer style was too large (10px padding + 100% + 10px padding) because you didn't specify box-sizing: border-box , so I added it. 另外,您的.b_footer样式太大(10px填充+ 100%+ 10px填充),因为您没有指定box-sizing: border-box ,所以我添加了它。

  1. Using float takes the elements out of the normal html flow, and has odd side effects if you don't fully understand them. 使用float会使元素脱离正常的html流,如果您不完全了解它们,则会产生奇怪的副作用。 Use display:inline block instead. 改用display:inline块。
  2. Use width: calc(100% - 140px) to create your right column. 使用width: calc(100% - 140px)创建右列。
  3. Use 100vh for the height instead of 100%; 使用100vh作为高度,而不是100%;
  4. You will have issues with the footer because again, 100% + whatever the footer size is always going to be larger than the page height. 页脚会出现问题,因为同样,100%+页脚大小总是大于页面高度。 Easiest solution is to fix the size of the footer, and use that in a height calculation. 最简单的解决方案是固定页脚的大小,并在高度计算中使用它。
  5. I've added html,body { margin:0; padding:0; } 我添加了html,body { margin:0; padding:0; } html,body { margin:0; padding:0; } html,body { margin:0; padding:0; } to remove the margins and padding. html,body { margin:0; padding:0; }删除边距和填充。 If you want them, add them back manually so that all browsers will use the same values, and use the new values in your width/height calcs. 如果需要它们,请手动将其添加回去,以便所有浏览器将使用相同的值,并在宽度/高度计算中使用新值。

 html,body { margin:0; padding: 0; } #main_block { display: block; /* Useless, divs are display:block */ width: 100%; /* Useless, display:block elements are width:100% by default */ height: 100%; /* Fairly useless now, should take children's height */ font-size:0; /* Force space between inline-block elements to be 0 */ } #left_frame { width: 140px; } #right_frame { width: calc(100% - 140px); } #left_frame,#right_frame { display: inline-block; box-sizing: border-box; height: calc(100vh - 50px); } .b_footer { padding: 10px; height: 50px; background-color: blue; text-align: center; color: white; font-weight: bold; box-sizing: border-box; } 
 <div id="main_block"> <iframe id="left_frame" src=""></iframe> <iframe id="right_frame" src=""></iframe> </div> <div class="b_footer"> Footer </div> 

To display them next to eachother, there are several options, in this case, the easiest seems (to me, opinions differ), to add float:left; 为了使它们彼此相邻显示,有几种选择,在这种情况下,最简单的方法(在我看来,观点有所不同)是添加float:left; to both frames. 到两个框架。

As for the problem with the frames not taking the full height, for this you can use height:100vh which means, 100% of the viewport height. 至于框架没有占据整个高度的问题,为此,您可以使用height:100vh ,这是指视口高度的100%。

As for the footer being behind the iframes as well as under them this is fixed by forcing the footer to float at the bottom of the page at all times. 至于页脚位于iframe的下方和下方,可以通过强制页脚始终浮动在页面底部来解决此问题。 This can be done by using position:absolute and bottom:0 as well as left:0 这可以通过使用position:absolutebottom:0以及left:0

As for the width having to be 140px, calc(100vw-140px) will do nicely here 至于宽度必须为140px, calc(100vw-140px)在这里会很好

Your updated code 您更新的代码

HTML HTML

<div id="main_block">
    <iframe src="http://www.w3schools.com" id="left_frame" src=""></iframe>
    <iframe src="http://www.w3schools.com" id="right_frame" src=""></iframe>

</div>
<div class="b_footer">
    Footer
</div>

CSS CSS

html,
body {
    margin: 0;
    padding: 0;
}
#main_block {
    display: block;
    width: 100vw;
    min-height: 100%;
}
#left_frame {
    width: 140px;
}
#right_frame {
    width: -moz-calc(100% - 140px); width: -webkit-calc(100% - 140px); width: calc(100% - 140px);
}
#left_frame,
#right_frame {
    float: left;
    height:100vh;
}
iframe {
    box-sizing: border-box;
}
.b_footer {
    width: 100%;
    height: 100px;
    position: absolute;
    bottom: 0;
    left: 0;
    top:100vh;
    background-color:blue;
    color:white;
    text-align:center;
}

Updated Fiddle 更新小提琴

Hope this helps! 希望这可以帮助!

If you're going to have multiple iFrames that basically fill up the entire page, why don't you go with frameset? 如果您要使用多个基本填充整个页面的iFrame,为什么不使用框架集?

<frameset rows="*,100">
  <frameset cols="140,*">
    <frame src="left.htm">
    <frame src="right.htm">
  </frameset>
  <frame src="footer.htm">
</frameset>

This circumvents all your problems at once. 这可以立即解决您的所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM