Troubleshooting Problem with Resizing Background Images on Bootstrap 4 Cards

Currently, I am utilizing Bootstrap's grid system along with a simple card element. Within the card, I am dynamically rendering a background image similar to Instagram's grid system. Everything seems to be functioning correctly, except for when the column width drops below a certain point, causing the card to resize to about 10% of its original height and making the image barely visible.

<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: Working fine as expected: https://i.sstatic.net/GoLHJ.png

However, once it goes below col-sm-4 (or md), it starts looking like this:

https://i.sstatic.net/iNfQp.png

When resizing the browser width down, the card height snaps to match the second image shown above.

Is there a way to adjust the sizing of the card in the second picture to maintain full height without affecting the other column widths? Adding "min-height: 200px;" resolves the issue, but distorts the appearance of the other columns by stretching them out.

I apologize if this is a basic question. Although I have come across some resources, I struggle to implement a solution since my expertise lies more in backend development rather than front-end Bootstrap mechanics.

Answer №1

If you use "background-size: contain," your images won't stretch and will be fully visible. However, you may still need to set a min-height. An alternative option is to consider using an img tag instead of divs with backgrounds. This way, the image will size based on the image itself rather than the div.

Best regards, Ethan

Answer №2

After exploring various options, I discovered a solution that worked perfectly for me. I appreciate everyone's input on this matter. In case someone else encounters a similar issue, here is the resolution I found:

Here is the functional solution:

<div class="card h-100">

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

I had attempted this previously, but was unsuccessful in achieving equal card heights and full-width/height images. The trick lies in setting the card to h-100 so that all cards have equal height. Then, stretch the image width and height to 100%, while using img-responsive. This method ensures that the image scales appropriately with varying column widths.

Best regards!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with display of absolute/relative positioned element in IE8

Within a list element (a div) styled with inline-block, there is a ul positioned relatively that is initially hidden. To make the div absolute, I added a class but encountered issues specifically in IE8/9 where clicking on the list does not reveal it as ex ...

Designing a blurred and distinct margin with the hover effect in html and css

Hey there, I've been attempting to replicate the hover effect on the buttons located on the left-hand side of this site , but unfortunately, I haven't had much success. The effect seems to involve a shift in margin and an unevening of margins tha ...

Utilizing HTML and JavaScript to add grayscale effect to images within a table, with the ability to revert to the colored version upon mouseover

Seeking advice on utilizing the mouseover / mouseout event in javascript to implement grayscale on a table. The challenge requires creating a gray image grid (table) using HTML and then incorporating Javascript so that hovering over an image triggers it to ...

Finding the current URL of an iframe and storing it in a JavaScript variable

Hi everyone, I'm new to JavaScript and I have encountered a problem. <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <iframe name=&apos ...

HTML form for sending data to an express route with specific parameters

Looking for some help on routing in express. I currently have a form that collects user input and submits it to the /images route, but I would like it to instead submit to /images/:s1. This way, I can utilize the URL parameters effectively. Any suggestio ...

Tips for altering the design of a registration page

I'm struggling to modify my registration page layout so that the avatar is positioned on the left while the username, password input boxes are on the right. I attempted to use split divs without success and also tried adjusting the positioning of the ...

Utilizing the essential "required" attribute in HTML5 in conjunction with a submit Button

I'm encountering a frustrating issue with a form. I typically use the 'required' attribute in an input box for quick validation, but when I switch to using a button instead, the validation doesn't seem to work. Is it possible that the & ...

Another option for "top-margin" in CSS?

I'm having some trouble fitting navigation tabs onto an <article> element, especially in Firefox and on different screen sizes. Does anyone have a solution that works across all browsers and maintains consistency when the screen is resized? The ...

How to Add Catchy Titles to Your Menu Items on Silverstripe CMS?

Hey there! I'm currently working with Silverstripe CMS using the "Simple" template. I'm interested in finding out how to add subtitles to my menu items. Here's the current structure of my navigation template: <nav class="primary"> &l ...

Blockage preventing text entry in a textarea

To enhance the basic formatting capabilities of a textarea, I implemented a solution where a div overlay with the same monospace font and position is used. This approach allows for displaying the text in the div with different colors and boldness. However ...

Implementing Bootstrap to enhance button functionality

<th><button type="button" id="selectAll" >Select All</button></th> I need assistance in converting the code above to bootstrap syntax for a haml file. Since I am inexperienced with bootstrap and haml, can someone provide guidance o ...

Creating a dynamic progress bar that scrolls for multiple elements

I am currently working on implementing a scrolling progress bar to show users how much of an article within a div they have read. For reference, something similar can be seen on this site. I have created my custom progress bar and coded it on fiddle, whe ...

Having Difficulty Positioning Tooltip Beside Input/Label Field

I have created a tooltip using HTML and CSS that appears when hovering over an image. However, I am encountering alignment issues when placing the image next to a textbox/input as it is not inline with the input box, likely due to some absolute positioning ...

Starting the Android System Magnifier with a Click: A Step-by-Step Guide

Is there a way to incorporate a magnifying glass into an HTML page using jQuery or within an Android app? Ideally, it would be for a single picture. In my application, I am displaying an HTML file from my assets in a webview. The HTML page utilizes jQuery ...

Show the nested div when hovering over the containing div using JavaScript

I have a situation where I have multiple divs within a parent div, all using the same class. Here is an example: <div class="deck-content"> <div class="deck-box">TEST< <div class="deck-hidden">< <span class= ...

Tips for retrieving the value when a button is clicked in AngularJS

Looking for assistance on retrieving the selected value in AngularJS when a button is clicked. Below is the code I am currently using: <div class="form-inline"> <div class="form-group"> <select class="form-control" data-n ...

What is the reason for the unexpected bar appearing between my divs?

I'm stumped as to why this random bar is appearing: If you want to take a look, here's the JSFiddle. I really need to remove that annoying yellowish line. And here is the code snippet for reference: body { padding: 0; margin: 0; backgr ...

Having issues with the Vue.js documentation example not functioning properly in Codepen?

My goal is to replicate a checkbox example from Vue.js's documentation. However, when I import Vue into codepen.io and copy the code for multiple checkboxes into the JS field, then paste the HTML into the designated field, something isn't working ...

Tips for effectively showcasing div elements

https://jsfiddle.net/qz8hL574/1/ for (var key in table) { if (table.hasOwnProperty(key)) { $('<div class="element"></div>').appendTo('#list'); document.getElementsByClassName("element")[key].innerHTML = ...

Having difficulty getting the h1:after element to properly float to the left and center

I am facing an issue with the styling of a :after class that creates a line under a floated h1 element. The h1 element is floated left, and I want to center the :after line underneath it. Despite floating it left and attempting a text-center on the parent ...