Current location - Recipe Complete Network - Complete cookbook of home-style dishes - How does css html center the img tag horizontally, vertically and horizontally?
How does css html center the img tag horizontally, vertically and horizontally?
1, the first css code is shown in the figure. Set the display to table cells, with text centered and vertical centered.

2. Open the browser to view the results, and the picture is in the center.

3. The css code of the second method is shown in the figure. Div is set to relative positioning, img is set to absolute positioning, then left: 50%, up: 50%. At this point, the upper left corner of the picture will be located in the center of the div. Important: Move the picture up by half the height of the picture and left by half the width of the picture. Right in the middle of the div.

4. View the results in the browser, with the picture centered horizontally and vertically.