PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 9, 2022

[FIXED] How can I set image height to be equal to text height?

 November 09, 2022     css, html, image     No comments   

Issue

I'm trying to make the image on the left have the same height as the text on the right.

At the moment, the image scales correctly as long as the window width is reduced, such that the image grows alongside the text. But if there aren't enough lines in the text, the image height remains at 150px, and I wish for it to always be the same height as the text.

Is it possible to do this with no JS?

.container {
  display: flex;
  width: 100%;
}
.col {
  flex: 1;
}
img {
  height: 100%;
}
<div class="container">
  <div class="col">
    <img src="https://via.placeholder.com/150" alt="" />
  </div>
  <div class="col">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</div>


Solution

Well you basically want it to behave like a background image and thats what you should do but if you cant for some reason, give the image an absolute position, and give the container a relative position and it should solve your problem, you could also change the image's behaviour with object-fit etc

.container {
  display: flex;
  width: 100%;
}
.col {
  flex: 1;
  position:relative;
}
img {
  height: 100%;
  position:absolute;
}
<div class="container">
  <div class="col">
    <img src="https://via.placeholder.com/150" alt="" />
  </div>
  <div class="col">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </div>
</div>



Answered By - Breezer
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing