css - 对齐居中的垂直和水平不规则缩略图

标签 css html

再次问这个问题是因为它被标记为重复(实际上不是),我觉得它被埋没了。在将此标记为另一个线程的另一个副本之前,请先阅读。

我试图将一些不规则形状的缩略图居中对齐到一个网格中,该网格随着浏览器窗口的缩小而分成几列。我真的很熟悉这段代码,但是我需要在每张图片下加上说明文字。我找不到这个问题的答案。我认为 figcaption 会起作用,但它奇怪地对齐了。缩略图的最大尺寸为 200px^2。所有这些缩略图都保持它们的比例。

JS Fiddle Example

//CSS
.centered {
    width: 200px;
    height: 200px;
    padding: 20px;
    border: 1px solid red;  /* for testing only */
    display: flex;
    justify-content: center; /* align horizontal */
    align-items: center; /* align vertical */
    float:left;
}

//HTML
<div class="centered">
    <img src="http://placehold.it/200x150" alt="" />
</div>

<div class="centered">
    <img src="http://placehold.it/150x200" alt="" />
    <figcaption>This text isn't working properly</figcaption>
</div>

<div class="centered">
    <img src="http://placehold.it/200x160" alt="" />
</div>

<div class="centered">
    <img src="http://placehold.it/140x200" alt="" />
</div>

<div class="centered">
    <img src="http://placehold.it/200x150" alt="" />
</div>

最佳答案

基本上您需要更改您的 html。您应该在 figure 标签中使用 imagefigcaption

.centered {
    width: 200px;
    height: 200px;
    padding: 20px;
    border: 1px solid red;  /* for testing only */
    display: flex;
    justify-content: center; /* align horizontal */
    align-items: center; /* align vertical */
    float:left;
}
figure{
	margin:0;
	padding:0;
}
figure img{
	display:block;
	margin: 0 auto;
}
<div class="centered">
    <img src="http://placehold.it/200x150" alt="" />
</div>

<div class="centered">
    <figure>
    	<img src="http://placehold.it/150x200" alt="" />
    	<figcaption>This text isn't working properly</figcaption>    	
    </figure>
</div>

<div class="centered">
    <img src="http://placehold.it/200x160" alt="" />
</div>

<div class="centered">
    <img src="http://placehold.it/140x200" alt="" />
</div>

<div class="centered">
    <img src="http://placehold.it/200x150" alt="" />
</div>

关于css - 对齐居中的垂直和水平不规则缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34409350/

相关文章:

javascript - 我如何居中 Bootstrap 的轮播?不是图片,是轮播本身

html - 移动设备上的滚动问题

HTML Div 对齐问题(没有固定宽度)

javascript - CSS3变换缩放后如何获得正确的偏移量

javascript - 使用 SQL 和 PHP 的动态 CSS

html - 禁用保存按钮时如何更改 Bootstrap 按钮的样式?

javascript - 我究竟做错了什么?我正在尝试使用 jQuery 单击使图像变大或变小

css - 如何在CSS网格中指定 "1fr"的宽度和高度?

javascript - 如何识别js注入(inject)的element.style来源?

html - CSS 导航列表重叠文本