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.

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历