Home > Web Front-end > CSS Tutorial > How to center an image in css

How to center an image in css

coldplay.xixi
Release: 2022-03-31 18:42:32
Original
58022 people have browsed it

Methods to center the picture: 1. Use the statement "background-position: half the width of the picture and half the height;"; 2. Use the statement "background-position:50%"; 3. Use "background-position" :center;" statement.

How to center an image in css

The operating environment of this tutorial: windows7 system, css3 version, DELL G3 computer.

Css method to center the image:

1. background-position uses pixels to set the background image to be centered (know the size of the background image)

background-position:100px 70px ;  /*宽的一半,高的一半*/
Copy after login

Rendering:

How to center an image in css

2. Background-position uses 50% to set the background image to be centered, which is very convenient

background-position:50%;
Copy after login

3. Background-position uses center to center the background image, which is very convenient. (The second center can be omitted)

background-position:center center;
Copy after login

The above three setting methods of background-position can all achieve the centering of the background image. The first value of the background-position attribute sets the horizontal position, and the second value sets the vertical position.

Related tutorial recommendations: CSS video tutorial

The above is the detailed content of How to center an image in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template