Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the height of an image to match the height of the text next to it

I am attempting to set the height of an image to the height of the text which is adjacent.

I've tried using the jQuery code which I found in an answer to the question How to set height of element to match height of another element?, however it makes the image too large (it's height is higher than the text).

$("#img").css("height", $(".text").height());
img {
  float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="here">
<img src="https://images-na.ssl-images-amazon.com/images/M/MV5BMzg2Mjg1OTk0NF5BMl5BanBnXkFtZTcwMjQ4MTA3Mw@@._V1_SX300.jpg" id="img">
<div class="text">
  <h2>Example</h2>
  <p>Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example</p>
  <p>Example Example</p>
  <p>Length: 111 min</p>
  <p>Example Example Example</p>
  <p>Example Example Example Example Example Example Example Example Example Example Example</p>
</div>
</div>

It would not work to manually set the height, as the image and text are dynamic, meaning they can change.

JsFiddle: https://jsfiddle.net/y9f0xhm0/

like image 304
The Codesee Avatar asked Jan 29 '17 12:01

The Codesee


People also ask

How do you set the height of a picture?

The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.

What specifies the height of an image?

The height attribute specifies the height of an image, in pixels.

How can I set the height and width of an image?

Example. The width attribute specifies the width of an image, in pixels. Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.


1 Answers

The answers I had received all had flaws in them which I discovered (such as if the page width was decreased, the image would be messed up) and overall, they were over-complicated.

After some research, a help from a friend who I 'collaborated' with over JsFiddle and a subsequent question which I posted, I have this code which solves my question of how to set the images's height the same as the adjacent text to it:

$("#here").find('img').css("height", $(".text").outerHeight());

var $img = $("#here").find('img');
$img.on('load', function() {
  $("#here").find('img').css("height", $(".text").outerHeight());
});

window.onresize = function() {
  $("#here").find('img').css("height", $(".text").outerHeight());
}
img {
  float: left;
}
p:last-of-type {
  margin-bottom: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="here">
  <img src="https://images-na.ssl-images-amazon.com/images/M/MV5BMzg2Mjg1OTk0NF5BMl5BanBnXkFtZTcwMjQ4MTA3Mw@@._V1_SX300.jpg" id="img">
  <div class="text">
    <h2>Example</h2>
    <p>Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example</p>
    <p>Example Example</p>
    <p>Length: 111 min</p>
    <p>Example Example Example</p>
    <p>Example Example Example Example Example Example Example Example Example Example Example</p>
  </div>
</div>

JsFiddle: https://jsfiddle.net/f512ejag/2/

like image 147
The Codesee Avatar answered Oct 25 '22 07:10

The Codesee