<分区>
<分区>
我正在尝试 https://stackoverflow.com/a/22218694/1175080 中描述的以下解决方案.
Alternatively, instead of aligning the content via the container, flexbox can also center the a flex item with an auto margin when there is only one flex-item in the flex container (like the example given in the question above).
这是我的解决方案。
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>
main {
display: flex;
height: 100%;
background: lightgreen;
}
section {
margin: auto;
background: yellow;
}
</style>
</head>
<body>
<main>
<section>Foo</section>
</main>
</body>
</html>
好像不行。 main
元素不会展开以占据页面的 100% 高度。我究竟做错了什么?如何让它发挥作用?
最佳答案
使用 height:100vh
body {
min-height: 100vh;
margin: 0;
}
main {
display: flex;
height: 100vh;
background: lightgreen;
}
section {
margin: auto;
background: yellow;
}
<main>
<section>Foo</section>
</main>
关于html - 如何使用 CSS flex 使元素占据页面的 100% 并将元素居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763847/