ReactJS 居中对齐模态框

3
我正在尝试在ReactJS中样式化模态框,需要一些帮助。我在试图将内容模态居中时遇到了困难。此外,在样式化模态框时,是否可以分配一个className并在css页面中进行样式化?我尝试过这样做,但它不起作用。所以我只能使用内联样式,但无法将模态框居中对齐。
            <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>
2个回答

9
你可以使用绝对定位将其居中:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

2

不必设置topleftbottomright来将您的内容居中,您可以设置

{
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接