Creating a flexible image layout within a container with relative positioning

I attempted to create a basic slideshow with images sliding from right to left as an animation:

let slides = document.querySelectorAll('.img-container');
let l = slides.length;
let i = 0;

setInterval(function(){
i = (i + 1) % l;
if(i == 0){
for(let j = l - 1;j != 0;j--){
slides[j].classList.remove('img-slide');
}
} else {
slides[i].classList.add('img-slide');
}
}, 3500);
body {
margin: 0px;
}

.section-1 {
width: 100%;
height: calc(100vh - 104.5px);
overflow: hidden;
display: flex;
}

.img-container {
width: 100%;
position: relative;
display: flex;
justify-content: center;
transition: ease 0.7s;
}

.img-container:nth-child(1){
z-index: 0;
}

.img-container:nth-child(2){
z-index: 1;
}

.img-container:nth-child(3){
z-index: 2;
}

.img-container:nth-child(4){
z-index: 3;
}

.img-container:nth-child(2).img-slide{
transform: translateX(-100%);
}

.img-container:nth-child(3).img-slide{
transform: translateX(-200%);
}

.img-container:nth-child(4).img-slide{
transform: translateX(-300%);
}
<section class="section-1">
<div class="img-container">
<img src="https://i1.ytimg.com/vi/PKffm2uI4dk/maxresdefault.jpg">
</div>
<div class="img-container">
<img src="https://i1.ytimg.com/vi/PKffm2uI4dk/maxresdefault.jpg">
</div>
<div class="img-container">
<img src="https://i1.ytimg.com/vi/PKffm2uI4dk/maxresdefault.jpg">
</div>
<div class="img-container">
<img src="https://i1.ytimg.com/vi/PKffm2uI4dk/maxresdefault.jpg">
</div>
</section>

The example images are stretched because they were placeholders. I have four different png images of 4000x4000 dimensions with transparency, so the "img-container" needs to be 100% of the body and have a background color. The section should have a fixed height. However, when using these larger images, they appear stretched and cropped on mobile devices. I applied:

img {
   width: 100%;
   height: auto;
}

But this caused all four images to shrink and display simultaneously in the viewport. How can I address this issue or where can I find more information on this topic?

Answer №1

In order to accomplish this task, you have the option to utilize the object-fit property with values such as object-fit: cover or object-fit: contain within the image tag.

element.style {
    object-fit: cover;
}

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

Mastering the Art of jQuery: Easily Choosing and Concealing a Div Element

I'm currently facing challenges in removing a div upon successful AJAX completion. The issue I'm encountering is that the word "Added" appears twice after success, indicating that I am not properly selecting the two divs containing it. Any sugges ...

Tips for applying CSS conditionally to content at varying nesting levels

Currently, I am working with sass and finding it challenging to modify the structure of the page easily. The layout I have in place is due to my header having varying sizes depending on the specific page users are browsing: <div class="container"> ...

Scraping a few URLs with Javascript for Web Data Extraction

I'm struggling to retrieve data from multiple URLs and write it to a CSV file. The problem I'm facing is that the fetched data is not complete (I expect 10 items) and it's not in the correct order. Instead of getting 1, 2, 3 sequentially, I ...

Redux saga will halt all other effects if one fails during execution

Having some trouble with yield all in saga effect, I've included a snippet of my code below function* fetchData(item) { try { const data = yield call(request, url); yield put(fetchDataSuccess(data)); } catch (error) { yield put(fetchDa ...

Randomly undefined custom jQuery function

Appreciate your assistance in advance. I am facing a peculiar scope issue on a page where multiple charts are rendered intermittently. I have created a set of jQuery functions to display different types of charts based on the provided options. Each funct ...

What does the HTML and JS code look like when using Next.Js?

Here's an illustration of the desired result: codepen.io/j0be/pen/jWGVvV How can I implement this HTML and JS in Next.js? I am looking to customize this code using TypeScript and SCSS in Next.js. However, I am unsure about how to convert the HTML an ...

