【发布时间】:2017-06-26 19:37:40
【问题描述】:
我目前设置了一个 ReactJS + React Router 项目,并且在 Chrome 开发工具中的元素显示:
<body>
<div class="wrapper">
<div data-reactroot>
<div>
<div class="container">Center Me</div>
</div>
</div>
</div>
</body>
class="wrapper" 有样式,但没有样式:
.container {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
然而<div class="container">Center Me</div> 位于顶部并水平居中但不是垂直居中。
我检查了那个 div 的大小,它非常短,但占据了浏览器页面的整个宽度。
我可能做错了什么?我怎样才能居中<div class="container">Center Me</div>?我什至尝试将100% for width and height 设置为更高的级别,但仍然无法正常工作。
【问题讨论】:
-
你在里面对齐的元素应该是一个标签,比如一个。您需要在 Html、Body 上设置样式以告知您希望最小高度和最小宽度为 100vh(视图高度)和 100vw(视图宽度)。
.container的每个家长也需要height: 100%;@Win 那么我应该在树的哪个位置添加高度和宽度?@MichaelCoker 添加到每个父母,但仍然相同......确保包含:- html, body { min-height: 100vh;最小宽度:100vw; }
标签: javascript html css reactjs react-router