Adjust the parent div size in HTML and Django to maintain consistent dimensions of the image

Within my application, users have the capability to upload images that are then displayed in a grid system. Due to their varying aspect ratios, it is not possible for them all to be the same size. Is there a way to keep the parent div the same size for all projects, despite the differing image sizes? Thank you.

Here is an example of my template:

 <div class="container">
        <div class="row">
            {% for project in projects %}
              <div class="col-lg-3 col-md-4 col-sm-6">
                <div class="border border-dark">
                    <img class="card-img-top" src="{{project.featured_images.url}}" alt="Card image cap">
                    <h5 class="text-center">{{project.title}}</h5>
                  </a>
                    <ul class="list-group list-group-horizontal">
                      <li class="list-group-item flex-fill">{{project.price}} €</li>
                      <li class="list-group-item flex-fill">{{project.location}}</li>
                    </ul>
                  </div>
                </div>
                
            {% endfor %}
        </div>
      </div>

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

Answer №1

To adjust the image display using CSS, simply specify the height of the .card-img-top element and include the property object-fit: contain in your styles:

.card-img-top {
  height: 300px;
  object-fit: contain;
}

If you want the image to always align from the top rather than the center, add object-position: top as well:

.card-img-top {
  height: 300px;
  object-fit: contain;
  object-position: top;
}

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

Remove a div element with Javascript when a button is clicked

I am working on a project where I need to dynamically add and remove divs from a webpage. These divs contain inner divs, and while the functionality to add new divs is working fine for me, I'm facing some issues with removing them. The code snippet b ...

Tips for aligning text alongside ons-select elements

I am working with the following HTML snippet: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css"> <link rel="stylesheet" href="https://u ...

Aligning items in the center using the flex property is limited to just one element

#draggable-container { margin: 0 auto; display: flex; flex-wrap: nowrap; align-items: center; } .draggable-game-content { width: 100%; height: 40rem; } .draggable-game { position: relative; border: 1px solid black; bor ...

Click to solve the equation

I want to build a basic HTML page with three input variables (A,B,C). After the user inputs these values, they will click "calculate", and JavaScript will execute the following equations: ((A + B)/2) * 3 = X and (C * 2) = Y The results will be displaye ...

Maximizing performance with prefetch_related and reducing database queries

I currently have two models in my database structure: class Folder(AbstractName): parent = models.ForeignKey('self', blank=True, null=True) owner = models.ForeignKey(User) typeof = models.ForeignKey(TypeParent, blank=True, null=True) ...

Trigger the Onchange event following the completion of the AutoComplete process

I am attempting to implement Autocomplete and onchange event simultaneously. Specifically, I am looking to trigger the onchange event in an autocomplete textbox. When I type something in the textbox using the keyboard and then click outside of the textbox ...

The Carousel feature in Bootstrap 4 is malfunctioning on the website

I've inserted a carousel into my webpage but it's not functioning as intended. I'm attempting to use this carousel code from "codepen.io/decibeldesign/details/bMrQKN" and the full webpage code can be found at "paste.ofcode.org/saJPkuhPPixFmX ...

Differences Between Bootstrap Source Code and Bootstrap CDN Link

I am in the process of updating the user interface for my website, utilizing Bootstrap 5.3. Initially, I utilized the CDN link provided in the official documentation. Recently, I acquired Bootstrap Studio as a tool to streamline the UI development process. ...

Enable the ability for anchor links to be clickable when the CSS media print format is used to generate

To illustrate: <a href="https://www.google.com">Google anchor link</a> on the website, it displays as: Google anchor link When printed, it shows as: Google anchor link(https://www.google.com) To address this issue, I included in the CSS: ...

Struggling with a stuck Bootstrap Navbar during responsive mode?

I recently designed a website using a bootstrap theme and incorporated a navbar into it. However, I noticed that the navbar collapses in responsive mode, but stays fixed to the right side of the page. This is causing me to horizontally scroll in order to ...

What causes the off-canvas menu to displace my fixed div when it is opened?

Using the Pushy off-canvas menu from GitHub for my website has been great, but it's causing some trouble with my fixed header. When I scroll down the page, the header sticks perfectly to the top, but once I open the off-canvas menu, the header disappe ...

Can the first row of a flex-box begin in the second column and end in the third column?

Can a layout be created with two rows and three columns without adding any extra elements using the flex property or any other method? .parent{ display:flex; flex-direction:column; } .child1{ background:lightblue; } .child2{ display:flex; border:1px ...

Switch up the component's style based on the route being accessed

Is there a way to dynamically load CSS styles in a component based on URL parameters? The idea is that the user will access the page using a URL structure like SOME_URL/{screenSize}/{type}. While the component remains the same, the CSS styling should chang ...

Exploring the Interaction Between Django 1.5 and User Model Connections

Exploring the capabilities of Django 1.5+, one can enhance a User model by adding custom fields. However, navigating best practices in this scenario can be challenging. Consider this dilemma: If a project exclusively utilizes a single type of user (e.g. a ...

How to delete URL parameters in JavaScript and HTML5

I have a URL that looks like this: /users/?i=0&p=90 How can I remove the part from ? to 90 Can anyone provide me with some JavaScript code to achieve this? EDIT I mean doing this with window.location.href directly in the browser URL bar. I trie ...

Tips for replacing an element in DOM with an updated HTML string

My current task involves updating a stringify HTML element using an element from the DOM. This stringify HTML element is stored in an array within the localStorage. First, I convert it into manipulatable HTML like this: let toBeUpdated = document.createEl ...

Utilizing Keywords Field in Django Models

As a newcomer to Django, I have a Field called keywords in my model where I plan to save keywords separated by commas. Users should be able to enter compound keywords like "learn Django" or "Django model-Fields." Additionally, I would like to set a maximu ...

Text that is inside a grid item will not be cut off using an ellipsis

For some reason, the ellipsis isn't showing up as expected. Instead of truncating with an ellipsis, the text just goes to the next line. text { display: inline; overflow: hidden; text-align: center; text-overflow: ellipsis; } grid { ...

The exterior DIV does not conform to shrinking dimensions

I've set the display property to inline-block, but the htmlDiv div in this code snippet is not behaving as expected. It's acting as though the inner divs are next to each other even when they're not. How can I fix this? If it's unclear ...

Python Django: Implementing Proper Spacing in HTML Email Titles

We are currently working on integrating the email feature using Python Django, but we are encountering an issue with extra spaces appearing in the header of the emails as shown in these screenshots: https://i.sstatic.net/EpCca.png https://i.sstatic.net/q6 ...