Search code examples
reactjsreact-bootstrap

Centering Modal Title and button in React Bootstrap


I am trying to implement React Modal for my project. I want to center the Modal Title and Modal Button. I tried to use flex and just-content-center but it didn't work. Any suggestions would be valuable.

<Modal show={modalShow} onHide={this.handleClose} aria-labelledby="contained-modal-title-vcenter" centered>
    <Modal.Header closeButton>
        <div className="d-flex justify-content-center">
            <Modal.Title>{isVerified?"Submitted succesfully":"Failed to submit"}</Modal.Title>
        </div> 
    </Modal.Header>
         <Modal.Footer>
    <Button variant={`${isVerified == false?"danger ":"success"}`} onClick={this.handleClose} centered>
        {isVerified?"Ok":"Try again"}
    </Button>
    </Modal.Footer>
</Modal>

Solution

  • I tried different classes but what is working is this:

    import React from "react";
    import { Modal, Button } from "react-bootstrap";
    
    const ModalPage = ({ modalShow, handleClose, isVerified }) => {
      return (
        <Modal
          show={modalShow}
          onHide={handleClose}
          aria-labelledby="contained-modal-title-vcenter"
          centered
        >
          <Modal.Header
            closeButton
            style={{
              display: "flex",
              justifyContent: "center",
              alignItems: "center",
            }}
          >
            <Modal.Title>
              {isVerified ? "Submitted succesfully" : "Failed to submit"}
            </Modal.Title>
          </Modal.Header>          
          <Modal.Footer
            style={{
              display: "flex",
              justifyContent: "center",
            }}
          >
            <Button
              variant={`${isVerified == false ? "danger " : "success"}`}
              onClick={handleClose}
              centered
            >
              {isVerified ? "Ok" : "Try again"}
            </Button>
          </Modal.Footer>
        </Modal>
      );
    };
    
    export default ModalPage;