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

The One-Hand Rule of WEB2.0

<br />My previous article about CSS was not ...

Detailed explanation of viewing and setting file permissions on Mac

Preface To modify file permissions in the termina...

The meaning of the 5 types of spaces in HTML

HTML provides five space entities with different ...

Detailed steps to build an independent mail server on Centos7.9

Table of contents Preface 1. Configure intranet D...

A record of the pitfalls of the WeChat applet component life cycle

The component lifecycle is usually where our busi...

How to deploy nextcloud network disk using docker

NextCloud You can share any files or folders on y...

Use of filter() array filter in JS

Table of contents 1. Introduction 2. Introduction...

Pitfall notes of vuex and pinia in vue3

Table of contents introduce Installation and Usag...

How to solve the problem of margin overlap

1. First, you need to know what will trigger the v...

HTML table markup tutorial (18): table header

<br />The header refers to the first row of ...

How to Fix File System Errors in Linux Using ‘fsck’

Preface The file system is responsible for organi...

MySql 8.0.11 installation and configuration tutorial

Official website address: https://dev.mysql.com/d...

Summary of several MySQL installation methods and configuration issues

1. MySQL rpm package installation # Download the ...