CSS fills the parent container div with img images and adapts to the container size

CSS fills the parent container div with img images and adapts to the container size

When multiple images are introduced into a page, the image sizes may be inconsistent. However, if we want to display them in a consistent size, directly setting the image size will cause the image to deform. This is the problem we encountered. Let's see the solution.

<div>
        <img src="imported image address" alt="">
</div>

Method 1: Center the img element vertically and set its height and width to a minimum full-screen value

div{
    position:relative;
 width: 100px;
    height: 100px;
    overflow:hidden;
}
 div img{
    position: absolute;
    top: 50%;
    left: 50%;
    display: block;
    min-width: 100%;
    min-height: 100%;
    transform:translate(-50%,-50%);
}

Method 2: Set the CSS style of img and add object-fit: cover, which is similar to the background-size: cover of the background image in CSS3;

div{
  width: 100px;
  height: 100px;
 
}
div img{
    width: 100%;
    height: 100%;
    object-fit:cover;
}

This is the end of this article about how to use CSS to fill the parent container div with img images and adapt to the container size. For more related information about how to use CSS to fill the parent container content with img, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Process parsing of reserved word instructions in Dockerfile

>>:  Three ways to realize the horizontal centering of elements and the understanding of the concepts of fixed layout and flow layout

Recommend

Comparative Analysis of MySQL Binlog Log Processing Tools

Table of contents Canal Maxwell Databus Alibaba C...

MySQL GROUP_CONCAT limitation solution

effect: The GROUP_CONCAT function can concatenate...

Vue parent-child component mutual value transfer and call

Table of contents 1. Parent passes value to child...

Using Docker Enterprise Edition to build your own private registry server

Docker is really cool, especially because it'...

Install MySQL in Ubuntu 18.04 (Graphical Tutorial)

Tip: The following operations are all performed u...

MySQL permission control details analysis

Table of contents 1. Global level 2. Database lev...

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box mod...

Several commonly used single-page application website sharing

CSS3Please Take a look at this website yourself, ...

How to write CSS elegantly with react

Table of contents 1. Inline styles 2. Use import ...

VMware installation of Ubuntu 20.04 operating system tutorial diagram

Memo: Just experience it. Record: NO.209 This exa...

Practical explanation of editing files, saving and exiting in linux

How to save and exit after editing a file in Linu...

js to achieve a simple lottery function

This article shares the specific code of js to im...