【发布时间】:2018-04-12 06:21:12
【问题描述】:
如何正确居中#content 而不会溢出#container? margin: auto 有点用,但对我来说似乎是个 hack,我不想在 CSS Flexbox 中使用任何边距。
请记住,#container 具有 position: fixed。
这是演示问题的代码:[View in JSFiddle ↗]
document.getElementById('content').innerHTML = [...Array(100).keys()].join('<br>')
#container {
position: fixed;
background: lightblue;
top: 0; bottom: 0;
left: 0; right: 0;
display: flex;
align-items: center;
justify-content: center;
overflow: auto;
}
#content {
border: 1px solid green;
/* uncomment the hack below to get desired behavior */
/* margin: auto */
}
<div id="container">
<div id="content">
</div>
</div>
您可以通过取消注释 margin: auto 来检查所需的行为,问题是如何仅使用 flex- 属性和不使用 margin: auto 来实现相同的结果。
【问题讨论】:
标签: css flexbox overflow css-position margin