When beautifying web pages, you will always use image centering. Do you know what are the three ways to center images in CSS? Let’s take a look below.
1. Use display:table-cell, the specific code is as follows:
html code is as follows:
<div class="img_wrap"> <img src="/static/imghwm/default1.png" data-src="wgs.jpg" class="lazy" alt="In two minutes, you will learn three ways to center an image in CSS." > </div>
css code is as follows:
.img_wrap{ width: 400px; height: 300px; border: 1px dashed #ccc; display: table-cell; //主要是这个属性 vertical-align: middle; text-align: center; }效果如下:
2. Use background method:
html code is as follows:
<p class="img_wrap"></p>
css code is as follows:
.img_wrap{ width: 400px; height: 300px; border: 1px dashed #ccc; background: url(wgs.jpg) no-repeat center center; }
The effect is as shown below:
3. Use a p tag outside the picture, and set the line-height to center the picture vertically:
The html code is as follows:
<div class="img_wrap"> <p><img src="/static/imghwm/default1.png" data-src="wgs.jpg" class="lazy" alt="In two minutes, you will learn three ways to center an image in CSS." ></p> </div>
css code is as follows:
*{margin: 0px;padding: 0px} .img_wrap{ width: 400px; height: 300px; border: 1px dashed #ccc; text-align: center;} .img_wrap p{ width:400px; height:300px; line-height:300px; /* 行高等于高度 */ } .img_wrap p img{ *margin-top:expression((400 - this.height )/2); /* CSS表达式用来兼容IE6/IE7 */ vertical-align:middle; border:1px solid #ccc; }
The rendering is as follows:
Thank you for reading, I hope you will benefit a lot
Recommended tutorial: "CSS Tutorial"
The above is the detailed content of In two minutes, you will learn three ways to center an image in CSS.. For more information, please follow other related articles on the PHP Chinese website!

Here's a container with some child elements:

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:


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

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment