Issue with maintaining image aspect ratio; unable to utilize background-image: cover property

If what I am attempting is not feasible, please inform me.

I am currently working on displaying images of various sizes without distortion. While most images look good, those with a smaller width than the container of 285px are getting distorted. I am fine with them being blurry as they stretch, but I want to maintain their aspect ratios. The problematic images are #2, #3, and #4.

I am restricted from using JavaScript for this task; it must be achieved solely through CSS. Additionally, I cannot use style="background-image:url" because the images are added dynamically, causing the entire script to malfunction. This limitation also rules out background-image:cover.

http://jsfiddle.net/pp74fb7b/9/

.squaregallerywrap {
width:  285px; 
height: 285px;
}

.squaregallerywrap img {
min-width: 100%;
min-height: 100%;
max-height: 286px;
min-width: 286px;
display:block;
margin:auto;
position:relative;
-moz-transform:translateX(-50%);
-webkit-transform:translateX(-50%);
-ms-transform:translateX(-50%);
-o-transform:translateX(-50%);
transform:translateX(-50%);
left:50%;
}

li {    
float: left;
overflow: hidden;
-webkit-transition: 0.4s all linear;
transition: 0.4s all linear;
}

Answer №1

This code snippet provides a clean CSS solution for creating a square image gallery grid.

CSS:

.squaregallerywrap {
    width: 285px;
    height: 285px;
    overflow:hidden;
    margin:0;
    padding:0;
}
.squaregallerywrap img {
    min-width: 100%;
    min-height: 100%;
    display:block;
    margin:auto;
    position:relative;
    -moz-transform:translateX(-50%)translateY(-50%);
    -webkit-transform:translateX(-50%)translateY(-50%);
    -ms-transform:translateX(-50%)translateY(-50%);
    -o-transform:translateX(-50%)translateY(-50%);
    transform:translateX(-50%)translateY(-50%);
    left:50%;
    top:50%;
}
li {
    float: left;
    overflow: hidden;
    -webkit-transition: 0.4s all linear;
    transition: 0.4s all linear;
}

See the live demo on JSFiddle: JSFiddle

Answer №2

Instead of adding dynamically, have you considered inserting the images like this:

<div class="dynamicgallerywrap" style="background:url('http://uniquewebsite.com/images/gallery/2/1.jpg')">

Afterwards, apply background-size:cover to .dynamicgallerywrap

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

Ensuring the input element is correctly aligned in its position

How can I center the input element inside its parent without any top margin? What CSS changes do I need to make? #menu { height:30px; background: #ccc; } #board-name { display:block; margin-left:auto; margin-right:auto; height: 80%; vertical-al ...

Why isn't the 100% max-width image liquid layout being applied?

This whole concept of liquid layout is new to me. It's frustrating how everything in my layout adjusts except for the images. I've been attempting to use max-width: 100% on images, as recommended in various sources. Yet, no matter how I define ...

Update the image path in Vue depending on the size of the screen

I'm looking to dynamically change the image src based on screen size in my Nuxt project. The current code I have is as follows: <div v-for="(item, index) in aside" :key="index""> <img :src="item.svgIconDark.fi ...

Set the height of the last child element to 100% in the CSS

Seeking assistance to adjust the height of the final list item to 100% in order to match the varying height of the right-floated div. ul.menu li:last-child {height:100%} Here is an example of my current situation: http://jsfiddle.net/kvtV8/1/ Any help ...

Is there a way to maintain image proportions when resizing the window?

I'm in the process of developing a classroom-themed website with interactive overlaying images. The goal is to have various pictures that users can click on to access different resources from different websites. My main challenge right now is getting ...

What is the best method for sending data from an HTML page to a Django view in order to utilize it

I'm encountering an issue with Django urls. I need to pass the value entered in the Name input field after clicking the submit button. Let's assume I have the following HTML form- <form method='POST' action="{% url 'submittedf ...

Customize the Gap Between Inline Radio Buttons in MaterializeCSS

Help needed to align MaterializeCSS's radio buttons inline without gaps. Thank you in advance for any assistance! Below is a snippet of the code: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize ...

Utilize CSS exclusively on the "Theme Options" page within a Wordpress website

Everything is running smoothly with my current PHP code, which controls the design of my "Theme Options" page under the WP API Appearance menu. However... The issue is that the CSS stylesheet is affecting all other menus in the WP dashboard as well, like ...

I tried moving the onchange(this) function from HTML to JavaScript, but I seem to have missed a parameter. The code ends

I'm currently building a website for a fictional ice cream shop to enhance my JavaScript skills. function hideAAndB() { var pickupDiv = document.getElementById("pickupDiv"); var deliveryDiv = document.getElementById("deliveryDiv"); pickupDi ...

Just recently updated to Angular 14 and I'm encountering a problem with images not loading from the assets folder. I'm wondering if there is a configuration step I missed. Could someone please

https://i.stack.imgur.com/4LEQ4.png https://i.stack.imgur.com/3sxzF.png Is there a configuration missing in Angular 14? When I try using <img [src]="assets/images/sidebarNav"> with ./, ../, it doesn't work. I have followed the instr ...

What is the best way to export image paths using require() from a single index.js file in a React Native project?

I am looking for a way to efficiently export all images from a single file named index.js, so that in the future, if I need to change the path of an image, I only have to make changes in one file. For example: export {default as avatar} from './avata ...

Retrieve the 'computed' CSS for the entire webpage

We operate multiple websites, each referencing 3-5 CSS files. Some of these files are shared across sites while others are specific to certain pages. Our aim is to streamline the CSS structure by consolidating into fewer files without altering the end res ...

I'm unable to adjust the font size of the final h3 element

I'm currently designing a webpage with various titles, but I've encountered an issue. I am unable to adjust the font size of the last h3 title without affecting the rest of the titles. I'm curious as to why this is happening and how I can re ...

Learn the step-by-step process of cropping and zooming an image using the react-image-crop

I am looking to implement zooming and cropping functionality for an image using react-image-crop instead of react-easy-crop. Currently, the cropping does not take into account the zoom level set by ReactCrop's scale property. I want to be able to zo ...

Bootstrap overrides the color of the calendar year in jQuery UI

I am facing an issue where the CSS styles of my jQuery UI calendar are being overridden by the Bootstrap CSS styles. Take a look at the screenshot provided. As you can see, the text color of the calendar's year is not black as intended. https://i.ss ...

Experiencing varying top offsets on initial click followed by consistent offsets on subsequent clicks

The first image shows jquery(document).height() and the second image shows offset().top. I am attempting to scroll to an element with a fixed header, but on the first click I get a different value for offset().top than on the second click. It seems that th ...

Uploading images simultaneously while completing another form

There are countless tutorials online on how to upload images to express.js servers. However, I have been unsuccessful in finding a way to upload images while simultaneously performing other tasks, such as: Imagine there is a form where users can create an ...

Arranging the placement of an arrow within an HTML dropdown menu

I'm struggling with positioning my custom arrow in select elements. Each select has a different width, and I need to ensure that the distance from the right edge of the select to the arrow is consistent across all of them. Currently, I am using backg ...

Testing a feature in Angular that modifies a variable

Trying to test a function that updates a Boolean variable has been causing some confusion for me. The strange thing is, even though the function seems to be called successfully when using the toHaveBeenCalled method, the variable itself never actually gets ...

Limit the ability to zoom in a webview application

I am facing an issue with my Android WebView app that is designed to load a specific URL and display it along with its links within the app. Despite setting the webpage size to 720px x 1280px using CSS to fit the screen of a Galaxy S3, I am encountering di ...