Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image max size according to parent's line-height

Tags:

html

css

Is it possible with pure CSS to make an inline image size (scale) according to the line-height of its parent?

Consider this set-up:

HTML:

<p>
  <img src="image.png"/> text text text
</p>

CSS:

p {
  line-height: 16px;
}

img {
  display: inline;
  vertical-align: middle;
}

What I would like is that the image gets scaled to a height of 16px, as is the line-height of the paragraph. Is this possible?

please note: I'm open to changes in the HTML and CSS, but I'm not after a JS solution.

like image 430
Bazzz Avatar asked Apr 29 '11 14:04

Bazzz


People also ask

How do I make an image fit in a parent div?

We have to give image tag width and height as 100%. And add the object-fit property value as contain. But the difference is if parent div size is more than image size then image will be automatically resized to parent div element size.

How is line height calculated?

It takes the font-size value and multiplies it by 1.2 . Let's calculate the height of one line with the following example. We just have to do the following calculation: 16 * 1.5 = 24px. So we now know that our text will have a minimum height of 24px.

How can I set an image to 100 width?

height is set on the element in HTML. width is set on the element in HTML. height (or width ) is set in the CSS — including using percentage values like max-width: 100%;

What is normal line height?

The default line-height is about 110% to 120% for the majority of the browsers. The line-height property sets the leading of lines of a text. If the line-height value is greater than the font-size value of an element, the difference will be the leading of text.


2 Answers

this works pretty well:

img {height: 1em;}
like image 133
ErichBSchulz Avatar answered Sep 28 '22 09:09

ErichBSchulz


There's no pure HTML/CSS way to do this, sadly.

like image 24
Boris Zbarsky Avatar answered Sep 28 '22 09:09

Boris Zbarsky