Centering method: 1. Set relative positioning for the box and absolute positioning for the picture; then adjust the position of the picture. 2. Set the img image element as a block-level element; then use table-cell and "vertical-align:middle;" to center it. 3. Use flexbox layout to center.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
css to center the image in the div box
##First method: Use the position attribute of css
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> .div1 { width: 200px; height: 200px; border: 1px solid #000000; position: relative; } img { width: 100px; height: 100px; position: absolute; margin: auto; top: 0; left: 0; right: 0; bottom: 0; } </style> </head> <body> <div class="div1"> <img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" / alt="How to center the image in the css box" > </div> </body> </html>Rendering:
Second: Use the new attributes of css3 table-cell, vertical-align:middle;
nbsp;html> <meta> <style> .div { width: 200px; height: 200px; border: 1px solid #000000; display: table-cell; vertical-align: middle; } img { width: 100px; height: 100px; display: block; margin: auto; } </style> <div> <img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" alt="How to center the image in the css box" > </div>Effect:
CSS video tutorial]
Third type: Use Flexbox layout
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> .div { width: 200px; height: 200px; border: 1px solid #000000; display: flex; /*!*flex-direction: column;*!可写可不写*/ justify-content: center; align-items: center; } img { width: 100px; height: 100px; display: block; margin: auto; } </style> </head> <body> <div class="div"> <img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" / alt="How to center the image in the css box" > </div> </body> </html>Effect:
The fourth way: using the transform attribute (disadvantage: need to support Html5)
nbsp;html> <meta> <style> .div { width: 200px; height: 200px; border: 1px solid #000000; position: relative; } img { width: 100px; height: 100px; position: absolute; top: 50%; left: 50%; -ms-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } </style> <div> <img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" alt="How to center the image in the css box" > </div>Rendering:
Programming Video! !
The above is the detailed content of How to center the image in the css box. 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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
