This article mainly introduces how to vertically center images in divs using css. It has certain reference value and I hope it can help everyone.
We usually work on pages We often encounter situations where we are asked to display an image in the middle of a div but often don’t know how to do it. Today we will share several commonly used css codes to achieve vertical centering of images in divs
HTML code
<div> <img src="/static/imghwm/default1.png" data-src="images/1.jpg" class="lazy" alt="How to vertically center images in divs using css" > </div>
Method 1
Use position and margin to realize
Allow child elements by setting the absolute positioning attribute to the parent element Relative to div positioning
relative means retaining the original position for positioning and positioning relative to its own original position
absolute means positioning away from the original position and relative to the nearest positioned parent If there is no positioned parent element, it will be positioned relative to the document
Note: To make the top, bottom, left and right of the child element 0, then set margin:auto It will automatically center vertically
The code is as follows
div{ width:200px; height:200px; border: 1px solid #ccc; position: relative;//父元素设置绝对定位 } img{ position: absolute;//相对定位 width:80px; height:50px; top:0; left:0; right:0; bottom:0; margin:auto;//使其垂直居中 }
Rendering
Method 2
Use the three attributes of display: table-cell; vertical-align: middle; text-align: center; to achieve
display:table-cell: will be used as a table cell Display (similar to
vertical-align: middle;Set vertical alignment, applicable to row-level elements
text-align: center: Sets the horizontal alignment. This property sets the horizontal alignment of text within block-level elements by specifying the point at which the line box is aligned.
div{ width:200px; height: 200px; margin:300px auto; display: table-cell;//作为一个表格单元格显示 vertical-align: middle;//设置垂直对齐方式 text-align: center;//设置水平对其方式 border:1px solid #ccc; } img { width:80px; height:50px; }
Rendering
Method 3
Use position, and margin- Implementation of top and margin-left
In this method, attention should be paid to the setting of margin-top and margin-left values. They should be set to half the height and width of the element, and both should be negative values
For example, margin-top: -40px means that the element is 40px upward from the upper boundary, and margin-top: 40px means that it is 40px downward from the upper boundary element
div{ width:200px; height:200px; border: 1px solid #ccc; position: relative; } img{ position: absolute; width:80px; height: 50px; top:50%; left:50%; margin-top: -40px;//向上40px margin-left: -25px;//向左25px }
Rendering
Summary: There are many ways to use css to vertically center images in divs. The above are the three methods I have summarized. You are welcome to add the rest. I hope This article can help everyone learn page layout.
The above is the detailed content of How to vertically center images in divs using css. For more information, please follow other related articles on the PHP Chinese website!

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

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.
