Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image size same as container font-size

Tags:

html

css

svg

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.

like image 959
Emanuel Avatar asked Feb 07 '18 15:02

Emanuel


People also ask

How do I make images the same size in a container?

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.

How do I auto-resize an image to fit a DIV container?

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.


1 Answers

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>
like image 171
Andrew Allison Avatar answered Sep 24 '22 01:09

Andrew Allison