Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images become misaligned compared to div items, despite the same css

Tags:

html

css

I have a div .pets with several images and divs within it. The css for class pet is applied to both img and div but the images become misaligned as in the image: images misalign Here is my html:

<div class="container">
    <div class="pets">
        <img src="/images/animal1.png" class="pet">
        <img src="/images/animal1.png" class="pet">
        <img src="/images/animal1.png" class="pet">
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
    </div>
</div>

And css for pets and pet:

.pets {
    background-color: lightgrey;
    margin-top: 15vh;
    border: 1px solid black;
    text-align: center;
}
.pet {
    background-color: red;
    border-radius: 100%;
    width:20vh;
    height:20vh;
    display: inline-block;
}

How can I make the images align the same as the divs?

like image 406
George Avatar asked Mar 04 '26 07:03

George


1 Answers

Since you have given display: inline-block; add vertical-align too:

.pet {
  background-color: red;
  border-radius: 100%;
  width:20vh;
  height:20vh;
  display: inline-block;
  vertical-align: middle;
}

The default is vertical-align: baseline;. That's the reason.

.pets {
  background-color: lightgrey;
  margin-top: 15vh;
  border: 1px solid black;
  text-align: center;
}

.pet {
  background-color: red;
  border-radius: 100%;
  width: 20vh;
  height: 20vh;
  display: inline-block;
  vertical-align: middle;
}
<div class="container">
  <div class="pets">
    <img src="//placehold.it/100?text=Pet" class="pet">
    <img src="//placehold.it/100?text=Pet" class="pet">
    <img src="//placehold.it/100?text=Pet" class="pet">
    <div class="pet"></div>
    <div class="pet"></div>
    <div class="pet"></div>
    <div class="pet"></div>
    <div class="pet"></div>
    <div class="pet"></div>
    <div class="pet"></div>
  </div>
</div>
like image 89
Praveen Kumar Purushothaman Avatar answered Mar 07 '26 04:03

Praveen Kumar Purushothaman