Having trouble getting vertical-align and background-position CSS properties to work when using JavaScript

I'm currently facing an issue with vertically cropping an image within a div. I've been struggling to make the vertical-align and background-position styles work as intended...

Despite trying various options like vertical-align, background-position, background-position-y, inline background image URL in style, nothing seems to be accomplishing the desired outcome...

The images are loaded from HTML within a js file:

<div class="product-image-wrap" data-slider-quickview="">
      <img src="${result[i].images[0].src}" class="variant-image-${result[i].images[0].id}" alt="">
 </div>

The CSS code used is as follows:

  .product-image-wrap {
    overflow: hidden;
    border-radius: 8px !important;
    padding: 12px;
    width: 100%;
    height: 170px;
    background-position: center, center;
    background-repeat: no-repeat;
     }

    .product-image-wrap .img {
      display: inline-block;
      width: 100%;
      height: 170px;
      vertical-align: middle;
       }

Answer №1

It's important to make sure I grasp your objective correctly. Perhaps you are looking to adjust the height of the container while preserving the image's aspect ratio? If that is the case, you can utilize object-fit: cover; on the image and then modify the height of the container div as needed:

div {
    overflow: hidden;
    border-radius: 18px;
    padding: 12px;
    width: 100%;
    height: 170px;
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div>
  <img src="https://www.petmd.com/sites/default/files/Acute-Dog-Diarrhea-47066074.jpg">    
 </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

Convert the onChange event in JavaScript to a SQL query

Trying to figure out the best way to achieve this, but I'm hitting a roadblock in my code. Essentially, I want the user to have the ability to select time intervals in increments of 30 minutes up to a maximum of 5 hours (which would be 10 options). Ea ...

The responsive class seems to be malfunctioning within the Laravel 5.3 framework

I have a project that involves Laravel and Vue.js. In my Laravel project, I am importing Bootstrap CSS using the following line: // Bootstrap @import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap"; This import is done in the app.scss file. ...

What is the extent of a variable within a jQuery function?

Similar Question: Understanding Asynchronous Behavior in Ajax Question: Why does my variable "temp" remain as 0 after the AJAX request? function calculate7() { var temp = 0; $.ajax({ type: "POST", ...

Cross-Origin Resource Sharing (CORS) verification for WebSocket connections

I am currently utilizing expressjs and have implemented cors validation to allow all origins. const options = { origin: ['*'], credentials: true, exposedHeaders: false, preflightContinue: false, optionsSuccessStatus: 204, methods: [&a ...

Having trouble parsing this JSON, getting duplicate results

Displayed below is the JSON data stored in data: { "dialogue":{ "id":"1", "backgroundURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/d-backgrounds\/onboard-metro.jpg" }, "di ...

Customize footer data in ui-grid design

I'm having trouble formatting the aggregate value for a column in ui-grid. Currently, the number display looks like this: total: 6370.046074130321 but I would prefer it to be formatted like this: total: $6370.05 I've attempted using both of ...

How can I customize the tooltip placement in Bootstrap 5 while utilizing the 'text-truncate' attribute?

In my HTML table, I have long text in a cell that I don't want to display fully. To truncate the text, I am using the 'text-truncate' CSS attribute. Additionally, I am considering using Bootstrap tooltip to show the full text when needed. He ...

An unanticipated SyntaxError was encountered while attempting to utilize an Ajax post, specifically in relation

I can't seem to figure out how to troubleshoot an ajax/jquery error. Here is the function I'm working with: var LogIn = { Email: $("#Name").val(), MobileNo: $("#txtMobileNumber").val(), PinCode: '', ...

What is preventing my Grails application from retrieving an object from my Angular script?

I'm currently working on adding objects to a JSON list in Grails. Below is the Angular code I am using: http://pastebin.com/4ypijUMD Additionally, here is my Grails controller: http://pastebin.com/skTtVtxv My understanding is that the angularJS scri ...

Trigger a drop-down list in React when typing a specific character, like {{ or @, in an input field

Is there a way in React.js to display a list or dropdown when a user types a certain character like '@' or '{{ }}' in the input text area? The user should be able to select an option from this list and have it inserted into the text are ...

Aligning a bootstrap grid in the center for mobile devices

I've encountered an issue where my 2×3 grid of images and text is perfectly centered on desktop but has a strange offset on mobile devices. Here's how it appears on the desktop: https://i.sstatic.net/HXqFq.jpg and here's the layout on mob ...

Transforming a C# .NET list of strings into an HTML dropdown menu

I've created a list of strings in my c# .net application and I want to store this list in an HTML dropdown. However, I'm not sure if I've done everything correctly in my view. Here's the code snippet: CalendarFolder calendar = ...

Tips and tricks for displaying a confirmation modal prior to a user switching to another tab

In my application, I have a section designed with Bootstrap 4 tabs. There are six tabs, and five of them contain forms with input fields. The original code was submitting all data (for all 5 out of 6 tabs) at once. This approach seemed unnecessary to me si ...

Extracting data from a JSON file and displaying it using Angular

I'm facing a challenge with parsing two JSON format data and displaying them in Angular. I'm unsure of how to proceed and need guidance. The second data includes a plan_id that refers to the "plan" data. How can I create a small lookup object to ...

I am interested in developing a Menu system that features different categories and subcategories

I am in need of assistance in creating a menu that includes both categories and sub categories. An example of what I am looking to achieve can be seen on the website www.boots.com If anyone has any suggestions or advice to offer, I would greatly apprecia ...

JavaScript: Break apart and tally the number of words within a given string

If I have an input string with the value "testing the string", I want to create a function that splits each word and counts how many times each word appears in the input string. The desired output should look like this: testing: 1, the: 1, string: 1 Than ...

What potential drawbacks come with opting for html5 semantic tags instead of traditional div elements?

A question with an unconventional twist has been raised several times before and the discussion continues. Despite this, no concrete disadvantages or evidence of their complete absence have been found. I fully support advancements in technology and creati ...

What is the method for retrieving the font size of elements in the native view using appium?

Can the font size of text in the native view be retrieved using appium? I am aware of using driver.findElement(By.id("by-id")).getCssValue("font-size"); for web views. Is there a similar method for native views? ...

Utilizing React Typescript to Efficiently Manage Multiple Checkboxes within a List

I'm working on a scenario where I have to manage multiple checkboxes in a list Only one checkbox can be selected at a time For example, if I toggle on Checkbox 1 and then click on Checkbox 2 - I want to automatically toggle off Checkbox 1 as I toggl ...

Guide on retrieving the value of "form" from a select using jQuery in a Ruby on Rails application

I am struggling to figure out how to use jQuery to pass the value of the form attribute from the select tag. I have been trying different ways, but so far haven't been successful. When using simple_form_for, the input statement looks like this: < ...