简体   繁体   English

Bootstrap 4-卡背景图像调整大小问题

[英]Bootstrap 4 - Card Background-image resize issue

So currently I'm using Bootstrap's grid system and a simple card. 所以目前我正在使用Bootstrap的网格系统和一张简单的卡。 In the card, I render dynamically the background image. 在卡片中,我动态渲染背景图像。 Think of the Instagram grid system. 想想Instagram的网格系统。 Everything works fine, except that once I go below a defined column width, the card resizes it to maybe 10% of its original height and the image is barely visible. 一切工作正常,除了一旦我低于定义的列宽,卡片将其尺寸调整为原始高度的10%,并且图像几乎不可见。

<div class="row">
    <div class="col-md-4 col-sm-4">
       <div class="card" style=" min-height: 100%; min-width: 
       100%;background-image: url('<%= rails_blob_path(post.images.first) 
       %>'); background-position: center; background-size: cover;">

       </div>
     </div>
   </div>

col-md-4 : Everything nice as excepted: col-md-4 :一切都很好,但例外: col-md-4示例

Below col-sm-4 (or md, doesn't really matter, once it drops below the last defined col class it starts to look like this): col-sm-4 (或md并不重要,一旦它降到最后定义的col类以下,它就会开始看起来像这样):

在col-md-4下面)

As you can see, once I scale the width of the browser down, it just snaps the height of the card to the second image. 如您所见,一旦我缩小了浏览器的宽度,它就将卡的高度对齐到第二张图像。

Is it possible to resize the card in the second picture to fill the height, without messing around with the heights of the other cols? 是否可以在第二张照片中调整卡的大小以填充高度,而不会弄乱其他列的高度? Adding "min-height: 200px;" 添加"min-height: 200px;" will fix this issue, but it will also mess with the other col widths and make them appear stretchy. 将解决此问题,但同时也会使其他col宽度变乱,并使它们显得有弹性。

Sorry if that's a basic question. 抱歉,这是一个基本问题。 I found a few posts but I couldn't fix it at all and normally backend, just trying to understand Bootstrap better. 我发现了一些帖子,但是我根本无法修复它,而且通常在后端也无法解决,只是试图更好地理解Bootstrap。

Using "background-size: contain" will make it so that your images don't stretch and are fully visible, but you are still stuck with setting a min-height, which won't mess with your col widths so you don't have to worry about that. 使用“ background-size:contain”将使图像不会拉伸并且完全可见,但是您仍然会设置最小高度,这不会干扰您的col宽度,因此您不会不必担心。 Consider using an img tag instead of divs with backgrounds though as they will size based on the image, whereas the image sizes itself with the div when you use divs w/ backgrounds 考虑使用img标签而不是具有背景的div,因为它们会根据图像进行尺寸调整,而当您使用带有背景的div时,图像本身会通过div进行尺寸调整

Cheers, Ethan 干杯,伊桑

So i came up with a solution, thanks everyone for your input. 所以我想出了一个解决方案,谢谢大家的投入。 Just in case someone else runs into this problem: 以防万一有人遇到这个问题:

Heres the working solution: 这是工作解决方案:

<div class="card h-100">

  <%= image_tag(post.images.first, class: 
 'img-responsive', style: "width: 100%; height: 
  100%;")%>
</div>

I tried this before, but the cards were unequally in the height and the images were not really full width/height. 我以前尝试过这种方法,但是卡的高度不均,图像的宽度/高度并不是真的。 The key is here to set the card to h-100 so that the card height is equally. 此处的关键是将卡设置为h-100 ,以使卡高度相等。 Then, stretch the image in width&height too 100% and use img-responsive . 然后,将图像的宽度和高度也拉伸100%,并使用img-responsive This way, the image scales nicely together with all col widths. 这样,图像可以与所有col宽度一起很好地缩放。

Greetings! 问候!

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

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