【发布时间】:2014-08-21 09:48:45
【问题描述】:
查看此站点上的其他问题,我使用了一种将所有位置设置为 0 并具有自动边距的方法,但这有一些不受欢迎的行为。
如果您垂直调整窗口大小,容器的顶部会从页面顶部移开。当它到达顶部时需要停止。
JSFIDDLE:
HTML:
<div id="container">
<p>This is the container.</p>
<p>If you resize the JSFiddle window horizontally, you will see that the left edge of the box doesn't move past the left edge of the window. This is correct behaviour.</p>
<p>Now if you move the window vertically, the top of this container will disappear off of the top of the window. This is wrong.</p>
</div>
CSS:
#container {
margin:auto;
height:300px;
width:300px;
top:0;
bottom:0;
left:0;
right:0;
position:absolute;
border:1px solid;
padding:10px;
}
【问题讨论】:
-
似乎答案是使用 CSS 表格。 *.com/questions/17520145/…
-
如果我理解你的问题,你需要的是一个最小边距顶部,这显然是弥补的,但这个 buildinternet.com/2009/10/purely-css-faking-minimum-margins 可能会有所帮助。
-
没关系,我忘了vertical-align:中间,我的错。
-
@Joey 看看 flexbox(align-items 和 justify-content 似乎是您需要的):css-tricks.com/snippets/css/a-guide-to-flexbox
-
虽然 flex 可能存在同样的问题。看看这个例子,它仍然做同样的jsfiddle.net/agilius/a9kXU/3