3

I'm trying to style a modal in ReactJS and would like some help. I'm facing difficulty in trying to center align the content modal. In addition, when styling the modal, is it possible to assign a className and style it in the css page? I've tried doing that but it doesn't work. So I just decided to do inline styling but I can't center align my modal.

            <button onClick={()=> SEOsetModalIsOpen(true)} className="viewmore">
                View More
                <ArrowIcon className="arrowright"/>
            </button>
            <Modal
              isOpen={SEOmodalIsOpen}
              shouldCloseOnOverlayClick={true}
              onRequestClose={()=>SEOsetModalIsOpen(false)}
              style={{
                  overlay: {
                    position: 'fixed',
                    top: 0,
                    left: 0,
                    right: 0,
                    bottom: 0,
                    opacity: 1,
                  },

                  content: {
                    position: 'absolute',
                    width: '500px',
                    height: '300px',
                    top: '200px',
                    left: '500px',
                    right: '500px',
                    bottom: '200px',
                    border: '1px solid #ccc',
                    background: 'blue',
                    overflow: 'auto',
                    WebkitOverflowScrolling: 'touch',
                    borderRadius: '4px',
                    outline: 'none',
                    padding: '20px'
                  }
              }}
              >
              <h2>Search Engine Optimisation</h2>
              <p>Hello World</p>
              <button onClick={()=>SEOsetModalIsOpen(false)}>Close</button>
            </Modal>
            </div>
hoseh
  • 129
  • 1
  • 3
  • 14

2 Answers2

9

You could use this to center it with absolute positioning:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
CodeKarl
  • 128
  • 6
2

instead of setting top, left, bottom, right to center your content, you can set

{
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
}
Deco
  • 5,112
  • 1
  • 16
  • 17
Sina
  • 379
  • 2
  • 9