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

青灯夜游
Release: 2023-01-11 09:19:03
Original
18035 people have browsed it

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; }
Copy after login

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!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!