React | Center 组件

在 Flutter 中有 Center 组件,效果就是让子组件整体居中,挺好用。

React 中虽然没有对应的组件,但是可以简单封装一个:

  • index.less
.container {
   
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: center;
  height: 100%;
}
  • index.tsx
import styles from './index.less';

interface CenterProps {
   
  children: React.ReactNode;
}

const Center: React.FC<CenterProps> = ({
    children }) => {
   
  return <div className={
   styles.container}>{
   children}</div>;
};

export default Center;

使用:

import Center from './Center';

const CenterPage = () => {
   
  return (
    <div>
      good
      <div style={
   {
    height: '200px', backgroundColor: 'green' }}>
        <Center>
          <div style={
   {
    backgroundColor: 'orange' }}>
            <div
              style={
   {
    backgroundColor: 'red', height: '50px', width: '100px' }}
            >
              古德古德
            </div>
            <div
              style={
   {
    backgroundColor: 'blue', height: '50px', width: '60px' }}
            >
              古德2
            </div>
          </div>
        </Center>
      </div>
    </div>
  );
};

export default CenterPage;

效果:

在这里插入图片描述


补充:
Ant Design 的 Flex 组件也可以轻松让子组件居中,不过 5.10.0 版本才开始提供该组件:

https://ant-design.antgroup.com/components/flex-cn

相关推荐

  1. vue 通讯

    2024-02-04 00:20:03       31 阅读
  2. Hadoop YARN

    2024-02-04 00:20:03       37 阅读
  3. flutter Pageview

    2024-02-04 00:20:03       36 阅读
  4. vue

    2024-02-04 00:20:03       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-04 00:20:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-04 00:20:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-04 00:20:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-04 00:20:03       18 阅读

热门阅读

  1. Ubuntu中rosdep update报错的解决办法

    2024-02-04 00:20:03       30 阅读
  2. 质量管理 了解

    2024-02-04 00:20:03       35 阅读
  3. 软件工程知识梳理5-实现和测试

    2024-02-04 00:20:03       33 阅读
  4. 深度学习有何新进展

    2024-02-04 00:20:03       28 阅读
  5. Git 的基本概念和使用方式

    2024-02-04 00:20:03       27 阅读
  6. EasyExcel多线程导出并实现Zip压缩

    2024-02-04 00:20:03       32 阅读
  7. C# 浅克隆与深克隆

    2024-02-04 00:20:03       30 阅读