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

CSS3 transition to achieve underline example code

This article introduces the sample code of CSS3 t...

Summary of the differences between MySQL storage engines MyISAM and InnoDB

1. Changes in MySQL's default storage engine ...

Summary of Linux Logical Volume Management (LVM) usage

Managing disk space is an important daily task fo...

A performance bug about MySQL partition tables

Table of contents 2. Stack analysis using pt-pmap...

Talk about implicit conversion in MySQL

In the course of work, you will encounter many ca...

Introduction to Nginx log management

Nginx log description Through access logs, you ca...

Solution to Docker's failure to release ports

Today I encountered a very strange situation. Aft...

AsyncHooks asynchronous life cycle in Node8

Async Hooks is a new feature of Node8. It provide...

Using iframe techniques to obtain visitor QQ implementation ideas and sample code

Today at work, a friend I added temporarily asked ...

Vue close browser logout implementation example

Table of contents 1. beforeunload event 2. Unload...

MySQL installation tutorial under Centos7

MySQL installation tutorial, for your reference, ...

Tutorial on installing mysql5.7.18 on mac os10.12

I searched the entire web and found all kinds of ...