How to center the background image in css

WBOY
Release: 2021-12-09 10:12:32
Original
18775 people have browsed it

In css, you can use the "background-position" attribute to center the background image. This attribute is used to set the starting position of the background image. When the value of the attribute is "center", the background image will be horizontal and vertical. Centered display, syntax "element {background-position:center}".

How to center the background image in css

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

How to center the background image in css

In css, the position of the background image can be set through the background-position attribute. The following example explains how to center the background image in CSS.

The example is as follows:

1. Create a new html file and name it test.html, which is used to explain how to center the background image in css. Create a module using div tags for testing. Set the class attribute of the div tag to test, which is used to set the following styles.

In the css tag, set the style of the div through class, define its width as 300px, height as 300px, and border as 1px gray border.

How to center the background image in css

2. In the css tag, set the background image of the div to 1.jpg through the background attribute, and use the background-position attribute to set the background image to center (center) show.

How to center the background image in css

Open the test.html file in the browser to check the effect.

Summary:

1. Use the div tag to create a module and set the class attribute of the div tag to test, which is used to set the following styles.

2. In the css tag, set the style of the div through class, define its width as 300px, height as 300px, and border as 1px gray border.

3. In the css tag, set the background image of the div to 1.jpg through the background attribute, and use the background-position attribute to set the background image to be displayed in the center.

(Learning video sharing: css video tutorial)

The above is the detailed content of How to center the background 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