The horizontal centering of the image can be achieved through the CSS margin or text-align properties. 1. Use margin: 0 auto; to set the left and right margins to be automatically calculated to achieve centering. 2. Set the container text-align: center; and the image element display: inline-block; to center it as an inline block element.
CSS property to achieve horizontal centering of images
In CSS, you can set margin
or text-align
attribute to achieve horizontal centering of the image.
1. Use the margin attribute
<code class="css">img { margin: 0 auto; }</code>
margin
attribute to set the top, bottom, left, and right margins of the image element. By setting margin: 0 auto;
, the left and right margins are set to auto
, which means that the browser will automatically calculate the left and right margins based on the width of the container, thereby centering the image horizontally.
2. Use the text-align attribute
<code class="css">.container { text-align: center; } img { display: inline-block; }</code>
text-align
property to set the text alignment of elements within the container. Set the container's text alignment to center by setting .container { text-align: center; }
. Then set the picture element to display: inline-block;
to make it an inline block element, thereby centering the picture horizontally.
The above is the detailed content of What attributes can be used in css to center the image horizontally?. For more information, please follow other related articles on the PHP Chinese website!