Home >Web Front-end >HTML Tutorial >How to center the box in html5
To center the box in HTML5, there are the following methods: horizontal centering: text-align: centermargin: autodisplay: flex; justify-content: center; vertical centering: vertical-align: middletransform: translate(-50% , -50%); position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
How to center a box in HTML5
In HTML5, there are several ways to center a box horizontally and vertically:
Horizontal Centering
text-align: center
: This property centers the text within the box horizontally. It won't center the entire box horizontally, though. margin: auto
: This property centers the box horizontally, provided the box has a width set. display: flex; justify-content: center;
: This CSS combination places the box in a horizontal flex container and centers it horizontally. Vertical center
vertical-align: middle
: This attribute is used for inline elements ( such as an image) or vertically centered content in a table cell. transform: translate(-50%, -50%);
: This property moves the box vertically and horizontally by 50% of its own height and width, thus Center it. It works with absolutely or relatively positioned boxes. position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
: This CSS combination will Absolutely positioned boxes are centered vertically and horizontally. Additional Tips
The above is the detailed content of How to center the box in html5. For more information, please follow other related articles on the PHP Chinese website!