Expand image to maximum width and height regardless of container size while preserving its aspect ratio

I need help with an <img> element that is absolutely positioned inside a <div> element. The <div> has dimensions of 100px by 100px. When the <img> is clicked, it should expand to a maximum width or height of 500px by 700px. If the original size of the <img> is smaller, for example 300px by 250px, then it should not exceed that size. Importantly, the aspect ratio must be maintained.

Simply setting the height and width to 100% results in the <img> matching the size of its containing <div>.

I am seeking a solution using either CSS or vanilla JavaScript. (I do not need code for a click animation, just a static final result so I can understand the concept)

Answer №1

To ensure the image fits within its container, set the max-width to 100% and the height to auto.

Approach 1 : Allow the image to expand up to the width of its parent div:

.wrapper {
  width: 500px;
  height: 500px;
  border: 1px solid red;
  position: absolute;
}
.wrapper img {
  /*width:100%;*/
  height: auto;
  max-width: 100%;
}
<div class="wrapper">

  <img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">

</div>

Approach 2 : Initially limit the image width using max-width property to fit thumbnail size.

On click of the image, remove the thumbnail class so it grows to original size overflowing the parent container.

.wrapper {
  width: 100px;
  height: 100px;
  border: 1px solid red;
  /*position: absolute;*/
}
.wrapper img.thumbnail {
  /*width:100%;*/
  height: auto;
  max-width: 100%;
}
<div class="wrapper">
  Initially
  <img class="thumbnail" src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">

</div>

<div class="wrapper">
  On click
  <img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">

</div>

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

What is the best way to transfer a row from one table to another using jQuery by selecting the row and then clicking to move it?

As a newcomer to stackoverflow, I am encountering an issue while working on two tables. There is a right button that should move the row into the next table, but unfortunately, my attempts to make it work have been unsuccessful. I would greatly appreciate ...

How come once I close a PrimeNG modal that is defined within a child component, I am unable to reopen it?

Currently, I am developing an Angular application that utilizes PrimeNG. In the process, I encountered a challenge. Initially, I had a component with a PrimeNG Dialog embedded within (refer to this link), and it was functioning properly. To streamline my ...

When using Mongoose paginate, there is always one missing document

I currently have a database with 6 documents and the following route: router.get('', async (req, res) => { const search = req.query.search !=null ? req.query.search : ""; const page = req.query.page !=null ? req.query.page : 1; const limit = ...

Primeng Growl component in Angular doesn't wrap text to the next line

Currently, I am facing an issue in Angular/Javascript where the text in growl is not wrapping using primeng 4.0.1. The problem arises when inserting a long file path into the growl, causing it to overflow from a single line. I am looking for a solution tha ...

Retrieve all true and false values that have been checked or unchecked in an array using jQuery AJAX, and send them to a Laravel controller

How can I set a default value of false for a checkbox, and when it is checked, display true with its name like: ("user_management_create":true). Also, upon form submission, if no checkbox is checked, provide a false value as well like ("User_management_eri ...

Is there a way to obtain an Instagram login token through AJAX?

I'm encountering issues while trying to receive a token from Instagram. Despite using the following code, I keep running into these errors: SEC7127: CORS request blocked the redirect. SCRIPT7002: XMLHttpRequest: Network Error 0x2ef1, Unable to final ...

Selecting nested <div> elements in jQuery can be

What is the best way to target a div element that contains the text "my content"? <table> <tr> <td class="ms-disc-bordered-noleft"> <div class=""> <div>some content</div> <div>my conten ...

The Power of ReactJS Spread Syntax

Currently working with React. In the state, I have an array of objects. this.state = { team: [{ name:'Bob', number:23 }, { name:'Jim', number:43 }] } My issue arises when attempting to create a copy of the arr ...

The hyperlinks on my website seem to be malfunctioning and I'm unable to resolve the issue

Regrettably, I must admit that my knowledge of HTML, CSS, and Bootstrap is quite limited, so the issue at hand may be something simple that I have overlooked. I designed a website for a friend which includes mailto links on a button. Initially, they were ...

Ensure that the children elements can be resized without exceeding the boundaries

My goal is to ensure that the resizable elements stay within their parent element. What I tried: I have set the minHeight : property and while resizing, I am using Math.max to limit the height to 30px. Expected Result: All resizable child elements sh ...

Next.js: Dealing with special characters in YouTube video API snippet titles

Trying to find the perfect video snippet title without any special characters. Accessing the API: https://www.googleapis.com/youtube/v3/search, along with specifying snippet. The current output for snippet.title is as follows: I&#39;M GONNA CARRY ...

Present an error message via AJAX if the action is unauthorized in Laravel 5.1

When checking a policy in a controller, it can be done like this: $this->authorize('user', $post); In the Laravel 5.1 documentation, it states: If the action is authorized, the controller will continue executing normally; however, if the au ...

Transform written content into visual data using canvas technology

Trying to develop a basic application that converts form data into an image. For instance, when the form is filled out dynamically and submitted, the goal is to translate the input into an image. Is it possible to achieve this using just javascript, vue. ...

Notifications for AngularJS tabs

I need help finding a method to incorporate "tab notification" using AngularJS, in order to show that there are important alerts that require attention. For example: (1) (3) TAB_ONE TAB_TWO TAB_THREE Could you provide any recom ...

ObtainComputedStyle yields surprising CSS properties

Can anyone help me figure out why I'm not able to access a CSS rule's value correctly using getComputedStyle? Here's what I'm dealing with: background: linear-gradient(to right, red , yellow); However, the result I'm getting is d ...

How can we utilize the Next.js pages router to efficiently import code that should be executed on the client side?

I am incorporating Google Maps into my project using the @googlemaps/extended-component-library library. You can find more information about this library here. import '@googlemaps/extended-component-library/api_loader.js'; import '@googl ...

Step-by-step guide to creating a dynamic button that not only changes its value but also

I am trying to implement a translation button on my website that changes its value along with the text. Currently, I have some code in place where the button toggles between divs, but I am struggling to make the button value switch as well. Given my limit ...

Pressing Ctrl + S enables you to preserve the webpage for future

Is there a way to save an entire webpage, including all the HTML files, using Ctrl + S in Chrome? I have tried two different methods for saving the page, but so far, neither of them has worked: from selenium import webdriver from selenium.webdriver.common ...

Can a website be designed to mimic the style of the current operating system?

Is there a way to apply CSS styling to web site elements, such as buttons, that mimics the appearance of operating system (Windows, macOS, Linux) button styles? This would allow HTML buttons to automatically adjust their CSS style whenever I switch theme ...

Accessing feedback from Reddit's API

After writing some code to search Reddit's API with a specific query, I now want it to display comments as well. Inside my $.getJSON statement that retrieves each title/post based on the search query, I have the following nested code block. The goal i ...