javascript 自动缩放图像以匹配文本高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12285608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:49:11  来源:igfitidea点击:

Automatically scale an image to match text height

javascripthtmlcssimage

提问by rlbond

I've got an image which I want to use inline with some text, but I need it to match the text height. I know it's possible to something like <img src="img.jpg" height=16>or even <img src="img.jpg" height="100%">, but the former does not scale with text size and the latter seems to make it the size of the div, not the text height. Can anyone help?

我有一个图像,我想将其与一些文本内联使用,但我需要它来匹配文本高度。我知道有可能像<img src="img.jpg" height=16>甚至<img src="img.jpg" height="100%">,但前者不会随着文本大小而缩放,而后者似乎使它成为 div 的大小,而不是文本高度。任何人都可以帮忙吗?

Example HTML:

示例 HTML:

<body>
This is a test <img src="img.jpg">
</body>

回答by Praveen Kumar Purushothaman

Did you try this giving the image height as 1em?

您是否尝试过将图像高度设为 1em?

img {height: 1em;}

Check out the fiddle here: http://jsfiddle.net/yAr7z/

在这里查看小提琴:http: //jsfiddle.net/yAr7z/