Set equal height of specific elements using CSS only?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Set equal height of specific elements using CSS only?



I have 2 columns of data, in each column I have a paragraph and an image. I am wondering if there is a CSS-only method of making the paragraphs have the same height so that the images line up. I know I could loop through with JavaScript to find the tallest paragraph and set the height of the other to that, but I'm hoping to find a CSS solution if possible.



Additionally, I cannot modify the DOM structure.




.container
display:flex;

.col
padding:0 15px;


<div class="container">
<div class="col">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fringilla ultrices nulla ac tempus.</p>
<img src="http://placehold.it/250x100" />
</div>
<div class="col">
<p>Nam mauris mi, eleifend et rutrum at, dignissim nec elit. Aliquam lacinia tincidunt leo, et pellentesque nibh ultricies ut. Etiam elit purus, blandit ac consequat a, dictum a dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<img src="http://placehold.it/250x100" />
</div>
</div>




3 Answers
3



I'm not sure Arup's example does what you expect - images still don't align. The answer has been updated.



Here's what should work for you:


.container
display:flex;


.col
padding:0 15px;
display: flex;
flex-direction: column;
justify-content: flex-end;


.col p
flex: 1;



We set the main axis to be our y axis by using flex-direction and we justify the content such that everything in a col div sits at the bottom of this axis. Setting flex: 1; on the paragraph allows the paragraph to grow in order to occupy the free space in the div.


y


flex-direction


col


flex: 1;





I updated it.. I misread it first time.
– Arup Rakshit
Aug 8 at 17:51



You can use flexbox for that, too. See the comments in CSS (borders added just for clarity):




.container
display: flex;

.col
padding:0 15px;
display: flex;
flex-direction: column; /* causes the image to go under the paragraph */
align-items: flex-start; /* images would stretch over the entire .col width without this */
border: 1px solid red;


.col p
flex: 1; /* makes the paragraph grow to the tallest height available */
border: 1px solid blue;


<div class="container">
<div class="col">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fringilla ultrices nulla ac tempus.</p>
<img src="http://placehold.it/250x100" />
</div>
<div class="col">
<p>Nam mauris mi, eleifend et rutrum at, dignissim nec elit. Aliquam lacinia tincidunt leo, et pellentesque nibh ultricies ut. Etiam elit purus, blandit ac consequat a, dictum a dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<img src="http://placehold.it/250x100" />
</div>
</div>



Add below styles.


.container
display:flex;


.col
padding: 5px 15px;
border: 1px solid #999;
display: flex;
flex-direction: column;
justify-content: space-between;



Live Example.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard