简体   繁体   中英

How to get image height and give that height to image div to make it responsive

I want to give image height to image main div to make it fit inside div like below is image which height is 186px so i want to give that image height to main div to make them fit. Is there any want to get image height which i mark in below image.

在此处输入图片说明

As there is a solution but that is working fine as it giving another height.

 var img = new Image();
 img.src = this.props.image;
 console.log(img.height) // it give me 406 not 186

This is what is my problem and what i want. 在此处输入图片说明

Yeah,you can get image height by accessing img.naturalHeight ,same goes for width as well but u have to wait for browser to download image (use onload event and access once image loaded) .u can find aspect ratio and make your div responsive or apply it's original width and height. Happy coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM