Setting the height of images using CSS is ineffective

I'm currently in the process of building a Django blog, but I've run into some issues regarding the size of the post thumbnails. Take a look at the image below:

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

The yellow markup indicates how the image should ideally fill the space. While the width seems fine, it's clear that the height isn't quite right as shown.

Below is the code snippet I'm working with:

{% extends 'base.html' %}
{% load static %}

{% block content %}
<style>
  img {
    height: 100%;
    width: 100%;
    
  }
</style>

        <!-- Post-->
        {% for obj in object_list %}
        <div class="row d-flex align-items-stretch">
          {% if not forloop.first and not forloop.last %}
          <div class="image col-lg-5"><img src="{{ obj.thumbnail.url }}" alt="..."></div> #This is where the image goes
          {% endif %}
          <div class="text col-lg-7">
            <div class="text-inner d-flex align-items-center">
              <div class="content">
                <header class="post-header">
                  <div class="category">
                    {% for cat in obj.categories.all %}
                    <a href="#">{{ cat }}</a>
                    {% endfor %}
                  </div>
                  <a href="{{ obj.get_absolute_url }}">
                    <h2 class="h4">{{ obj.title }}</h2>
                    </a>
                </header>
                <p>{{ obj.overview|linebreaks|truncatechars:200 }}</p>
                <footer class="post-footer d-flex align-items-center"><a href="#" class="author d-flex align-items-center flex-wrap">
                    <div class="avatar"><img src="{{ obj.author.profile_picture.url }}" alt="..." class="img-fluid"></div>
                    <div class="title"><span>{{ obj.author }}</span></div></a>
                  <div class="date"><i class="icon-clock"></i> {{ obj.timestamp|timesince }} ago</div>
                  <div class="comments"><i class="icon-comment"></i>{{ obj.comment_count }}</div>
                </footer>
              </div>
            </div>
          </div>
          {% if forloop.first or forloop.last %}
          <div class="image col-lg-5"><img src="{{ obj.thumbnail.url }}" alt="..."></div> #This is where the image goes
          {% endif %}
        </div>
        {% endfor %}
      </div>
    </section>

Answer №1

By default, the <img> element will automatically resize to fill the entire width and height, often sacrificing its original aspect ratio.

If this behavior did not occur in your particular case, it is likely because you have specified object-fit: contain in your CSS.

To return to the default behavior or apply a different solution, you can either remove the object-fit: contain property or use:

  img {
    height: 100%;
    width: 100%;
    object-fit: fill !important; # fill is the default value.
  }

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

Struggling to implement CSS variables across different browsers

I'm struggling to make CSS variables function properly, here is what I have so far: :root { --primary-color: #ffcd600; --spacing: 10px; --blur: 10px; } img { padding: var(--spacing); background: var(--primary-color); } When I check the el ...

The JavaScript Top Slide Down effect is malfunctioning

Every time I press the "More" button, I expect some forms to pop out but nothing happens. Can someone please assist me with this issue? <body> <!--Hero Image--> <div class="hero-image"> <div class="hero ...

How do I stop the Navbar style from resetting every time I switch between pages on an ASP.Net Core Web App?

I'm experiencing an issue with the styling refreshing on my navbar when I click a link to navigate to a new page. As a beginner in using ASP.NET Core webapp, I'm looking for a solution to fix this. Here is a video demonstrating the problem: Video ...

Upon redirection, my JavaScript codes cease to function properly and require a page refresh to resolve

Upon redirecting to another page with included JS codes, my main.html does not function properly until I refresh the page. What steps can be taken to resolve this issue? 0.html <body data-theme="b"> <div class="p2" data-role="page" id="p2"> ...

Display HTML Tables, along with the <tr>, <td>, and <a> tags in a Textview on an Android device

I am currently working on a chat app that retrieves HTML content as JSON via an API. One issue I am facing is how to properly display Table and tags within each chat message, as the current setup only allows for basic HTML elements to be displayed in a Tex ...

