简体   繁体   English

如何针对移动设备快速响应地垂直对齐一列图像

[英]How do I center a column of images vertically, responsively for mobile devices

I'm coding up an app that displays 9 newest photos having certain tag. 我正在编写一个显示9张带有特定标签的最新照片的应用程序。 I solved the problem of different image sizes by setting every picture's size to 240px wide and 200px tall. 我通过将每张图片的尺寸设置为240px宽和200px高来解决了不同图像尺寸的问题。

My question is how do I center them vertically so they are in the middle of screens width? 我的问题是如何将它们垂直居中以使其位于屏幕宽度的中间? I want them to be responsive, so they are always in the middle, no matter the width of the device displaying them. 我希望它们能够响应,因此无论显示设备的宽度如何,它们始终位于中间。

HTML:- HTML:-

<div id="results" class="row"></div> 

So far I've tried pretty much everything I could find on the internet. 到目前为止,我已经尝试了几乎可以在互联网上找到的所有内容。 I should mark, that there are 9 photos being displayed, so user will have to scroll. 我应该标记为显示9张照片,因此用户必须滚动。 That means that every solution containing position: absolute; 这意味着每个解决方案都包含以下位置:绝对; won't do it. 不会的

Photos are being picked through Flickr API. 通过Flickr API拾取照片。 After that, I .append() them to div#results by doing:- 之后,我通过执行以下操作将它们附加到div#results:

$("#results").append('<div class="miniaturka col-xs-12 col-sm-4 col-md-4 col-lg-4"><a href="'+myresult_size.url+'" target="_blank"><img src="'+myresult_size.source+'" ></a></div>');

Bootstraps grid fails to center them. Bootstraps网格无法将它们居中。

try with this 试试这个

 <div id="results" class="row text-center"></div> 

or use 或使用

 #results{ margin: 0 auto; } 

For cell of 4-wide you need to use offset class col-sm-offset-4 . 对于4宽单元格,您需要使用offset类col-sm-offset-4 When you define col-sm-4 , all classes for wider viewports will inherit this definition, I mean col-sm-4 is equal to col-sm-4 col-md-4 col-lg-4 . 当您定义col-sm-4 ,所有用于更宽视口的类都将继承此定义,我的意思是col-sm-4等于col-sm-4 col-md-4 col-lg-4 Also you need to apply class text-center for cell that is wider than its child image. 同样,您需要为比其子图像更宽的单元格应用类text-center

So result is: 所以结果是:

$("#results").append('<div class="miniaturka col-xs-12 col-sm-4  col-sm-offset-4 text-center">
    <a href="'+myresult_size.url+'" target="_blank">
       <img src="'+myresult_size.source+'" >
     </a>
    </div>');

Here is an example snippet: 这是一个示例片段:

 img { display: block; margin: 0 auto; max-width: 240px; max-height: 200px; height: auto; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <div class="miniaturka col-xs-12 col-sm-4 col-sm-offset-4 text-center"> <a href="#" target="_blank"> <img src="http://placekitten.com/g/200/300"> </a> <a href="#" target="_blank"> <img src="http://placekitten.com/g/300/300"> </a> <a href="#" target="_blank"> <img src="http://placekitten.com/g/400/300"> </a> <a href="#" target="_blank"> <img src="http://placekitten.com/g/200/300"> </a> <a href="#" target="_blank"> <img src="http://placekitten.com/g/200/300"> </a> <a href="#" target="_blank"> <img src="http://placekitten.com/g/200/300"> </a> </div> </div> 

Text-align center on parent centers all child elements which are inline. 父级上的文本对齐中心居中对齐所有子级元素。

 .miniaturka { width: 100vw; text-align: center; } 
 <div id="results" class="row"> <div class="miniaturka col-xs-12 col-sm-4 col-md-4 col-lg-4"> <a href="#" target="_blank"> <img src="http://placehold.it/350x150"> </a> </div> <div class="miniaturka col-xs-12 col-sm-4 col-md-4 col-lg-4"> <a href="#" target="_blank"> <img src="http://placehold.it/350x150"> </a> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM