Adjusting image size to fit divs depending on orientation

I am currently working on a project where I need images to fill a div while still maintaining their aspect ratio. To achieve this, I am utilizing jQuery to determine if the images are portrait or landscape, and adjusting the width or height accordingly.

However, I am facing an issue where all the images are being assigned the landscape class. I have spent a lot of time trying to figure out why this is happening...

Any help or insight on this matter would be greatly appreciated!

Snippet of HTML:

    <div class="prop">
    <div class="propimage"><img src="{@image}" alt="{@prop_name}"></div>
    <div class="propname">{@prop_name}</div>
    <div class="propdescription">{@description}</div>
    <div class="propprice">{@price}</div>
    </div>

Snippet of CSS:

    .portrait img {
    width: 100%;
    }
   .landscape img {
    height: 100%;
    }
   .propimage img {
        display: block;
    }
    img {
        max-width: none;
    }
    .propimage {
        width: 100%;
        height: 300px;
        overflow: hidden;
        float: left;
    }

Snippet of SCRIPT:

    <script>
    $(".propimage").each(function(){
        // Uncomment the following if you need to make this dynamic
        //var refH = $(this).height();
        //var refW = $(this).width();
        //var refRatio = refW/refH;

        // Hard coded value...
        var refRatio = 240/300;

        var imgH = $(this).children("img").height();
        var imgW = $(this).children("img").width();

        if ((imgW/imgH) < refRatio){ 
            $(".propimage").addClass("portrait");
        } else {
            $(this).addClass("landscape");
        }
    })
    </script>

Answer №1

To showcase your code, kindly upload it on js fiddle or share the link for us to view a working example. In the meantime, you can test out the following code:

<script>
    $(".propimage").each(function(){
        // Uncomment the following if you need to make this dynamic
        //var refH = $(this).height();
        //var refW = $(this).width();
        //var refRatio = refW/refH;

        // Hard coded value...
        var refRatio = 240/300;

        var imgH = $(this).children("img").height();
        var imgW = $(this).children("img").width();

        if (imgW=<200 || imgH<=300)){ 
            $(this).children("img").css("width","100%");
            $(this).css("width","100%");
        } else {
             $(this).children("img").css("height","100%");
             $(this).css("height","100%");
        }
    })
    </script>

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

Managing email delivery and responses within Nextjs server functions using Nodemailer and React Email package

Currently, I'm working on a Next.js project that involves sending emails. The functionality works as expected, but I've encountered an issue when trying to verify if the email was successfully sent or not. Here's my current setup: await tran ...

Is there a way to execute a JavaScript function by clicking on an anchor tag?

Many websites, such as StackOverflow, have attempted to address this particular issue. However, the explanations provided are complex and difficult for me to grasp. I am looking for someone who can help identify my mistakes and explain the solution in simp ...

I am interested in displaying or hiding a table depending on a specific condition in MVC 4

Looking at this MVC 4 Razor code snippet: <h4>You have @Model.Count() items up for sale currently. @Html.ActionLink("Add a new listing by clicking here", "Create")</h4> <br /> <table style="visibility: hidden"> .... I am seeking ...

The HTML retrieved cannot be accessed by external JavaScript code

I've encountered a major issue. I'm using ajax to retrieve content in HTML format, but my main JavaScript file is unable to manipulate any retrieved elements - tags, classes, IDs, you name it. Is this normal? How can I work with the data once it& ...

Creating a Consistent Look for Italic Font Formatting in Tailwind

In an effort to establish consistent typography on a website being developed by my team, we have devised custom utility classes in Tailwind. One of these classes, small-italicized, also requires the text to be italicized. However, it seems that the fontSiz ...

Why isn't the default value appearing on page load in jQuery?

I have a set of radio buttons with five different values. For each value selected, a corresponding span tag is generated with a description associated with that specific radio button option. Here's an example: <table class="jshop"> <tbod ...

What is the best way to display HTML content stored in a String on Xcode for IOS?

Looking for a way to load editable HTML inside my IOS app, I came up with the following approach: To achieve this, I created a NSString with a format that allowed me to generate a String containing the HTML code based on various variables: NSString *goog ...

Saving functions in the localStorage API of HTML5: A step-by-step guide

I have encountered an issue while trying to store an array in local storage using JSON.stringify. The array contains functions (referred to as promises) within an object, but it seems that when I convert the array to a string, the functions are removed. Su ...

Displaying postcode on a category page: A step-by-step guide

I would like to showcase the user input code and present it on the web exactly as entered. <?php #code... ?> Any assistance is greatly appreciated. Please excuse my English. Thank you! ...

Whenever the click event is triggered, Ajax is making numerous duplicate calls

Each time I click to fetch my content using an AJAX call, the calls end up duplicating themselves. I've tried various on-click events I came across on Stackoverflow threads, but unfortunately none of them seem to be solving the issue. $(document).rea ...

Rendering of @font-face on Windows 7 was executed flawlessly

I have been utilizing @font-face to implement custom fonts on a website. The custom font displays correctly in Firefox, IE, Safari, and Chrome on Mac. However, when viewed on Chrome in Windows 7, the text appears green at 10px instead of black or grey. ... ...

Using JavaScript to transfer the data ID from a button to a form

I am working on a project where I have a button that triggers a modal: <button type="button" class="btn btn-primary add-subscription" data-toggle="modal" data-workspace_id="{{ workspace.id }}" data-target="# ...

Tips for extracting the chosen value from a dropdown list within a table cell using JavaScript

Here is an example of an HTML element: <td> <select> <option value="Polygon 47">Polygon 47</option> <option value="Polygon 49">Polygon 49</option> </select> </td> I am looking for a ...

Is there a way to design a right border that is shorter in height than the div itself

I've got a box full of social media content below: 1 | 2 | 3 Is there any way to shorten the vertical pipes "|" so they don't extend beyond the height of the div? ...

Loading screen displayed while transitioning between routes within Angular

I have been struggling to implement a loading spinner in my project. How can I display a loading screen when changing routes in Angular? Here is the HTML code snippet: <div *ngIf="showLoadingIndicator" class="spinner"></div> ...

Are there any other CSS properties that can achieve the same functionality as "contain: content;"?

Searching for an alternative to the contain: content; CSS property that is compatible with older browsers. Is there a solution out there? Perhaps a CSS hack or workaround? ...

Transform HTML into PNG with Canvas2image, followed by the conversion of PNG into BMP

Is there a direct way to convert HTML to BMP? After searching online, it seems that there isn't a straightforward method. So, I decided to convert HTML to PNG using canvas JS and then use PHP to convert the file from PNG to BMP. I have a question abou ...

Clicking the Less/More button using jQuery

In my current setup, I have a code snippet that loads and displays two comments initially. There is also a button labeled More comments which, when clicked, fetches and shows the remaining comments using AJAX. Upon clicking the More comments button, it tr ...

Prevent Xdebug from processing multiple requests

One recurring issue I encounter in my app is calling an API endpoint every 60 seconds. However, when I attempt to debug using Xdebug, stepping through the controller associated with that endpoint often takes longer than a minute. This results in another re ...

Mastering the art of knowing when to implement asynchronous and synchronous programming techniques is essential for

As I explore asynchronous programming in JavaScript, I am faced with the challenge of integrating two email providers: Sendgrid and Mailgun. My goal is to send an email using one provider, and if any errors occur during the process, automatically resend th ...