I have this html code:
<p>text <span class="icon svgicon"><img src="img.svg" /></span></p>
<p>text <span class="icon svgicon"><svg>...</svg></span></p>
Lets say the paragraph and the span have the font-size 16px. Is it possible to make the image have the same height ( 16px ) using CSS?
I want to make somehow to image inherit the height from the span's font-size so i wont need to customize it for every place where i put it.
This are supposed to be some svgicons.
Last resort would be to make a JS to parse all icons see the holder font-size and set the height.
Thanks.
Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.
To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.
I believe you accomplish it by setting image height relative to the font height and setting the image width to auto like so:
.svgicon img, .svgicon svg {
height: 1em;
width: auto;
}
p, span {
font-size: 16px;
}
<p>text <span class="icon svgicon"><img class="custom" src="img.jpeg" /></span></p>
<p>text <span class="icon svgicon"><svg>...</svg></span></p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With