dsstjqsr631426 2016-03-29 00:12
浏览 53
已采纳

尽管使用Bootstrap上传尺寸,但强制Wordpress图像具有相同的高度

I have some code to output images in Wordpress like this...

<div class="col-md-2">

<?php $image = get_field('artist_photo');
       if( !empty($image) ): 
        // thumbnail
        $size = 'medium';
        $thumb = $image['sizes'][ $size ];
       ?>
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" class="img-responsive" />

</div>

<?php endif; ?>

The problem is, all of the images have different heights (based on their proportions at original upload}.

Is it possible to make them have a uniform height whilst remaining responsive?

  • 写回答

1条回答 默认 最新

  • duanniying2342 2016-03-29 00:45
    关注

    Here are two ways to constrain the height of an image in CSS.

    First, you can set a maximum height. So something like:

    .col-md-2 img {
      display: block;
      max-height: 300px;
      width: auto;
      height: auto;
    }
    

    Second, you can display the image as a background image and then use the background-size property to constrain it. In your markup display the image using:

    <div class="image" style="background: url(<?php echo $thumb; ?>) no-repeat center;" />
    

    And style it like so:

    .col-md-2 {
      height: 300px;
    }
    
    .col-md-2 div.image {
      height: 100%;
      width: 25%; // for example, assuming you want 4 images per row
      background-size: constrain;
    }
    

    I'm not sure how this will work in the context of bootstrap, but option 2 might be your only option if you can't specify an explicit height for the image or its parent.

    There are more devils in the details and the specifics will depend on your situation, but hopefully this is helpful.

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?