[英]Removing vertical white space between divs, especially after hitting a breakpoint
I'm working on a responsive page design at the moment and I'm running into an issue with white-space between the divs, especially after hitting breakpoints. 目前,我正在进行响应式页面设计,并且在div之间出现空白,尤其是在达到断点之后。
body, html { padding: 0; margin: 0; } .header { padding-top: 5px; background-color: red; width: 100%; } .sub-header { padding: 5px; margin: 0px; background-color: yellow; width: 100%; } .main-content { padding: 5px; background-color: blue; width: 100%; } .footer { position: absolute; bottom: 0; padding: 5px; background-color: green; width: 100%; } @media screen and (max-width: 320px) { .sub-header { display: none; } } }
<div class="header">Header <div class="sub-header">Sub-Header</div> </div> <div class="main-content">Auto adjust size</div> I want to have the blue div take up the remaining space in this white space, as well as after the sub-header is removed at the break point. <div class="footer">footer</div>
Here's a quick mock up of what I'm experiencing: http://jsfiddle.net/gaych7vp/6/ 这是我所经历的快速模拟: http : //jsfiddle.net/gaych7vp/6/
I understand what I have to do in order to make it take up the remainder of the white space before it hits a breakpoint (I'm assuming just tweaking the height values), but how would I go about making the blue div take up the remaining white space that gets created when the yellow div gets hidden after hitting the breakpoint? 我知道我必须做些什么才能使其在达到断点之前占据空白的其余部分(我假设只是调整高度值),但是我将如何使蓝色div占据击中断点后黄色div隐藏时剩下的空白区域?
I'm still really new to javascript but from other answers I've read it could be done by creation a function that finds the height of the browser and then subtracts it from the other divs. 我对javascript确实还很陌生,但是从我读过的其他答案来看,可以通过创建一个功能来完成该功能,该功能可以找到浏览器的高度,然后从其他div中减去它。 Is that possible and if so, how could I accomplish that? 那有可能吗,如果可以的话,我该怎么做呢?
Use position:absolute
with different top
values 使用position:absolute
不同的top
值
.main-content {
position:absolute;
top:51px;
bottom:0px;
}
and 和
@media screen and (max-width: 320px) {
.main-content {
top: 23px;
}
}
Another approach is using display:table
and display:table-row
另一种方法是使用display:table
和display:table-row
body, html{
width:100%;
height: 100%;
}
body{
display:table;
}
.main-content {
width: 100%;
height: 100%;
display:table-row;
}
Make a div fill the height of the remaining screen space 使div填充剩余屏幕空间的高度
You can use calc on the .main-content
div to calculate the size, but you would need to set the heights of the header, footer, and subheader divs. 您可以在.main-content
div上使用calc来计算大小,但是您需要设置页眉,页脚和子页眉div的高度。 Seems to me though you could just give the body a background color of blue, and achieve the same thing? 在我看来,虽然您可以给身体一个蓝色的背景色,并实现相同的目的?
There's no need for a JavaScript solution here. 这里不需要JavaScript解决方案。
The white area is caused because you are using position: absolute
to force the footer to the bottom of the window, regardless of the content. 导致白色区域是因为您使用的是position: absolute
,无论内容如何,都将页脚强制到窗口底部。
This isn't the normal way to achieve this, you'll run into issues later on when you do add content to your main-content
div. 这不是实现此目的的正常方法,稍后在向main-content
div中添加内容时,您会遇到问题。 You'll find that the footer will be positioned over this content (this will also happen if you shrink the window vertically). 您会发现页脚将位于此内容上(如果垂直缩小窗口,也会发生此情况)。
I think what you'd like to do, is give the main-content
div a min-height:
, this way, the page won't collapse and look terrible if there is little content, but it will stretch naturally when more content is added. 我认为您想要做的是给main-content
div设置一个min-height:
这样,即使内容很少,页面也不会折叠并且看起来很糟糕,但是当内容更多时,页面自然会拉伸添加。
You can also and remove the position: absolute
from the footer
div. 您还可以从footer
div中删除position: absolute
。
Here is a demonstration: http://jsfiddle.net/t46aopas/ 这是一个演示: http : //jsfiddle.net/t46aopas/
** UPDATE ** **更新**
If you'd like a dynamic solution, I've created a heavily annotated JavaScript example here: http://jsfiddle.net/nahgdkaw/ 如果您想要一个动态解决方案,我在这里创建了一个带有大量注释的JavaScript示例: http : //jsfiddle.net/nahgdkaw/
(I included lots of comments since you said you were new to JavaScript ;) ) (因为您说过您是JavaScript的新手,所以我发表了很多评论;))
That should hopefully help you along the way a little. 希望这会对您有所帮助。
Be aware that if the content inside the .main-content
div is larger than the .main-content
div area, the div will NOT expand to accommodate it. 请注意,如果.main-content
div中.main-content
大于.main-content
div区域,则div将不会扩展以容纳它。
You can however use the code provided and add in an if
statement to, before resizing the .main-content
div, check if the .main-content
content is larger than the available area (you may need to add a wrapper div around the .main-content
content). 但是,您可以使用提供的代码并在if
语句中添加.main-content
语句,然后再调整.main-content
div的大小,检查.main-content
内容是否大于可用区域(您可能需要在.main-content
周围添加包装div .main-content
内容)。 If so, then resize the .main-content
div to match the .main-content
content height (otherwise, perform the resize as it currently is). 如果是这样,则调整.main-content
div的大小以匹配.main-content
内容的高度(否则,按当前状态执行调整大小)。
(Also, I strongly advise against using HTML tables for anything other than tabular data) (此外,我强烈建议不要将HTML表格用于表格数据以外的任何其他内容)
I edited my original answer but don't have the reputation points necessary to add a comment to notify you. 我修改了原始答案,但没有添加评论以通知您的信誉点。 I'll remove this answer after you've seen my updated answer above. 在您看到上面的最新答案后,我将删除此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.