Add original width / height in image tags [closed]

1

Is there any way to pass CSS code to HTML tag? Example, I have the tag below:

<img src="../img/cS-1.jpg" alt="Smiley face">

All css code that I put for that image aparacer inside the img tag when I go in the browser and inspect element? Example to stay this way

I really need only the original size of the photo inside the tag

    
asked by anonymous 27.11.2017 / 19:20

2 answers

1

You can use naturalWidth and naturalHeight that returns the original dimensions of the image and insert into the style element. But, to make it easier to select, add a class (or id ) to the element:

var img = document.querySelector('.imagem');

img.style.width = img.naturalWidth+'px';
img.style.height = img.naturalHeight+'px';
<img class="imagem" src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/48/Basketball.jpeg/220px-Basketball.jpeg"alt="Smiley face" />

    
27.11.2017 / 21:36
0
var tmpImg = new Image();
tmpImg.src="https://assets.servedby-buysellads.com/p/manage/asset/id/32054";//ordocument.images[i].src;$(tmpImg).on('load',function(){varorgWidth=tmpImg.width;varorgHeight=tmpImg.height;alert(orgWidth+"x"+orgHeight);
});

Use this script you can see it at working here

    
27.11.2017 / 20:45