Home>Article>Web Front-end> How to set the background image to be centered in html/css

How to set the background image to be centered in html/css

青灯夜游
青灯夜游 Original
2021-09-09 18:09:12 18051browse

In html/css, you can use the background-position attribute to set the background image to be centered. You only need to add the "background-position: center;" style to the element where the background image is set, and you can set the background image to be in the center. Centered horizontally and vertically.

How to set the background image to be centered in html/css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

html/css steps to set the background image to be centered

Open the front-end development tool and create a new html code page

How to set the background image to be centered in html/css

Create a div tag for setting the background color on the html code page, and then add class = "bg-img"

How to set the background image to be centered in html/css

to this tag ##Set background image. Create a style tag, and then set the background image, non-repeating image, width, and height styles for the class bg-img in the tag. Style code:

.bg-img{ height: 100vh; width: 100%; background-image: url(img/car.jpg); background-repeat: no-repeat; }

How to set the background image to be centered in html/css

Save the html code and open it with a browser. At this time, you will find that the background image on the browser is displayed in the upper left corner.

How to set the background image to be centered in html/css

Go back to the html code page and add the style

background-position: centerin the bg-img class.

How to set the background image to be centered in html/css

After saving the html code and refreshing the browser, you will find that the background image on the browser has been automatically centered.

How to set the background image to be centered in html/css

Recommended tutorials: "

html video tutorial", "CSS video tutorial"

The above is the detailed content of How to set the background image to be centered in html/css. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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