Utilizing jQuery Methods within Node.js

Recently delved into the world of node.js and created multiple pages (such as login, signup, blog, etc). If I desire an effect in which: 1. Hovering over the "News" button triggers a slider to appear downwards, To make adjustments to an image within ...

Exploring anchor elements within a div using Selenium in Python to extract URLs

Hey there, I'm currently attempting to iterate through anchor elements within a div and retrieve the links of these elements. <div class="List"> <a class="selected" href="link">text 1</a> <a ...

Center Vertically Using CSS Flexbox

Hey there, I'm currently facing an issue with aligning my website's logo vertically in the middle with my navigation link list. I have attempted to use "vertical-align: middle" on my div columns but it doesn't seem to be working. Right now, ...

Issue with long text in a resizable table using jQuery tablesorter 2.31.1

My issue is that when I try to resize the columns in tablesorter, they snap back into place. The column width set with resizable_widths does not apply correctly until a column is manually resized. Despite setting the width of all cells to 120px, any cells ...

Automatically sorting data in JavaScript from PHP-generated JSON

When I receive JSON data from PHP and use it in JavaScript to populate a select box, the order is automatically sorted. This behavior should not occur. I am utilizing this JSON as the value for HTML select box options and I require the data to be in its o ...

Selenium WebDriver Error: Element not found - Element could not be located

Having an issue with clicking on a button that has CSS styling. The code for the button is as follows: <div id="rightBtn"> <input type="submit" class="mainButton" id="dodajTrenera" value="Dodaj" name="dodaj_trenera"> </div> To perform t ...

Static files are not being served by Django Heroku when Debug is set to False

I am currently hosting my Django application on Heroku and have implemented whitenoise to manage the serving of static files. Below is a snippet from my settings.py: DEBUG = False ALLOWED_HOSTS += [ 'example.com', ] MIDDLEWARE = [ &ap ...

Using *ngIf together with fxFlex.lg and fxFlex.xl is causing compatibility issues

One of the challenges I faced involved a div element with a conditional *ngIf statement and responsive layout values set using fxFlex.lg="30" fxFlex.xl="50": <div class="project-test__element" *ngIf="myCondition()" ...

Improprove Google PageSpeed score - must resolve

My mobile website is loading incredibly slow, despite my attempts to improve the speed using Google PageSpeed insights. Unfortunately, I'm having trouble interpreting the screenshot and identifying what changes need to be made. Is there anyone who c ...

Issue with positioning ControlNav and thumbnails in Nivo Slider

This is my first attempt at using Nivo Slider, so please be patient with me. I am attempting to place my controlNav thumbnails INSIDE the slider (specifically in the center, 15px from the bottom of the slider), however, when I use position:absolute along ...

Tips for sending data through AJAX before the browser is about to close

My issue is with a javascript function that I've called on the html unload event. It seems to be working fine in Google Chrome, but for some reason it's not functioning properly in Firefox and IE. <script> function fun() { ...

What is the best way to align all of my content to the center in HTML?

In my web design class, I encountered an issue while creating a page. Currently, my page consists of a navigation bar and an image. I wanted to center both elements using CSS. display: block; margin-left: auto; margin-right: auto I applied this CSS to th ...

Utilizing a JavaScript function to toggle the Bootstrap dropdown without the need for manual clicking

I've configured a Bootstrap dropdown in my site's mini cart that includes a lightbox effect to grey out the background content when the dropdown is activated. Here's the code snippet: $(".dropdown").on('show.bs.dropdown hide.bs.dropdow ...

Is it Possible to Load Images Without Using Javascript?

When I hover over certain links on my HTML pages, large images are displayed but take a while to load. I prefer not to use JavaScript to preload the images. Are there any alternative solutions available? ...

Attempting to reach <p> from numerous web pages

Currently, I am working on a project where I need to extract text data from various websites. I have successfully managed to retrieve the text data using <p> tags. I would really appreciate any assistance with this task. Thank you! ...