search
HomeWeb Front-endCSS TutorialHow to center and adapt images in css

How to center and adapt images in css

Apr 09, 2019 am 09:42 AM
csspicture

The mobile terminal basically requires image adaptation processing, such as carousel images, product cover images, etc.; the images posted by users are not of the specified size. So, do friends who are doing mobile development encounter difficulties in adapting to images? Let me tell you, in fact, CSS has an object-fit attribute, which can be very convenient for us to adjust images. This article is the method of adjusting images in CSS compiled by the editor.
So what can be done? It just so happens that css3 has the

object-fit attribute Let’s talk about each attribute value

1.object-fit: cover;

This attribute value is the most commonly used one on the mobile terminal. Look at the example below

<img src="/static/imghwm/default1.png"  data-src="http://xxiaoyuan.top/static/js/upload/wz/152826027010892.jpg"  class="lazy"   alt="">

//css部分
img{
    border: 2px red solid;
	width: 300px;    
	height: 300px;
    object-fit: cover;
}

The cover is to fill the entire content in the center. One side will be completely filled, and the rest will be cropped (the filling here will definitely fill the entire content)

Rendering

Original image size ratio

2.object-fit: fill;

fill is to fill the entire content, but will not exceed the content. When there is After filling one side, if there is still a blank space, it will be pulled up to fill the entire content

Rendering

See the original picture above

3.object-fit: contain;## When

#contain is filled, it will not exceed the content. When one side is filled, the filling ends and the image ratio will not be increased or changed. In this way, when the image is not enough to fill the entire content, there will be one side left blank.

Rendering

See the original picture above

4.object-fit: scale-down;

scalc-down feels the same as contain, but I don’t see anything. Different, if anyone knows, please feel free to communicate.

5.object-fit: none;

none is the default attribute value, no filling will be given

Summary: Compatibility Basically, all mobile browsers are compatible, unless those are older than you. If the above content is not very clear, it can be compared to a picture that is gradually enlarged (filled) from very small to very small in the middle of the aspect ratio content you set. If you still can't understand it, maybe my language organization is not good enough. Then go to the official documentation

[Recommended course:

css video course

]

The above is the detailed content of How to center and adapt images in css. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

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

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

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

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

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

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

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.

Revisiting Image MapsRevisiting Image MapsMay 07, 2025 am 09:40 AM

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

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.