Align images of varying sizes vertically within div containers, even when the image is larger than the div itself

I'm facing a challenge when it comes to aligning images vertically inside divs.

The problem arises due to the following conditions:

  1. The images will have varying and unknown sizes.
  2. These images are larger than the divs they are contained in, requiring them to be masked by the divs.
  3. The website uses a responsive design approach, with media queries adjusting the layout for different screen sizes. This results in the sizes of the containing divs changing based on the screen size.
  4. On mobile devices like smartphones or tablets, the size of the containing div changes when the device is turned, without requiring a page refresh. The challenge here is to keep the image centered within the div under such circumstances.

I've encountered issues using display:table-cell because the images exceed the div size, making it difficult to maintain proper height and apply overflow:hidden.

I attempted to use jQuery vAlign, which works well unless there are changes in screen size (such as rotating a device). Since it's called on (document).ready, the alignment does not update dynamically without a page refresh.

Is there a way to trigger vAlign using media queries? If not, is there a workaround for the table-cell method that allows smaller div to mask larger img with overflow:hidden?

UPDATE: I've been experimenting with pure CSS (view the working example here) but still struggling to achieve the desired result.

Answer â„–1

I'm not entirely clear on your meaning, but if I understand correctly, you can achieve the desired result by using the following HTML structure

<div class="vertical">
    <div class="wrapper"><img src="image.jpg"></div>
</div>

Along with the following CSS

div.vertical { display:table-cell } 
div.wrapper { display:block;position:relative;overflow:hidden; }
div.wrapper img { position:absolute; }

This setup should allow you to mask (or crop) the image and align it vertically. Keep in mind that the div.vertical class is just for demonstration purposes and you will need to replace its contents with your actual application requirements.

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

Achieving perfect center alignment for the middle element with flexbox

When it comes to the layout below, my objective is to always keep the middle item centered within the container div. This means that the left and right items should be aligned to the far left and right of the container respectively. The current styling fo ...

The Angular Material date picker unpredictably updates when a date is manually changed and the tab key is pressed

My component involves the use of the Angular material date picker. However, I have encountered a strange issue with it. When I select a date using the calendar control, everything works fine. But if I manually change the date and then press the tab button, ...

Get Code Now Button

I operate a website dedicated to Amazon reviews, and I am in need of a button that can request a code from a text file uploaded by the seller. My question is how can this be achieved using HTML? I have been experimenting with this sample: <!-- Butt ...

Enhancing Sharepoint Webpart with AJAX Capabilities

Seeking a step-by-step guide on how to implement a Webpart with AJAX functionality in Sharepoint 2010 using only jQuery and avoiding the Microsoft AJAX toolkit. Can all the necessary components be wrapped in a webpart for easy deployment to a Sharepo ...

"Encountering a challenge when trying to populate a partial view using AngularJs and MVC

I am a beginner in AngularJS and I'm using a partial view for Create and Edit operations, but I'm encountering issues while trying to retrieve the data. The data is successfully being retrieved from my MVC controller but it's not populating ...

I'm facing an issue with grid-template-area not working properly, even though I've ensured that

My div elements are not aligning with the grid template areas specified in the CSS. When inspecting in Firefox, it shows as "zero one two three four", "five six ... etc." The classes mentioned apply to divs within the container div with a class of "spaces ...

The parameters of a submitted form in JQuery are constantly changing

I attempted to pass a value to another page through the URL. I cannot hardcode it directly in the HTML because I need to gather information from multiple steps first. Therefore, I must modify the form action before it is submitted. Below is the form struc ...

How can I dynamically update the status displayed in a DIV tag using PHP code?

I'm working on a web page where I am downloading data one by one in a continuous loop. Once each download is complete, I need to update the status displayed in a DIV tag on the webpage. The server connection and data download are handled by PHP code, ...

Problem with Cakephp 2.2: Using Jquery Ajax with Js->link to save multiple entry ids on a single page

Is it possible that this question has already been addressed elsewhere? Despite my efforts, I have not been able to locate the answer. Cakephp is usually a reliable platform for me, but currently, it's causing me a lot of frustration. I am attempting ...

Ways to activate jQuery validation when submitting a form instead of triggering it on blur

I am currently utilizing the Jquery Validation plugin to validate form fields upon submission. However, I have encountered an issue where incorrect email format triggers a validation message when moving to the next input field. This behavior is undesirable ...

What are some methods for concealing custom CSS overflow in IE8?

In my latest project, I created a script that utilizes pure css to toggle between an image, image caption, and text. However, when testing it in IE8, all the elements appear at once instead of toggling as intended. To showcase the issue more clearly, I h ...

Updating Multiple Rows in Kendo jQuery Grid

Using the cellClose event to update cell values is functioning properly within the selected row. In my table, there is a last row labeled "Total" which displays the sum of the values in Column D. ----------Column_A Column_B Column_C Column_D ----------Pr ...

html input type called array named AmountMap[1] is unable to be configured with <input type="textbox" size="15" name="amountMap[1]" value="0" >

**Having trouble setting the value of an HTML input type named like an array AmountMap[1] <input type="textbox" size="15" name="amountMap[1]" value="0" > When trying to set amountMap[1].value='10', ...

Replicate the function of the back button following the submission of an ajax-submitted form to Preview Form

I am currently working on a multi-part form with the following data flow: Complete the form, then SUBMIT (using ajax post) jQuery Form and CodeIgniter validation messages displayed if necessary Preview the submitted answers from the form Options: Canc ...

Tips on aligning a v-btn alongside v-expansion-panels on the same row

Struggling with aligning my layout. Trying to get a single set of v-expansion-panels and a v-btn in the same row, both visually centered within a card. I managed to almost achieve it in this codepen: https://codepen.io/anzuj/pen/PoPPbdw with the following ...

Is it possible to reset only certain data values in Vue without re-initializing the entire set?

In the development process, we often encounter the need to re-initialize data structures. One common method is as follows: function initialData() { return { is_active: true, is_collapsed: true, resetable_data: 'value' ...

What is the best way to change the value of a key in a JSON Object?

I am currently utilizing _underscore library. My goal is to change the value of a specific key. var users = [{ "_id": { "$oid":"3426" }, "name":"peeter" }, { "_id": { "$oid":"5a027" }, "name":"ken" }, { "_id": { "$oid":"5999" }, ...

What could be causing the React.js axios data to display on the console but not on the screen?

I am currently working on a React Axios Project using data from . The characters data includes another API link, so I implemented an Axios loop to display the names of the characters. While I can see the characters' names in the console, they are not ...

Unique ActionBar design for my NativeScript-Vue application

I'm currently working on customizing the ActionBar for my nativescript-vue app. I have implemented FlexBoxLayout within the ActionBar, but I am facing an issue where the icon and title of the bar are not aligning to the left as intended; instead, they ...

Flexbox properties are being ignored by input fields

Hey there, I'm currently working on a product calculator and need to create 9 input fields organized in 3 rows of three columns. However, when I try to use display: flex and flex-direction: row on the parent div, it doesn't seem to be working as ...