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

CSS to implement QQ browser functions

Code Knowledge Points 1. Combine fullpage.js to a...

The table merges cells and the img image to fill the entire td HTML

Source code (some classes deleted): Copy code The ...

CSS3 implements the sample code of NES game console

Achieve resultsImplementation Code html <input...

Summary of frequently used commands for Linux file operations

0. New operation: mkdir abc #Create a new folder ...

Share 8 MySQL pitfalls that you have to mention

MySQL is easy to install, fast and has rich funct...

How to monitor Windows performance on Zabbix

Background Information I've been rereading so...

jQuery implements Table paging effect

This article shares the specific code of jQuery t...

JSONP cross-domain simulation Baidu search

Table of contents 1. What is JSONP 2. JSONP cross...

Practical record of MySQL 5.6 master-slave error reporting

1. Problem symptoms Version: MySQL 5.6, using the...

Six tips to increase web page loading speed

Secondly, the ranking of keywords is also related ...

jQuery implements the drop-down box for selecting the place of residence

The specific code for using jQuery to implement t...

Specific use of MySQL segmentation function substring()

There are four main MySQL string interception fun...

Complete steps to use mock.js in Vue project

Using mock.js in Vue project Development tool sel...

React implements the sample code of Radio component

This article aims to use the clearest structure t...

MySql5.7.21 installation points record notes

The downloaded version is the Zip decompression v...