What's the best way to trigger an event within a tag in a Vue.js component?

app.js Vue.component("popup",{ template : /*html*/` <div class="popup is-active" > <div class="popup-background"></div> <div class="popup-card"> <header class=" ...

I am looking to implement a dropdown menu that appears after clicking on an image. Any suggestions on how to achieve this using

I've been experimenting with adding a dropdown class, but I'm feeling a bit lost on where to start. Here's a snippet of code that shows what I'd like to do to add a dropdown menu: <span id="dropdown-info" ng-init= "myVar='i ...

Getting the URL of a CSS background image in NodeJS and Express: A step-by-step guide

I've recently learned that using getComputedStyle in a Node environment is not possible due to it being a browser-specific behavior. Despite this, I am still interested in retrieving all background image URLs within Node and downloading them as shown ...

Retrieving the ID and value of a specific dropdown list using jQuery from an array of dropdown lists

My HTML structure looks like this: <?php foreach($active_brand as $brand) { ?> <select name="selector" id="selector"> <?php foreach($options as $option) { ?> <option <?php if($brand['State'] == $option) { ?& ...

Is it possible to adjust the zoom on my website so that it adapts to the user's higher resolution if my website layout is initially set for 800x600 resolution?

Apologies for the confusion in my previous question. I am not looking to alter the user's browser settings but rather focus on optimizing my website layout for the most common resolution size. Ideally, I want my page to adjust and zoom in if the user& ...

Arrange an array by the occurrence rate of its elements within objects contained in another array, using Javascript

I found the explanation to be rather complex. Essentially, my goal is to iterate through each object in the 'objects' array, analyze the 'choice' values, tally the frequency of each letter, and then arrange the original arrays based on ...

The router component in "react-router-dom" is not functioning properly

My goal is to explicitly utilize history in my project. While I am familiar with BrowserRouter, I prefer to use Route and make history one of its properties. Upon running the program, I encounter this page: enter image description here Below is my AppRout ...

Updating Array Values in AngularJS based on Input Text Box Modifications

In my application, there is a text box that looks like this - <input type="text" class="form-control" id="inputID" name="ItemId" ng-model="inputItemId" ng-required="true" ng-blur="addValueToArray(inputItemId)"/> The user has the ability to add or r ...

Are there any techniques for running unit tests on a Vue.js transition?

I have been working on a Vue component that includes a transition with a dynamically generated name. My challenge is to test whether the transition name is correctly set based on the props I pass into the component. Below is the code snippet of the compone ...

Issues with functionality of React/NextJS audio player buttons arise following implementation of a state

I am currently customizing an Audio Player component in a NextJs application using the ReactAudioPlayer package. However, the standard Import Next/Audio and using just <Audio> without props did not yield the expected results. The player functions as ...

What are the steps to create a class diagram for a NodeJS application?

For my final year project, I am looking to develop an application using Node API. As we delve into drawing the class diagram, it occurs to me that unlike Java or C#, Node JS does not have a built-in class concept. What would be the most effective approac ...

Add the $scope ng-click event to a previously hidden element once it becomes visible

If you need a clearer explanation, feel free to ask. I have incorporated a global search function into the header of my website. I am looking to show a separate input box for mobile search that utilizes the same ng-click event, but the input field remains ...

Issue with Element's Response to JQuery Click Event

After pulling elements from the database in PHP, I utilized Jquery to add Elements to the page. Each button has two classes; one for controlling the GUI and another for handling the click event of that specific button. Here is the code snippet: echo " ...

Optimal approach for managing numerous modals containing map data in Next.js

Hey there, I'm facing an issue while trying to utilize map data in conjunction with modals. Although I have set the state for all modals, when I use an array object within the map data, the modals end up showing duplicated. To provide clarity, let me ...