How to dynamically adjust the size of the img tag in react to ensure that the displayed picture does not exceed the specified length and width

problem description

cannot find a method to dynamically obtain the width and height of the original picture

the environmental background of the problems and what methods you have tried

if the original picture is small, img will follow the size of the original picture, and if the picture is too large, the img will be reduced to the specified width and height
it won"t work in the way of ordinary html

.
Sep.09,2021

you can use the object-fit attribute, which should meet your needs

can maintain the original size ratio. Make sure that the size of the replacement content must be larger than the container size, and that at least one of the width and height is the same as the container.

img{
    width:50px;
    height:50px;
    object-fit:cover;
}

clipboard.png


naturalWidth and naturalHeight you can get the original size of the picture element

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3546e-2b7c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3546e-2b7c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?