【问题标题】:How can I align centered inline-block elements correctly?如何正确对齐居中的内联块元素?
【发布时间】:2014-10-20 19:37:38
【问题描述】:

http://jsfiddle.net/ytn6z8oo/

要查看问题是什么,请在 jsfiddle 中调整结果窗口的大小,以便连续三个项目。如您所见,第三项与其他项不一致,显然是因为它的内容较少。我怎样才能解决这个问题?我尝试了 float:left,但它们不再居中,这是必不可少的。

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:kubilayyalcin@yahoo.de" 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;
}

【问题讨论】:

  • 添加一个空的&lt;p&gt; 元素。
  • @nalply 这不是一个真正令人满意的解决方案 :)
  • 这就是我将其作为评论发布的原因:-P
  • 为什么人们不赞成这个?
  • 不是我,你看stackoverflow.com/help/how-to-ask。你的问题只对你有用。这对您来说是一个快速解决方案,对其他人没有帮助。

标签: css html position css-float css-position


【解决方案1】:

您可以使用vertical-align: top; 覆盖默认的vertical-align 值,即baseline

figure.red {
  vertical-align: top;
} 

小提琴:http://jsfiddle.net/ytn6z8oo/1/

【讨论】:

  • 谢谢,很容易我忽略了它:D
【解决方案2】:

删除

display:inline-block;

并添加

float:left;

示例:http://jsfiddle.net/9n8h2wy6/2/

【讨论】:

  • 正如我已经写过的,我不能使用 float:left 因为它需要保持居中。
猜你喜欢
  • 2012-07-20
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多