Detailed explanation of the solution to image deformation under flex layout

Detailed explanation of the solution to image deformation under flex layout

Flex layout is a commonly used layout method nowadays, but sometimes it can cause some minor problems. When I was using flex layout to make an avatar on the left and text on the right, I found that when the width of the image was fixed, the width of the image still changed.

The picture below shows that the avatar should be round, but it has been squeezed and deformed.

<div class="people">
	<img class="avatar" src="./avatar.jpg" alt="avatar">
	<div class="des">
		<p>Tony</p>
		<p>That’s right, I am your teacher Tony, come and get your hair cut by me! </p>
	</div>
</div>
/* Parent element */
.people {
	display: flex;
}
/* avatar*/
.avatar {
	width: 64px;
	height: 64px;
	border-radius: 32px;
}
/* Character introduction*/
.des {
	margin-left: 24px;
}

A common way to search online is to wrap a div outside the img tag.

<div class="people">
	<div><img class="avatar" src="./avatar.jpg" alt="avatar"></div>
	<div class="des">
		<p>Tony</p>
		<p>That’s right, I am your teacher Tony, come and get your hair cut by me! </p>
	</div>
</div>

Another simpler way is to add flex-shrink: 0 directly to the avatar's css.

/* avatar*/
/* The flex-shrink property defines the shrink ratio of the item. The default value is 1, which means that if there is not enough space, the item will shrink. */
/* If the flex-shrink property of an element is 0 and the other items are 1, the one with a value of 0 will not shrink when there is insufficient space. */
.avatar {
	flex-shrink: 0;
	width: 64px;
	height: 64px;
	border-radius: 32px;
}

This concludes this article on the solution to image deformation under flex layout. For more relevant flex image deformation content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope that everyone will support 123WORDPRESS.COM in the future!

<<:  Absolute path URL and relative path URL in html and subdirectory, parent directory, root directory

>>:  An article to understand what is MySQL Index Pushdown (ICP)

Recommend

Implementation of element shuttle frame performance optimization

Table of contents background Solution New Questio...

CocosCreator implements skill cooling effect

CocosCreator realizes skill CD effect There are s...

How to install and uninstall open-vswitch in Linux

1. Compile and install ovs from source code: Inst...

Four data type judgment methods in JS

Table of contents 1. typeof 2. instanceof 3. Cons...

Solution to the problem that docker nginx cannot be accessed after running

## 1 I'm learning docker deployment recently,...

Linux MySQL root password forgotten solution

When using the MySQL database, if you have not lo...

Detailed explanation of the use cases of Vue listeners

The first one is to use jQuery's ajax to send...

Two ways to understand CSS priority

Method 1: Adding values Let's go to MDN to se...

Native js implementation of magnifying glass component

This article example shares the specific code for...

Comparison of the advantages of vue3 and vue2

Table of contents Advantage 1: Optimization of di...

How to pass W3C validation?

In addition to setting regulations for various ta...

Small problem with the spacing between label and input in Google Browser

Code first, then text Copy code The code is as fol...

Detailed steps to build an NFS file sharing server in Linux

Linux builds NFS server In order to achieve data ...

Linux loading vmlinux debugging

Loading kernel symbols using gdb arm-eabi-gdb out...