提问者:小点点

如何将子元素反应中的内容居中


组件返回了以下代码:

                <div style={{marginLeft: 330}}>
                    <PostForm />
                    {this.state.posts.map(post => (
                        <Post key={post.id} post={post} />
                    ))}
                </div>

我如何使用内联样式使postform和Post组件在所有设备上的屏幕居中。


共1个答案

匿名用户

您可以将display:'flex',justifyContent:'center',alignitems:'center'添加到父组件(在本例中是div):

    <div style={{
        marginLeft: 330,
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center'
    }}>
        <PostForm />
        {this.state.posts.map(post => (
            <Post key={post.id} post={post} />
        ))}
    </div>