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

Thursday, November 17, 2022

[FIXED] Why won't this vertically align? Img to the left of div

 November 17, 2022     css, html, vertical-alignment     No comments   

Issue

I'm still having trouble with vertical-align, although having used it many times successfully. In this case, I have an img on the left and a div on the right, here's my code:

.review {
  font-size: 1em;
}

.avatarImg {
  float: left;
  width: 25%;
}

.reviewBlock {
  width: 75%;
  float: left;
}
<div class="review">
  <img class="avatarImg" src="http://via.placeholder.com/200x200" alt="">
  <div class="reviewBlock"> <!-- misc elements --> </div>
</div>

My img is a few pixels shorter than my reviewBlock, so I would like for it to be shifted down without having to use margin-top. As far as I can see there is a baseline established, see these pictures. enter image description hereenter image description here

I have tried putting display:inline-block on both the img and the div, as well as vertical-align:middle and it has not done anything. Can anyone explain what's going on here? Much thanks.


Solution

Adding display: inline-block; vertical-align: middle; to both elements will align them.

Make sure you also removed float, or else this won't work

As they are inline-block I also removed the white space between the 2 by connecting them with a comment <!-- -->

.review {
  font-size: 1em;
}

.avatarImg {
  display: inline-block;
  vertical-align: middle;
  width: 25%;
}

.reviewBlock {
  display: inline-block;
  vertical-align: middle;
  width: 75%;
}
<div class="review">
  <img class="avatarImg" src="http://placehold.it/100/f00" /><!--
  --><div class="reviewBlock">
    Blaa blaa<br> Blaa blaa<br> Blaa blaa<br> Blaa blaa
  </div>
</div>



Answered By - Asons
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