css - 元素不会在 Modal 中居中

标签 css twitter-bootstrap react-bootstrap

我目前正在使用 React Bootstrap 模态。我正在尝试使用 flexbox 将模式中的元素居中,但是这样做有很多麻烦。下面是我的代码,以及它目前的样子。

enter image description here enter image description here

import { Modal } from 'react-bootstrap'
import React from 'react';
import { Button } from 'react-bootstrap';
import * as welcome from '../../styles/welcome'

class Welcome extends React.Component{
constructor(props){
   super(props)
   this.state = {
    showModal: true
 }
this.close=this.close.bind(this)
this.open=this.open.bind(this)
}

 componentWillReceiveProps(nextProps){
  debugger
  if(nextProps.modal_content !== this.props.modal_content){
    this.setState({ showModal: true });
 }
}

close(){
  this.setState({ showModal: false });
}

open() {
  this.setState({ showModal: true });
}

 render() {

return (
  <div>
    <Modal style={welcome.welcomeBody} show={this.state.showModal} onHide={this.close}>

        <div style={{display: 'flex', justifyContent: 'center'}}>
           <div style={{backgroundColor: 'black', border: '1px solid black', borderRadius: '100%', width: '50px', height: '50px'}}></div>
       </div>


    </Modal>
  </div>
);
}
 }

导出默认欢迎

最佳答案

您在 image 中应用 flex 以使其居中对齐...这是错误的...

Flex 始终应用于 parent 容器,以便其子项使用 flex 功能。

将您的image 包裹在div 中,然后应用display:flexjustify-content:center 到该 div,以便图像居中对齐。

<div style={{display: 'flex',justifyContent: 'center'}}>
  <img src="pic.png"></img>
</div>

<div style="display: flex;justify-content: center;">
  <img src="http://via.placeholder.com/350x150">
</div>

更新代码

<div style="display: flex;justify-content: center;">
  <div style="background-color:black;border:1px solid;height:50px;width:50px;border-radius:50%;"></div>
</div>

关于css - 元素不会在 Modal 中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740776/

相关文章:

CSS Zoom 不起作用 - iPad OS(v13 - 最新)Safari

Firefox 与 Chrome 中的 HTML 页面呈现问题 : Firefox fail?

javascript - Angular js路由在本地主机中不起作用

reactjs - 采用 React-bootstrap 样式的 Formik

javascript - 如何限制文本区域并从文本区域移动到另一个文本区域

html - 页脚不再停留在页面底部

javascript - 将一个 div 堆叠在隐藏在滚动条上的 Bootstrap 导航栏之上

jquery - 如何使用忙碌图标显示 Bootstrap Modal

twitter-bootstrap - 使用 react-bootstrap 优于 bootstrap 的优势

javascript - 如何使用 reactJS 和 css 在全屏背景中显示图像?