Home >Web Front-end >HTML Tutorial >How to center the box in html
How to center a box using HTML
Centering a box is easy using a CSS stylesheet in HTML. There are two main ways to achieve box centering:
Horizontal centering
text-align: center;
Style: This is The simplest way to horizontally center a block-level element such as div
, h1
, or p
. margin: auto;
style: This method is suitable for non-block-level elements (such as inline elements), such as images or embedded videos. It will automatically set the left and right margins of the element so that it is centered. Vertical center
margin: 0 auto;
Style: This style sets the left and right margins of the element to 0
and center it vertically. position: absolute;
style: This style removes the element from the normal document flow and allows you to use the top
and left
attributes to absolutely position it. You can then set these properties to 50%
and offset the element using transform: translate(-50%, -50%);
to center it. Example
The following example shows how to center a box horizontally and vertically using HTML and CSS:
<code class="html"><div style="text-align: center; margin: 0 auto;"> <p>这是一个水平和垂直居中的盒子。</p> </div></code>
Note :
-webkit-
, -moz-
). The above is the detailed content of How to center the box in html. For more information, please follow other related articles on the PHP Chinese website!