How to use CSS to fill the parent container div with img images and adjust the container size

How to use CSS to fill the parent container div with img images and adjust 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 concludes this article on how to use CSS to fill the parent container div with img images and achieve adaptive container size. For more information about how to use CSS to fill container content with img images, please search previous articles on 123WORDPRESS.COM or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Implementation script for scheduled database backup in Linux

>>:  SQL implementation of LeetCode (181. Employees earn more than managers)

Recommend

Comprehensive understanding of HTML Form elements

As shown below: XML/HTML CodeCopy content to clip...

HTML table tag tutorial (19): row tag

The attributes of the <TR> tag are used to ...

How to use vw+rem for mobile layout

Are you still using rem flexible layout? Does it ...

CentOS7.5 installation of MySQL8.0.19 tutorial detailed instructions

1. Introduction This article does not have screen...

js to realize a simple puzzle game

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

Vue implements drag progress bar

This article example shares the specific code of ...

Button is stretched on both sides in IE

When you write buttons (input, button), you will f...

Solve the problem that Mysql5.7.17 fails to install and start under Windows

Install MySQL for the first time on your machine....

MySQL 8.0.18 installation tutorial under Windows (illustration)

Download Download address: https://dev.mysql.com/...

JavaScript to achieve a simple message board case

Use Javascript to implement a message board examp...

Analysis of the causes of accidents caused by Unicode signature BOM

Maybe you are using include files here, which is u...

Install Zookeeper under Docker (standalone and cluster)

After starting Docker, let's take a look at t...