'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 1:[1]
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;
Solution 2:[2]
For centering the header title and close button:
If you increase the horizontal padding and use ms-auto on the title you can move the close button to an appropriate spot while keeping the title text centered.
<Modal>
<Modal.Header className="px-4" closeButton>
<Modal.Title className="ms-auto">Title</Modal.Title>
</Modal.Header>
<Modal.Body>Body</Modal.Body>
</Modal>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Konstantin Modin |
Solution 2 |