


In the development of front-end pages, there are two ways to display pictures, namely: img tag displays pictures, and the background attribute is set to display background pictures. We know that img images can be centered, so can background images be centered? How to center the background image? This article will introduce to you how to center the background image in CSS. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
First of all, let’s take a look at how to center the background image on the browser using css?
In fact, it is very simple. The background-position attribute in CSS can achieve the horizontal and vertical center alignment of the background image. Below we use a simple code example to introduce how the background-position attribute sets the background. The image is displayed in the center.
We first use the background attribute to display the background image:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>背景图片居中</title> <style> *{margin: 0;padding:0;} .demo{ width: 400px; height: 300px; margin: 50px auto; border: 1px dashed #000; background-image:url(How to center the background image in css? Introduction to the method of centering the background image (code example)) ; background-size:200px 160px ; background-repeat:no-repeat ; } </style> </head> <body> <div class="demo"> </div> </body> </html>
Rendering:
Next let’s look at the background-position attribute Settings:
1. Background-position uses pixels to center the background image (know the size of the background image)
In the example, the lower right corner of the background image coincides with the center of the demo box. To make the background To center the image, you need to make the center of the background image coincide with the center of the demo box. You need to move the background image half of the height downward and half of the width to the right. Therefore, by adding the following code to css, you can center the background image
background-position:100px 70px ; /*宽的一半,高的一半*/
Rendering:
2. Use 50 for background-position % Set the background image to be centered, which is very convenient
background-position:50% 50% ;
3. background-position Use center to center the background image, which is very convenient. (The second center can be omitted)
background-position:center center;
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 A value sets the vertical position; you can decide which method to use depending on your situation. I hope it will be helpful to everyone's learning. For more related tutorials, please visit CSS3 video tutorial, Html5 video tutorial, bootstrap video tutorial!
[Related article recommendations]
3 ways to use css positioning to center the img image (code example)
The above is the detailed content of How to center the background image in css? Introduction to the method of centering the background image (code example). For more information, please follow other related articles on the PHP Chinese website!

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
