doutang1873 2019-06-26 09:48
浏览 61
已采纳

如何制作像Facebook这样的照片库?

I am working on a 4x3 image gallery with pictures from a database. All pictures have different sizes. Using Bootstrap and CodeIgniter I make rows and columns for every image. Now I want wide pictures to fit the height of the row, center horizontally and hide the horizontal overflow. I also want that tall pictures fit the width of the column, center vertically and hide the vertical overflow. Just like Facebook.

margin-left: auto; margin-right: auto; don't center the image.

My index page:

<h1><?=$title?></h1>

<?php $i = 1; foreach ($photos as $photo) {
    if ( $i % 3 == 1 ) { ?>
        <div class="row gallery-row">
    <?php } ?>
    <div class="col-md-4">
        <img class="gallery-image" src="<?php echo site_url(); ?>assets/images/<?php echo $photo['photo_name'];?>">
    </div>
    <?php if ( $i % 3 == 0 ) { ?>
        </div>
    <?php }
    $i++;
} ?>

My css:

.row.gallery-row{
    height: 25%;
}

.gallery-image{
    height : 100%;
    overflow:hidden;
    display: table-cell;
    text-align: center;
}

The closest I get is with the CSS above: The images fit the height of the row and overflow is hidden. I still want the images to center and if the images are taller then wide they should prioritize fitting the width. Here is what the page looks like image

The bottom left picture has a woman on it but it doesn't show because it isn't centered.

Solution: I changed my css to :

.gallery-image{
    height : 100%;
    width : 100%;
    object-fit: cover;
}

My page now looks like this: image Adding some padding in between the rows will make it look just like facebook.

  • 写回答

1条回答 默认 最新

  • dongming5444 2019-06-26 09:58
    关注

    Maybe adding object-fit in your .gallery-image css is what you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败