简体   繁体   English

如何在反应中垂直和水平居中组件?

[英]How to vertically and horizontally center a component in react?

How can I achieve absolute centering with CSS-in-JS?如何使用 CSS-in-JS 实现绝对居中? When I use the following code, my component moves across the screen.当我使用以下代码时,我的组件会在屏幕上移动。 I guess the translate is being applied many times instead of just once.我猜翻译被应用了很多次而不是一次。 What's going on, and how can I fix it without using a library?发生了什么,如何在不使用库的情况下修复它?

  render() {
      return (<ComponentASD 
        style={{
        position: 'absolute', left: '50%', top: '50%',
        transform: 'translate(-50%, -50%)'
      }} />);
    }

Another option is to use flex-box.另一种选择是使用 flex-box。

<div style={{
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
}}>
    Hello world
</div>

Your example code works well:您的示例代码运行良好:

ReactDOM.render(
  <div
    style={{
        position: 'absolute', left: '50%', top: '50%',
        transform: 'translate(-50%, -50%)'
    }}
    >
    Hello, world!
  </div>,
  document.getElementById('root')
);

https://codepen.io/anon/pen/JaLLmX https://codepen.io/anon/pen/JaLLmX

It has to do something with your layout.它必须与您的布局有关。

Thanks for the testing+comments!感谢测试+评论! The styling ended up being fine, the issue was somehow caused by the layout of ComponentASD, which is actually MUI's CircularProgress https://material-ui.com/api/circular-progress/#demos样式最终很好,问题是由 ComponentASD 的布局引起的,这实际上是 MUI 的 CircularProgress https://material-ui.com/api/circular-progress/#demos

I wrapped that component in a div inside of render and applied the styling to the div, which fixed the problem (keeps it stationary and properly aligned).我将该组件包装在渲染内部的div中,并将样式应用于 div,从而解决了问题(使其保持静止并正确对齐)。

This is flexbox you will need to use this on the parent Component.这是 flexbox,您需要在父组件上使用它。

   .Parent {
        display: flex,
        flexFlow: row nowrap,
        justifyContent: center,
        alignItems: center,
    {

It will center the children vertically and horizontally You can also align each component as I've shown you below它将垂直和水平居中孩子您还可以对齐每个组件,如下所示

render() {
  return (<Childcomponent 
    style={{
    display: flex,
    flexFlow: row nowrap,
    justifySelf: center,
    alignSelf: center,
  }} />);
}

If you want to use it in a component just call it in the component:如果要在组件中使用它,只需在组件中调用它:

.Login {
text-align: center;
background-color: #2681C6;
min-height: 100vh;}

Your jsx file sould have something like just to call it, using "className":你的 jsx 文件应该有类似的东西,使用“className”来调用它:

<div className="Login"><div>

If you want to have a properly scrollable list if the window's height is smaller than the content's height you want to center, you can do this ( based on this CSS answer ):如果窗口的高度小于要居中的内容的高度,如果您想要一个可正确滚动的列表,您可以这样做(基于this CSS answer ):

<div
style={{
  display: "table",
  position: "absolute",
  height: "100%",
  width: "100%",
  top: 0,
  left: 0
}}
>
<div
style={{
  display: "table-cell",
  verticalAlign: "middle",
  textAlign: "center"
}}
>
// Here comes the content you want to center
</div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM