Search code examples
csshtmlpositioncss-floatcss-position

How can I align centered inline-block elements correctly?


http://jsfiddle.net/ytn6z8oo/

To see what the problem is, please resize the result window in jsfiddle so there are three items in a row. As you can see, the third item doesn't align with the others, apparently because it has less content. How can I fix this? I tried float:left, but then they aren't centered anymore, which is essential.

HTML

<div id="red">

<figure class="red"><img src="images/redaktion_fotos/kubi.jpg" class="profil" /> <figcaption>Kubilay Yalçın</figcaption>
<p><img src="/templates/askanier/images/icons/crown.png" /> Chefredakteur</p>
<p><img src="/templates/askanier/images/icons/askanier.png" /> Allgemein</p><br>
<p><a href="https://www.facebook.com/kubyal" class="button fb" target="_blank">Kontakt</a><a href="mailto:[email protected]" class="button mail">Kontakt</a></p>
</figure>

<figure class="red"><img src="images/redaktion_fotos/simon.jpg" class="profil" /> <figcaption>Simon Mathewson</figcaption>
<p><img src="/templates/askanier/images/icons/cursor.png" /> Website</p>
<p><img src="/templates/askanier/images/icons/askanier.png" /> Allgemein</p>
<br>
<p><a href="https://www.facebook.com/simon.mathewson" class="button fb" target="_blank">Kontakt</a></p>
</figure>

<figure class="red"><img src="images/redaktion_fotos/sophie.jpg" class="profil" /> <figcaption>Sophie Altst&auml;dt</figcaption>
<p><img src="/templates/askanier/images/icons/sport.png" /> Sport und Fitness</p><br>
<p><a href="https://www.facebook.com/sophie.eatme" class="button fb">Kontakt</a></p>
</figure>

<figure class="red"><img src="images/redaktion_fotos/safa.jpg" class="profil" /> <figcaption>Safa Hazem</figcaption>
<p><img src="/templates/askanier/images/icons/karikatur.png" /> Karikaturistin</p><br>
<p><a href="https://www.facebook.com/safa.ha.921" class="button fb" target="_blank">Kontakt</a></p>
</figure>

<figure class="red"><img src="images/redaktion_fotos/vicky.jpg" class="profil" /> <figcaption>Vicky Mielczarek</figcaption>
<p><img src="/templates/askanier/images/icons/fashion.png" /> Mode und Lifestyle</p><br>
<p><a href="https://www.facebook.com/vicky.mk.5" class="button fb target="_blank"">Kontakt</a></p>
</figure>


</div>

CSS

#red {
  padding: 0 0 20px 0;
  text-align: center;
}

#red::after {
  content: "";
  display: block;
  height: 0;
  clear: both;
}

figure.red {
  margin: 30px 15px 0 15px;
  display: inline-block;
  background-color: #ddd;
  border: 1px solid #ccc;
  padding: 10px 10px 0 10px;
  font-family: dejan;
  height: 300px;
} 

figure.red:hover {
  border: 1px solid #bbb;
}

figure.red img.profil {
  width: 150px;
  height: 150px;
  margin: 0 auto;
  display: block;
}

figure.red figcaption {
  color: white;
  background: url(../images/figcaption.png) no-repeat;
  width: 236px;
  height: 44px;
  font-weight: bold;
  text-shadow: -1px -1px 0px rgba(0,0,0,0.5);
  padding-top: 25px;
  margin: -12px auto 0 auto;
  text-align: center;
}

figure.red p {
  margin: 0;
  text-align: center;
}

figure.red .button {
  margin-bottom: -10px;
  padding: 10px 15px 10px 40px;
  background-position: center left 15px;
}

figure.red .fb {
  background-image: url(../images/icons/fb.png);
  background-color: #3b5998;
  background-repeat: no-repeat;
}

figure.red .mail {
  background-image: url(../images/icons/mail.png);
  background-color: #666;
  background-repeat: no-repeat;
}

figure.red figcaption a.button:last-child {
  margin-left: 5px;
}

Solution

  • You could use vertical-align: top; to override default vertical-align value that is baseline.

    figure.red {
      vertical-align: top;
    } 
    

    Fiddle: http://jsfiddle.net/ytn6z8oo/1/